简体   繁体   中英

What index should I use for IS NOT NULL queries

The following is my table schema :-

+---------+---------+----------+
|   ID    |   COL1  |   COL2   |
+---------+---------+----------+
|    1    |   NULL  |    1     |
|    2    |    1    |   NULL   |
|    3    |   NULL  |   NULL   |
+---------+---------+----------+

Now, I have to run queries like :-

SELECT * FROM `TABLE` WHERE `COL1` IS NOT NULL AND `COL2` IS NULL

AND

SELECT * FROM `TABLE` WHERE `COL2` IS NOT NULL AND `COL1` IS NULL

No matter what index I use, SELECT * FROM TABLE WHERE ANY_COL IS NOT NULL does not take advantage of the index. What do I do now to take advantage of indexes?

These queries should take advantage of multi-column indexes: table(col2, col1) for the first one and table(col1, col2) for the second one.

The reason is not null would not use an index is because most values are -- presumably -- not null. That means that the query engine sees no advantage in using the index to get the data, versus just reading all the records and doing the filter after reading.

Also note that index usage on small tables is not representative of what happens on larger tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM