MySql中不等号的一点疑问:从为什么不等号有

2025-03-22 22:02:00
推荐回答(1个)
回答1:

看表结构:

[sql] view plain copy

  • mysql> show create table s;  

  • +-------+----------------------------------------------------------------------------------------------------------------------------------------+  

  • | Table | Create Table                                                                                                                           |  

  • +-------+----------------------------------------------------------------------------------------------------------------------------------------+  

  • | s     | CREATE TABLE `s` (  

  • `id` int(11) DEFAULT NULL,  

  • `score` int(11) DEFAULT NULL,  

  • KEY `id` (`id`)  

  • ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |  

  • +-------+----------------------------------------------------------------------------------------------------------------------------------------+  

  • 1 row in set (0.00 sec)  


  • 看查询:
  • [sql] view plain copy

  • mysql> explain select count(*) from s where id<>2;  

  • +----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+  

  • | id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra                    |  

  • +----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+  

  • |  1 | SIMPLE      | s     | index | id            | id   | 5       | NULL |    3 | Using where; Using index |  

  • +----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+  

  • 1 row in set (0.00 sec)  


  • 在这个查询中,我们查询的是id不等于2的count(*),结果explain显示用到了index。
  • 根据以前的知识,我们知道,数据库中使用不等号是不会用到索引的。那么这里是怎么回事呢?

    下面是我个人的理解。

    这里涉及到了一个“覆盖索引”的问题。

    先说一下什么是覆盖索引呢?其实这不是一个特定结构的索引。只是说,如果我们要查询的东西,能够直接从索引上得到,而不用再去表中取数据,那么这个使用的索引就是覆盖索引。

    回到我们的问题。select count(*) from s where id<>2;由于id列上有索引,而这个查询在索引上完全能够做到(查找索引上id不是2的即可)。所以这里是利用了覆盖索引的思想。

    ------------------------------------------------

    上面的 查询是用了索引的,再看下面这个:

    [sql] view plain copy

  • mysql> explain select count(score) from s where id<>2;  

  • +----+-------------+-------+------+---------------+------+---------+------+------+-------------+  

  • | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |  

  • +----+-------------+-------+------+---------------+------+---------+------+------+-------------+  

  • |  1 | SIMPLE      | s     | ALL  | id            | NULL | NULL    | NULL |    3 | Using where |  

  • +----+-------------+-------+------+---------------+------+---------+------+------+-------------+  

  • 1 row in set (0.01 sec)  


  • 由于score不在索引上,所以这里用不到覆盖索引。那么Extra列自然也不会有using index 了。

  • -------------------------------------------------

    不等号是这个道理。like关键字也是这个道理。

    使用like %%的时候,也会遇到即使是%在开头,也会有using index的场景。那也是用到了覆盖索引的思想。

    在《高性能mysql》中,page121页第二段也提到了,extra中,出现了Using index是指用到了覆盖索引。

    -----------------------------------------------------