Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index for nullable column

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 see a FULL TABLE SCAN (even a hint didn't help)

SELECT e.ename 
FROM   emp e
WHERE  e.ename = 'gdoron';

Does use the index...

I googled and found out there are no null entries in indexes, thus the first query can't use the index.

My question is simple: why there aren't null entries in indexes?

like image 245
gdoron is supporting Monica Avatar asked Feb 07 '12 11:02

gdoron is supporting Monica


People also ask

Can a NULLable column be indexed?

By default, relational databases ignore NULL values (because the relational model says that NULL means "not present"). So, Index does not store NULL value, consequently if you have null condition in SQL statement, related index is ignored (by default).

Can we create index for NULL values?

To get around the optimization of SQL queries that choose NULL column values, we can create a function-based index using the null value built-in SQL function to index only on the NULL columns.

Can we create index on NULLable column in MySQL?

MySQL can perform the same optimization on col_name IS NULL that it can use for col_name = constant_value . For example, MySQL can use indexes and ranges to search for NULL with IS NULL .

Can index column have NULL values?

The Oracle database does not include rows in an index if all indexed columns are NULL . That means that every index is a partial index—like having a where clause: CREATE INDEX idx ON tbl (A, B, C, ...)


Video Answer


2 Answers

By default, relational databases ignore NULL values (because the relational model says that NULL means "not present"). So, Index does not store NULL value, consequently if you have null condition in SQL statement, related index is ignored (by default).

But you can suprass this problem, check THIS or THIS article.

like image 188
aF. Avatar answered Sep 21 '22 09:09

aF.


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.

like image 24
Allan Avatar answered Sep 17 '22 09:09

Allan