Why isn't index used for this query?

后端 未结 4 2098
太阳男子
太阳男子 2021-02-05 18:05

I had a query where an index was not used when I thought it could be, so I reproduced it out of curiosity:

Create a test_table with 1.000.000 rows (10 disti

4条回答
  •  春和景丽
    2021-02-05 18:45

    UPDATE: Try making the col column NOT NULL. That is the reason it's not using the index. When it's not null, here's the plan.

    SELECT STATEMENT, GOAL = ALL_ROWS           69  10  30
                        HASH GROUP BY           69  10  30
     INDEX FAST FULL SCAN   SANDBOX TEST_INDEX  56  98072   294216
    

    If the optimizer determines that it's more efficient NOT to use the index (maybe due to rewriting the query), then it won't. Optimizer hints are just that, namely, hints to tell Oracle an index you'd like it to use. You can think of them as suggestions. But if the optimizer determines that it's better not to use the index (again, as result of query rewrite for example), then it's not going to.

    Refer to this link: http://download.oracle.com/docs/cd/B19306_01/server.102/b14211/hintsref.htm "Specifying one of these hints causes the optimizer to choose the specified access path only if the access path is available based on the existence of an index or cluster and on the syntactic constructs of the SQL statement. If a hint specifies an unavailable access path, then the optimizer ignores it."

    Since you are running a count(*) operation, the optimizer has determined that it's more efficient to just scan the whole table and hash instead of using your index.

    Here's another handy link on hints: http://www.dba-oracle.com/t_hint_ignored.htm

提交回复
热议问题