简体   繁体   中英

MYSQL using filesort when ORDER BY indexed column in simple query

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 | Column_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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM