2012-03-19 43 views
1
  1. 首先,我创建表tagMySQL的使用索引优化排序

    CREATE TABLE `tag` (
        `id` smallint(6) NOT NULL AUTO_INCREMENT, 
        `total` int(11) DEFAULT NULL, 
        `total_question` int(11) DEFAULT NULL, 
        PRIMARY KEY (`id`), 
        KEY `idx_sort` (`total`,`total_question`) USING BTREE 
    ) ENGINE=InnoDB DEFAULT CHARSET=gb2312; 
    

    的mysql>explain select * from tag order by total;

    +----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+ 
    | id | select_type | table | type | possible_keys | key  | key_len | ref | rows | Extra  | 
    +----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+ 
    | 1 | SIMPLE  | tag | index | NULL   | idx_sort | 10  | NULL | 1 | Using index | 
    +----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+ 
    

    排序使用索引,而不是使用filesort

  2. 当我添加列nametag表:

    CREATE TABLE `tag` (
        `id` smallint(6) NOT NULL AUTO_INCREMENT, 
        `total` int(11) DEFAULT NULL, 
        `total_question` int(11) DEFAULT NULL, 
        `name` char(20) DEFAULT NULL, 
        PRIMARY KEY (`id`), 
        KEY `idx_sort` (`total`,`total_question`) USING BTREE 
    ) ENGINE=InnoDB DEFAULT CHARSET=gb2312; 
    

    的MySQL>explain select * from tag order by total;

    +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ 
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra   | 
    +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ 
    | 1 | SIMPLE  | tag | ALL | NULL   | NULL | NULL | NULL | 1 | Using filesort | 
    +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ 
    

    排序使用文件排序,不使用索引

  3. 当我创建的索引只total

    CREATE TABLE `tag` (
        `id` smallint(6) NOT NULL AUTO_INCREMENT, 
        `total` int(11) DEFAULT NULL, 
        `total_question` int(11) DEFAULT NULL, 
        `name` char(20) DEFAULT NULL, 
        PRIMARY KEY (`id`), 
        KEY `idx_sort` (`total`) USING BTREE 
    ) ENGINE=InnoDB DEFAULT CHARSET=gb2312; 
    

    的mysql>explain select * from tag order by total;

    +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ 
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra   | 
    +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ 
    | 1 | SIMPLE  | tag | ALL | NULL   | NULL | NULL | NULL | 1 | Using filesort | 
    +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ 
    

    排序使用文件排序!为什么?我只使用total列进行排序。

+0

因为选择'*' - '*'就是全部,而指数里只含有总的值,因为,你是只索引总列,所以,如果你正在做'选择从标签顺序总数'< - 这将不会触发filesort – ajreal 2012-03-19 11:19:38

+0

我认为你不能如果你想在单个查询中有所有列返回 – ajreal 2012-03-19 11:49:15

+0

什么如果你没有选择最后一个版本表中的所有列,会发生什么?从标签顺序总数中选择id,total,total_question; – 2012-03-19 12:13:48

回答

0

可以运行desc select * from tag force index (idx_sort) order by total ; 你可以看到输出:

mysql> desc select * from tag force index (idx_sort) order by total ; 
+----+-------------+-------+-------+---------------+----------+---------+------+------+---  ----+ 
| id | select_type | table | type | possible_keys | key  | key_len | ref | rows | Extra | 
+----+-------------+-------+-------+---------------+----------+---------+------+------+--- ----+ 
| 1 | SIMPLE  | tag | index | NULL   | idx_sort | 5  | NULL | 1 |   | 
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------+