Index for nullable column

前端 未结 3 526
攒了一身酷
攒了一身酷 2020-12-15 22:52

I have an index on a nullable column and I want to select all it\'s values like this:

SELECT e.ename 
FROM   emp e;

In the explain plan I s

3条回答
  •  無奈伤痛
    2020-12-15 23:14

    If you're getting all of the rows from the table, why do you think it should use the index? A full table scan is the most efficient means to return all of the values. It has nothing to do with the nulls not being in the index and everything to do with the optimizer choosing the most efficient means of retrieving the data.


    @A.B.Cade: It's possible that the optimizer could choose to use the index, but not likely. Let's say you've got a table with an indexed table with 100 rows, but only 10 values. If the optimizer uses the index, it has to get the 10 rows from the index, then expand it to 100 rows, whereas, with the full-table scan, it gets all 100 rows from the get-go. Here's an example:

    create table test1 (blarg varchar2(10));
    
    create index ak_test1 on test1 (blarg);
    
    insert into test1
    select floor(level/10) from dual connect by level<=100;
    
    exec dbms_stats.gather_table_stats('testschema','test1');
    
    exec dbms_stats.gather_index_stats('testschema','ak_test1');
    
    EXPLAIN PLAN FOR
    select * from test1;
    

    My point is largely that this question is based largely on a flawed premise: that index-scans are intrinsically better that full-table scans. That is not always true, as this scenario demonstrates.

提交回复
热议问题