Home » SQL & PL/SQL » SQL & PL/SQL » Order of WHERE columns doesn't matter? The optimizer will handle that? (Oracle 11g)
Order of WHERE columns doesn't matter? The optimizer will handle that? [message #617997] Sun, 06 July 2014 10:39 Go to next message
rc3d
Messages: 213
Registered: September 2013
Location: Baden-Württemberg
Senior Member
Hi

An Oracle guy said me that the order of WHERE columns and filters doesn't matter. The optimizer will handle that.

Is that true?

I did always as first WHERE argument the clause which filters most rows out. I did the most precise at the end.

Re: Order of WHERE columns doesn't matter? The optimizer will handle that? [message #617998 is a reply to message #617997] Sun, 06 July 2014 10:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
Is that true?

Yes, in these days, it is.

Quote:
I did always as first WHERE argument the clause which filters most rows out.


In the old days, this was the wrong way, you should put the most restrictive conditions at the end, but this no more matter now.

Re: Order of WHERE columns doesn't matter? The optimizer will handle that? [message #618005 is a reply to message #617998] Sun, 06 July 2014 11:31 Go to previous messageGo to next message
rc3d
Messages: 213
Registered: September 2013
Location: Baden-Württemberg
Senior Member
thanks Michel
Re: Order of WHERE columns doesn't matter? The optimizer will handle that? [message #618008 is a reply to message #618005] Sun, 06 July 2014 12:58 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
In most cases, as Michel noted, optimizer will choose best WHERE clause predicate sequence. And in few cases where predicate sequence chosen by optimizer isn't best choice you can list WHERE clause predicates in what you think is best order and use ORDERED_PREDICATES hint.

SY.
Re: Order of WHERE columns doesn't matter? The optimizer will handle that? [message #618164 is a reply to message #618008] Tue, 08 July 2014 13:54 Go to previous messageGo to next message
rc3d
Messages: 213
Registered: September 2013
Location: Baden-Württemberg
Senior Member
Solomon Yakobson wrote on Sun, 06 July 2014 12:58
In most cases, as Michel noted, optimizer will choose best WHERE clause predicate sequence. And in few cases where predicate sequence chosen by optimizer isn't best choice you can list WHERE clause predicates in what you think is best order and use ORDERED_PREDICATES hint.

SY.


Oracle knows best. I can't be better than Oracle developers.
Re: Order of WHERE columns doesn't matter? The optimizer will handle that? [message #618203 is a reply to message #618164] Wed, 09 July 2014 06:25 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Oracle knows best (paraphrasing Einstein's "The more I learn, the more I realize how much I don't know") not to assume it knows all - that's why Oracle offers 2+ dozens of hints. And "I can't be better than Oracle developers" mentality leads nowhere.

SY.

Re: Order of WHERE columns doesn't matter? The optimizer will handle that? [message #618205 is a reply to message #618203] Wed, 09 July 2014 06:47 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
I cannot agree with this. Hints are usually not needed because Oracle got something wrong, but because YOU got something wrong. Typically, lack of extended statistics. Many of the new features (cardinality feedback, SQL directives, adaptive plans, dynamic sampling) are all about correcting DBA errors in this area.
Re: Order of WHERE columns doesn't matter? The optimizer will handle that? [message #618461 is a reply to message #618205] Sat, 12 July 2014 01:35 Go to previous messageGo to next message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
I remember back in 1989 (+- a year or two) some guy doing a presentation at an Oracle conference on this specific topic. Yea, in the early days a whole one hour session could be had on something as simple as this. Ah well... So far what has been said is true but there are a few exceptions to note. Here is a summary.

In the past (pre 11g days), the order of predicates in the WHERE clause could make a difference in performance. This is because not all predicates are equal in their ability to remove rows from a result set and since any row removed does not have to be checked for additional predicates once it has been removed, the faster you remove rows the fewer predicate tests that get applied to the result set overall and thus you get a savings in CPU on the query.

BUT... to my understanding, this only applies to FILTER operations, not ACCESS operations, both of which are visible via the predicate section of the query plan. This has a significant implication. It means that this tuning is almost exclusively only of value in inefficient queries that could benefit from other tuning, or in queries doing FULL TABLE SCANS. If your query is doing partition pruninig or index lookups or joins, then the predicates that participate in these operations are not affected by ordering. This reduces the number of predicates left to be ordered in real world queries by a considerable margin.

In 11g, the optimizer uses statistics to compute the selectivity of predicates and then automatically does the predicates in the order it thinks is most optimal. But there are two problems with this you should keep in mind.

1. this is subject to all the issues of statistics. If the optimizer has to guess on the statistics of a predicate then it may not be ordered in the best order.

2. there is a potential "bug" related to user defined functions. Technically it is not a problem on the oracle side since semantically it should not matter what order predicates are evaluated. But since changing the order of predicates means that intermediary surviving rows change during predicate evaluation, it is likely to change the set of rows that are fed into a user defined function. This should not matter unless the function has a bug in it. It would be a logical bug in the function, not in oracle's reorder of the predicates. I had this problem on a migration to 11g and had to get the developers to fix their code because they assumed that since there was a predicate of AND COLX IS NOT NULL in the WHERE CLAUSE, that no rows would go to the function with a null value in this column. Thus the function did not check for nulls in this column. When 11g reordered the predicates, all of a sudden there were rows going to the function with nulls in the associate column parameter. The function had a bug that caused an infinite PL/SQL loop when a null value was fed into this parameter, so the query went from fast to never finishing (took a while to hunt this one down).

But in the end everyone gets it wrong. Here is the real rule.

There is so much stuff you can be doing to make your system go faster, that if you are spending your time looking at order of predicates, you are wasting it. This is nothing more than an interesting topic in the annals of useless tuning strategies and is why it is a good thing Oracle now does it for us automatically in an attempt to squeeze out a few more milliseconds.

Have fun talking about it. It is a good brain exerciser. But don't waste your time doing it. You should be looking at stuff like design issues, statistics collection, bad coding practices, and you guys can help me out here with 101 other better ways to spend your time tuning an Oracle system.

Kevin
Re: Order of WHERE columns doesn't matter? The optimizer will handle that? [message #618486 is a reply to message #618461] Sat, 12 July 2014 07:18 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Kevin,

I wouldn't dispute order of predicates is even close to be on a list of things one would look to tune. But your example with UDF is one of the cases when it might be. OK, developers fixed the bug in UDF. But it didn't change execution plan and UDF was called before AND COLX IS NOT NULL thus, most likely, resulting in sub-optimal performance (calling UDF too may times) unless you created extended stats for that UDF. But in some cases it isn't possible. For example, products like Siebel have their own stats collection which one doesn't want to mess with too much. So if we need to write non-Siebel code (e.g. wite a report or run ad hoc query) involving Siebel tables (and with Siebel tables 100 million rows is more a rule than an exception) SPM and hints become important tool. Another example is volatile tables where we modify data daily (or even more often) in large volumes. In such situations one needs to weigh stats collection time with performance improvement up-to-date stats provide. Do we want to collect stats if collecting up-to-date stats takes 55 min but most queries run 20 minutes with stale stats and 10 min with up-to-date stats? May be yes or may be not. We need to evalue the rest of the queries. If there are few that run 2 hours with stale stats and only 5 min with up-to-date stats we might look at such queries and apply either SPM or hints (and one of them could be ordered predicates).
Anyway, my point is - I like Oracle providing hints. No, I like Oracle providing multiple tools/ways of query optimization and hints being one of the tools. Using right tool for specific case is a different story.

SY.
Re: Order of WHERE columns doesn't matter? The optimizer will handle that? [message #618569 is a reply to message #618486] Sun, 13 July 2014 12:53 Go to previous message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
Quote:
Using right tool for specific case is a different story.


Right on!

Kevin
Previous Topic: SYS_CONNECT_BY_PATH
Next Topic: how to display any particular column first using select * query.
Goto Forum:
  


Current Time: Thu Apr 25 18:50:16 CDT 2024