Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have had the chance to play around with this, and my previous comments regarding the NOT IN are a red herring in this case. The key thing is the presence of NULLs, or rather whether the indexed columns have NOT NULL constraints enforced.</p> <p>This is going to depend on the version of the database you're using, because the optimizer gets smarter with each release. I'm using 11gR1 and the optimizer used the index in all cases except one: when both columns were null and I didn't include the <code>NOT IN</code> clause:</p> <pre><code>SQL&gt; desc big_table Name Null? Type ----------------------------------- ------ ------------------- ID NUMBER COL1 NUMBER COL2 VARCHAR2(30 CHAR) COL3 DATE COL4 NUMBER </code></pre> <p>Without the NOT IN clause...</p> <pre><code>SQL&gt; explain plan for 2 select col4, count(col1) from big_table 3 group by col4 4 / Explained. SQL&gt; select * from table(dbms_xplan.display) 2 / PLAN_TABLE_OUTPUT --------------------------------------------------------------------------------------- Plan hash value: 1753714399 ---------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | ---------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 31964 | 280K| | 7574 (2)| 00:01:31 | | 1 | HASH GROUP BY | | 31964 | 280K| 45M| 7574 (2)| 00:01:31 | | 2 | TABLE ACCESS FULL| BIG_TABLE | 2340K| 20M| | 4284 (1)| 00:00:52 | ---------------------------------------------------------------------------------------- 9 rows selected. SQL&gt; </code></pre> <p>When I dobbed the <code>NOT IN</code> clause back in, the optimizer opted to use the index. Weird.</p> <pre><code>SQL&gt; explain plan for 2 select col4, count(col1) from big_table 3 where col1 not in (12, 19) 4 group by col4 5 / Explained. SQL&gt; select * from table(dbms_xplan.display) 2 / PLAN_TABLE_OUTPUT --------------------------------------------------------------------------------------- Plan hash value: 343952376 ---------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | ---------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 31964 | 280K| | 5057 (3)| 00:01:01 | | 1 | HASH GROUP BY | | 31964 | 280K| 45M| 5057 (3)| 00:01:01 | |* 2 | INDEX FAST FULL SCAN| BIG_I2 | 2340K| 20M| | 1767 (2)| 00:00:22 | ---------------------------------------------------------------------------------------- Predicate Information (identified by operation id): PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------- 2 - filter("COL1"&lt;&gt;12 AND "COL1"&lt;&gt;19) 14 rows selected. SQL&gt; </code></pre> <p>Just to repeat, in all other cases, as long as one of the indexed columns was declared not nill, the index was used to satisfy the query. This may not be true on earlier versions of Oracle, but it probably points the way forward.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload