最快的MySQL运算符在索引列中检查不为0:>或!=

Con*_*ing 2 mysql

在索引列中检查非0的最快方法是什么,使用:

WHERE column != 0
Run Code Online (Sandbox Code Playgroud)

要么

WHERE column > 0
Run Code Online (Sandbox Code Playgroud)

使用索引列可以更好地完成上述工作之一,还是在速度方面它们几乎相同?

cet*_*ras 8

我在〜650k行表上做了这个,column列被索引了.似乎存在细微差别,确实> 0更快.这两个查询在两个相同的数据库上运行,因此没有缓存可能会影响执行时间.

explain
select * from table where column > 0;

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  table   range   column  column  5   \N  4736    Using where

Execution Time : 00:00:00:005

explain
select * from table where column != 0;

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  table   range   column  column  5   \N  4746    Using where

Execution Time : 00:00:00:012
Run Code Online (Sandbox Code Playgroud)