I have a very big table with a column that has mostly null values.
I query this table a lot with x_column is not null
condition.
I wonder how I should index it and what is the best way to improve performance here because I read in several places that is not null
does not even use an index. So what can I do to use an index here?
See this or this for example, but there are more. I do not want to add another field (computed field) to solve it. Is there another, maybe newer way?
(I’m using innoDB)
2
Answers
What you read is not accurate.
https://dev.mysql.com/doc/refman/8.4/en/is-null-optimization.html says:
Likewise,
IS NOT NULL
is optimized likecol_name <> constant_value
. I.e. it’s a range access.Demo:
I tested MySQL 8.4.2, but this should apply to MySQL 8.0 and 5.x as well.
Maybe the references you read that said
is not null
doesn’t use an index are based on some version of MySQL from 20 years ago, but all currently supported versions do use an index in this case.x_column is not null
is similar to testingx_column is > 123
— it is a "range" test. Then the "range test" would probably decide that the index is not worth using. That is, the index might be used if very few of the rows have a non-null value.