简体   繁体   中英

Why this simple query not using any one index?

Query:

SELECT *, history_count as `count` 
FROM pdf_history  
WHERE 1  AND history_date>=1426180929  AND history_count!=0

EXPLAIN

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra 

1   SIMPLE  pdf_history ALL history_date,history_count  NULL    NULL    NULL    697 Using where

One of the reason optimizer choose not to use the index is when the filter doesn't reduce the search space.

for example if

history_date>=1426180929

or

history_count!=0

already bring all the records then using the index doesnt really help.

My suggestion do this both querys and check the ANALYZE to see how many of the 600 records are match that filter

SELECT count(*) FROM pdf_history WHERE history_date>=1426180929;
SELECT count(*) FROM pdf_history WHERE history_count!=0;

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