Does mysql 5.0 index null values?

2019-06-15 16:24发布

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?

4条回答
来,给爷笑一个
2楼-- · 2019-06-15 16:34

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.

查看更多
Ridiculous、
3楼-- · 2019-06-15 16:44

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...

查看更多
Fickle 薄情
4楼-- · 2019-06-15 16:46

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

查看更多
【Aperson】
5楼-- · 2019-06-15 16:49

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.

查看更多
登录 后发表回答