Why isn't index used for this query?

后端 未结 4 2074
太阳男子
太阳男子 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:53

    bitmap index will do as well

    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 2200191467
    
    ---------------------------------------------------------------------------------
    | Id  | Operation          | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
    ---------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT   |            |    10 |    30 | 15983   (2)| 00:03:12 |
    |   1 |  HASH GROUP BY     |            |    10 |    30 | 15983   (2)| 00:03:12 |
    |   2 |   TABLE ACCESS FULL| TEST_TABLE |  1013K|  2968K| 15825   (1)| 00:03:10 |
    ---------------------------------------------------------------------------------
    
    SQL> create bitmap index test_index on test_table(col);
    
    Index created.
    
    SQL> EXEC dbms_stats.gather_table_stats( 'MY_SCHEMA', 'TEST_TABLE' );
    
    PL/SQL procedure successfully completed.
    
    SQL> SELECT col, COUNT(*)
      2    FROM test_table
      3    GROUP BY col
      4  /
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 238193838
    
    ---------------------------------------------------------------------------------------
    | Id  | Operation                | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
    ---------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT         |            |    10 |    30 |   286   (0)| 00:00:04 |
    |   1 |  SORT GROUP BY NOSORT    |            |    10 |    30 |   286   (0)| 00:00:04 |
    |   2 |   BITMAP CONVERSION COUNT|            |  1010K|  2961K|   286   (0)| 00:00:04 |
    |   3 |    BITMAP INDEX FULL SCAN| TEST_INDEX |       |       |            |          |
    ---------------------------------------------------------------------------------------
    
    

提交回复
热议问题