MySQL использовать сортировку индекса оптимизации optimization - PullRequest
1 голос
/ 19 марта 2012
  1. Сначала я создаю таблицу tag:

    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 |
    +----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
    

    Сортировка по индексу, без использования файловой сортировки .

  2. Когда я добавляю столбец name в tag таблицу:

    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 для сортировки.

1 Ответ

0 голосов
/ 12 сентября 2012

вы можете запустить 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 |           |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------+
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...