Does mysql 5.0 index null values?

2019-06-15 15:50发布

问题:

I realize that the exact question has been asked before on stack overflow with only a vaguely conclusive answer: link

So here it is again.

Table A: Has an integer column which is nullable, along with an index. Query:

SELECT * 
FROM table 
WHERE column IS NULL 
LIMIT 10;

Will an index be used, or will mysql do a full table scan until it has found 10 entries?

回答1:

i think this should be helpful:

http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html

A search using col_name IS NULL employs indexes if col_name is indexed.



回答2:

The only way to know that for sure in your specific case is to run "EXPLAIN SELECT" on your query.

It really depends on the number of NULLs in your table compared with total number of rows; whether table statistics have been updated, etc...



回答3:

You'd have to analyze the query. In general, it depends on the relative percentage of NULLs to values. If a majority of the entries in that integer column is NULL, that it will likely skip it.

On the other hand, if only 10% (for example) are NULL, it will (or should) use the index.



回答4:

There is one simple way to know it.

You can execute this command show index from tablename and it will return some analysis results which one is called NULL.

if NULL equals YES, your index contains NULL

http://dev.mysql.com/doc/refman/5.1/en/show-index.html