SQL/Oracle: when indexes on multiple columns can be used

后端 未结 3 1924
日久生厌
日久生厌 2021-02-02 13:00

If I create an index on columns (A, B, C), in that order, my understanding is that the database will be able to use it even if I search only on (A), or (A and B), or (A and B an

3条回答
  •  温柔的废话
    2021-02-02 13:35

    That is not correct. Always best to come up with a test case that represents your data and see for yourself. If you want to really understand the Oracle SQL Optimizer google Jonathan Lewis, read his books, read his blog, check out his website, the guy is amazing, and he always generates test cases.

    create table mytab nologging as (
    select mod(rownum, 3) x, rownum  y, mod(rownum, 3) z from all_objects, (select 'x' from user_tables where rownum < 4)
    );
    
    create index i on mytab (x, y, z);
    
    exec dbms_stats.gather_table_stats(ownname=>'DBADMIN',tabname=>'MYTAB', cascade=>true);
    
    set autot trace exp
    
    select * from mytab where y=5000;
    
    Execution Plan
    ----------------------------------------------------------
       0      SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=1 Bytes=10)
       1    0   INDEX (SKIP SCAN) OF 'I' (INDEX) (Cost=1 Card=1 Bytes=10)
    

提交回复
热议问题