The AND-EQUAL Trap

What is AND-EQUAL?

There is a very old feature of Oracle that causes far more problems than it ever solved. One of the first questions asked by a novice SQL tuner is

"Can Oracle use two indexes on a table at the same time?"

The wise answer is "Yes. But..."

If a table contains two or more single column indexes, and two or more of those columns are used in a SQL using equals predicates, then it is possible for Oracle to use all of the indexes on the columns supplied. Oracle will scan each index separately for matching rows and then merge the rowids from each scan, producing a list of rowids that match every index lookup. Oracle calls the feature AND_EQUAL.

AND_EQUAL is used for low volume SQLs, and is not to be confused with Index Join, another multi-index trick for high volume queries.

Sounds good? Well it's usually not. What usually happens is that the indexes all return much the same rows and the AND-EQUAL merge does not really narrow the search down much, or one index is much better than the others and we would be better off just using that one.

Consider the following examples on a table with two single column indexes on columns A and B:

It is a common trap to believe that two single column indexes are (almost) as good as one concatenated index over both columns. This is never the case.

Fortunately, AND-EQUAL is never chosen automatically by the Cost Based Optimizer, so is now rare. It will only happen when:

How do I stop it?

If you see an AND-EQUAL line in your Explain Plan output, check the information above and decide whether AND-EQUAL is really appropriate. If not, calculate the statistics, remove the AND_EQUAL or RULE hint, or if your Optimizer Goal is set to RULE, add a CHOOSE hint to the SQL.

Is AND-EQUAL ever useful?

Hardly ever. Here are some cases where you might use AND-EQUAL.


©Copyright 2003