我对这个查询和索引有疑问
我的查询是:
EXPLAIN SELECT * FROM s1_training where amt > 0 LIMIT 500
我索引了(amt)但是如果我有这个地方它不会受到影响:
WHERE amt != 0
WHERE amt > 0
WHERE amt < 0
结果是:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE s1_training ALL amt NULL NULL NULL 64 Using where
使用索引的唯一方法是设置
WHERE amt = number ( like amt = 2 )
结果会是
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE s1_training ref amt amt 4 const 1
在此先感谢您的帮助
最好的问候。
最佳答案
这取决于行数和值的基数。
例如,如果大多数值为 >0
,那么 mysql 优化器将决定进行表扫描,因为速度更快。
对于第二个条件 WHERE amt=2
这可能会返回少得多的值,因此使用索引是因为速度更快。
关于mysql表索引不影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27764111/