Oracle 10g - optimize WHERE IS NOT NULL

后端 未结 9 1832
长情又很酷
长情又很酷 2020-12-13 20:56

We have Oracle 10g and we need to query 1 table (no joins) and filter out rows where 1 of the columns is null. When we do this - WHERE OurColumn IS NOT NULL - we get a full

相关标签:
9条回答
  • 2020-12-13 21:23

    Create an index on that column.

    To make sure the index is used, it should be on the index and other columns in the where.

    ocdecio answered:

    If you are doing a select *, then it would make sense to do a table scan rather than using the index.

    That's not strictly true; an index will be used if there is an index that fits your where clause, and the query optimizer decides using that index would be faster than doing a table scan. If there is no index, or no suitable index, only then must a table scan be done.

    0 讨论(0)
  • 2020-12-13 21:26

    Using hints should be done only as a work around rather than a solution.

    As mentioned in other answers, the null value is not available in B-TREE indexes.

    Since you know that you have mostly null values in this column, would you be able to replace the null value by a range for instance.

    That really depends on your column and the nature of your data but typically, if your column is a date type for instance:

    where mydatecolumn is not null Can be translated in a rule saying: I want all rows which have a date.

    Then you can most definitely do this: where mydatecolumn <=sysdate (in oracle)

    This will return all rows with a date and ommit null values while taking advantage of the index on that column without using any hints.

    0 讨论(0)
  • 2020-12-13 21:31

    See http://www.oracloid.com/2006/05/using-index-for-is-null/

    If your index is on one single field, it will NOT be used. Try to add a dummy field or a constant in the index:

    create index tind on t(field_to_index, 1); 
    
    0 讨论(0)
提交回复
热议问题