开发者

MYSQL using filesort when ORDER BY indexed column in simple query

开发者 https://www.devze.com 2023-04-01 02:16 出处:网络
I\'ve seen a lot of similar questions, but I\'m not seeing a solution that will work for me. Or maybe I\'m just being dense. :) Hopefully someone can help me out.

I've seen a lot of similar questions, but I'm not seeing a solution that will work for me. Or maybe I'm just being dense. :) Hopefully someone can help me out.

I have the following table:

CREATE TABLE `table_name` (
  `value1` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `value2` varchar(50) NOT NULL,
  `value3` tinytext,
  `value4` tinytext,
  `value5` tinytext,
  `value6` char(3) DEFAULT NULL,
  `value7` char(3) DEFAULT NULL,
  PRIMARY KEY (`value1`),
  KEY value2_index ('value2')
) ENGINE=MyISAM AUTO_INCREMENT=46 DEFAULT CHARSET=latin1;

To verify my indexes were set, this is the SHOW INDEX result:

+--------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table        | Non_unique | Key_name       | Seq_in_index | Colu开发者_如何学Gomn_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| table_name   |          0 | PRIMARY        |            1 | value1      | A         |          43 |     NULL | NULL   |      | BTREE      |         |
| table_name   |          1 | value2_index   |            1 | value2      | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |
+--------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
2 rows in set (0.00 sec)

Where I'm running this query:

SELECT value2, value6
FROM table_name
WHERE value7 = 'Yes'
ORDER BY value2;

I thought by adding an index on value2, it would stop the query from using filesort. However, the EXPLAIN shows differently:

+----+-------------+--------------+------+---------------+------+---------+------+------+-----------------------------+
| id | select_type | table        | type | possible_keys | key  | key_len | ref  | rows | Extra                       |
+----+-------------+--------------+------+---------------+------+---------+------+------+-----------------------------+
|  1 | SIMPLE      | table_name   | ALL  | NULL          | NULL | NULL    | NULL |   43 | Using where; Using filesort |
+----+-------------+--------------+------+---------------+------+---------+------+------+-----------------------------+
1 row in set (0.00 sec)

What am I doing wrong?

Thanks!


The query first needs to find rows with value7 = 'Yes', meaning your index must include value7 as the first column, to be used. For those rows that match, it needs to order by value2. So that query needs a multi-column index on (value7, value2).

You can read more about indexes in the MySQL docs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号