【发布时间】:2016-01-11 09:43:58
【问题描述】:
我创建一个这样的表:
CREATE TABLE `testtable` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL,
`updated_at` timestamp NOT NULL,
PRIMARY KEY (`id`),
KEY `test_updated_at_index` (`updated_at`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
并且 select 上的说明说它正在使用文件排序:
mysql> explain select * from testtable order by updated_at;
+----+-------------+-----------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | testtable | ALL | NULL | NULL | NULL | NULL | 1 | Using filesort |
+----+-------------+-----------+------+---------------+------+---------+------+------+----------------+
为什么不使用索引?此外,如果我只是删除 created_at 列,那么它确实使用索引!
为什么添加一个额外的列会导致mysql忘记如何使用索引?
如何更改表以便 mysql 使用索引进行排序,即使有额外的列?
【问题讨论】:
标签: mysql sql indexing query-optimization mysql-5.5