The optimizer thinks that the full table scan will be better.
If there are just a few NULL
rows, the optimizer is right.
If you are absolutely sure that the index access will be faster (that is, you have more than 75%
rows with col1 IS NULL
), then hint your query:
SELECT /*+ INDEX (t index_name_on_col1) */
*
FROM mytable t
WHERE col1 IS NOT NULL
Why 75%
?
Because using INDEX SCAN
to retrieve values not covered by the index implies a hidden join on ROWID
, which costs about 4
times as much as table scan.
If the index range includes more than 25%
of rows, the table scan is usually faster.
As mentioned by Tony Andrews
, clustering factor is more accurate method to measure this value, but 25%
is still a good rule of thumb.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…