I have a query like this:
select count(*) from people where text_field is not null;
It’s slow. Is it kosher to put an index on mediumtext field? What’s the best practice for this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This
is not nullquery predicate is notorious for being impossible to index. That’s because null comparisons in SQL work non-intuitively.Your best bet is to figure out some other way to find these non-empty text_field values. If this query needs to run often, you’ll need to store some kind of flag value.
For example, add an INT column called text_field_length and populate it with the length of the text_field, then select where text_field_length > 0
Edit eight years later
Newer versions of MySQL (5.7 +) and MariaDB now have computed columns. Those computed columns may be indexed.
So you may add a computed column, maybe called
text_field_not_null, to yourpeopletable, and an index on that column.Then, this query will exploit that new index.
And, the value of this new column gets handled automatically.
(MySQL has almost caught up to the pricey table servers in this respect.)