这个效率是很好的,你也可以explain看看查询计划如果没问题,type应该是const

解决方案 »

  1.   

    explain,的确用到了这个索引,但是用索引的方式是“range”
      

  2.   


    type 不是 const,而是 range。貌似 range 性能不是最好的。
      

  3.   

    贴出 show index from ...
    explain ...
    以供分析。
      

  4.   

    我发现,当select索引中包含的列,或者主键时,会使用 到索引。但当查询“非索引列且非主键列”时,explain显示会进行全表扫描。CREATE TABLE `index_test` (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `fn` varchar(16) CHARACTER SET latin1 NOT NULL,
      `ln` varchar(16) CHARACTER SET latin1 NOT NULL,
      `bd` date NOT NULL,
      `fn_crc` int(10) unsigned NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`),
      KEY `fn_ln_bd` (`fn`,`ln`,`bd`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
    -------------------------------------------------------------------------------------------------------------------
    mysql> explain select id from index_test where fn = 'xx' and ln in ('aa', 'bb', 'cc') and bd = '2012-12-23';
    +----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
    | id | select_type | table      | type | possible_keys | key      | key_len | ref   | rows | Extra                    |
    +----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
    |  1 | SIMPLE      | index_test | ref  | fn_ln_bd      | fn_ln_bd | 18      | const |    3 | Using where; Using index |
    +----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
    1 row in set (0.00 sec)mysql> explain select * from index_test where fn = 'xx' and ln in ('aa', 'bb', 'cc') and bd = '2012-12-23';
    +----+-------------+------------+------+---------------+------+---------+------+------+-------------+
    | id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra       |
    +----+-------------+------------+------+---------------+------+---------+------+------+-------------+
    |  1 | SIMPLE      | index_test | ALL  | fn_ln_bd      | NULL | NULL    | NULL |    1 | Using where |
    +----+-------------+------------+------+---------------+------+---------+------+------+-------------+
    1 row in set (0.00 sec)mysql> explain select id, fn, ln, bd from index_test where fn = 'xx' and ln in ('aa', 'bb', 'cc') and bd = '2012-12-23';
    +----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
    | id | select_type | table      | type | possible_keys | key      | key_len | ref   | rows | Extra                    |
    +----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
    |  1 | SIMPLE      | index_test | ref  | fn_ln_bd      | fn_ln_bd | 18      | const |    3 | Using where; Using index |
    +----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
    1 row in set (0.00 sec)mysql> explain select fn_crc from index_test where fn = 'xx' and ln in ('aa', 'bb', 'cc') and bd = '2012-12-23';
    +----+-------------+------------+------+---------------+------+---------+------+------+-------------+
    | id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra       |
    +----+-------------+------------+------+---------------+------+---------+------+------+-------------+

      

  5.   

    贴出你的 show index from index_test;
    怀疑你的表中可能只有几条数据。
      

  6.   

    的确是的!!!
    请问这是mysql的一个什么feature啊?