How do I troubleshoot performance problems with an Oracle SQL statement

前端 未结 9 1517
夕颜
夕颜 2021-02-10 08:12

I have two insert statements, almost exactly the same, which run in two different schemas on the same Oracle instance. What the insert statement looks like doesn\'t matter - I\'

9条回答
  •  悲&欢浪女
    2021-02-10 08:30

    When the performance of a sql statement isn't as expected / desired, one of the first things I do is to check the execution plan.

    The trick is to check for things that aren't as expected. For example you might find table scans where you think an index scan should be faster or vice versa.

    A point where the oracle optimizer sometimes takes a wrong turn are the estimates how many rows a step will return. If the execution plan expects 2 rows, but you know it will more like 2000 rows, the execution plan is bound to be less than optimal.

    With two statements to compare you can obviously compare the two execution plans to see where they differ.

    From this analysis, I come up with an execution plan that I think should be suited better. This is not an exact execution plan, but just some crucial changes, to the one I found, like: It should use Index X or a Hash Join instead of a nested loop.

    Next thing is to figure out a way to make Oracle use that execution plan. Often by using Hints, or creating additonal indexes, sometimes changing the SQL statement. Then of course test that the changed statement

    a) still does what it is supposed to do

    b) is actually faster

    With b it is very important to make sure you are testing the correct use case. A typical pit fall is the difference between returning the first row, versus returning the last row. Most tools show you the first results as soon as they are available, with no direct indication, that there is more work to be done. But if your actual program has to process all rows before it continues to the next processing step, it is almost irrelevant when the first row appears, it is only relevant when the last row is available.

    If you find a better execution plan, the final step is to make you database actually use it in the actual program. If you added an index, this will often work out of the box. Hints are an option, but can be problematic if a library creates your sql statement, those ofte don't support hints. As a last resort you can save and fix execution plans for specific sql statements. I'd avoid this approach, because its easy to become forgotten and in a year or so some poor developer will scratch her head why the statement performs in a way that might have been apropriate with the data one year ago, but not with the current data ...

提交回复
热议问题