Skip to content
Advertisement

Oracle SQL query efficiency Improvement

I have a query which is never finishing (left running for over 24 hours and was still going).

Now there isn’t a huge amount of data in each table so I can only assume it is the efficiency of the query I have written.

Execution Plan

Predicate Information (identified by operation id):

Advertisement

Answer

You have a bunch of OR conditions. I would suggest replacing these by not exists:

I’m pretty sure your performance problem is caused by cartesian products. If a supplier has 100 order lines, and 100 req lines, and 100 invoices, then the join is creating 100*100*100 = 1,000,000 rows just for that one supplier. This is a big intermediate table.

By using EXISTS instead, Oracle will not be producing gargantuan intermediate tables.

Also, you can test the performance by adding one clause at a time.

Finally, I’m not 100% sure if the logic is correct for the middle two conditions. For instance, you might really want this for first NOT EXISTS:

As written, your logic is that at least one state is not 'Closed' or 'Cancelled', which is what the above revision also does. I put in that no states are 'Closed' or 'Cancelled', just because that made more sense to me.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement