How to use Explain Plan to optimize queries?

前端 未结 4 487
甜味超标
甜味超标 2020-12-24 02:38

I have been tasked to optimize some sql queries at work. Everything I have found points to using Explain Plan to identify problem areas. The problem I can not find out exact

4条回答
  •  温柔的废话
    2020-12-24 03:35

    This is a massive area of expertise (aka a black art).

    The approach I generally take is:

    1. Run the SQL statement in question,
    2. Get the actual plan (look up dbms_xplan),
    3. Compare the estimated number of rows (cardinality) vs actual number of rows. A big difference indicates a problem to be fixed (e.g. index, histogram)
    4. Consider if you can create an index to speed part of the process (generally where you conceptually think the plan should go first). Try some indexes.

    You need to understand the O() impacts of different indexes in the context of what you are asking the database. It helps you understand data structures like b-trees, hash tables etc. Then, create an index that might work and repeat the process.

    If Oracle decides not to use your index, apply an INDEX() hint and look at the new plan. The cost will be greater than the plan it did choose - this is why it didn't pick your index. The hinted plan might lead to some insight about why your index is not good.

提交回复
热议问题