我正在尝试使用索引来改善我正在处理的应用的性能。
当我对结果应用限制时,我的索引似乎不起作用。
我创建的表上有一个简单的索引
create table InboundSource (saddress int unsigned, day date, eventname char(80), count int unsigned);
create index InboundSourceDay on InboundSource (day);
如果我对表进行查询,则索引会显着加快。
mysql> select day from InboundSource group by day desc;
...
145 rows in set (0.22 sec)
如果我对该查询设置了限制,它会降低到通常的速度。
mysql> select day from InboundSource group by day desc limit 10;
+------------+
| day |
+------------+
| 2015-01-13 |
| 2015-01-12 |
| 2015-01-11 |
| 2015-01-10 |
| 2015-01-09 |
| 2015-01-08 |
| 2015-01-07 |
| 2015-01-06 |
| 2015-01-05 |
| 2015-01-04 |
+------------+
10 rows in set (5.98 sec)
但是,如果我对派生的表进行查询,那么对 结果进行限制,它比直接使用限制查询要快得多
mysql> select * from (select day from InboundSource use index (InboundSourceDay) group by day desc) as A limit 10;
+------------+
| day |
+------------+
| 2015-01-13 |
| 2015-01-12 |
| 2015-01-11 |
| 2015-01-10 |
| 2015-01-09 |
| 2015-01-08 |
| 2015-01-07 |
| 2015-01-06 |
| 2015-01-05 |
| 2015-01-04 |
+------------+
10 rows in set (0.01 sec)
每个查询的EXPLAIN:
mysql> explain select * from (select day from InboundSource group by day desc) as A limit 10;
+----+-------------+---------------+-------+---------------+------------------+---------+------+------+-----------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+-------+---------------+------------------+---------+------+------+-----------------------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 145 | |
| 2 | DERIVED | InboundSource | range | NULL | InboundSourceDay | 4 | NULL | 146 | Using index for group-by; Using temporary; Using filesort |
+----+-------------+---------------+-------+---------------+------------------+---------+------+------+-----------------------------------------------------------+
2 rows in set (0.01 sec)
mysql> explain select day from InboundSource group by day desc limit 10;
+----+-------------+---------------+-------+---------------+------------------+---------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+-------+---------------+------------------+---------+------+----------+-------------+
| 1 | SIMPLE | InboundSource | index | NULL | InboundSourceDay | 4 | NULL | 23893889 | Using index |
+----+-------------+---------------+-------+---------------+------------------+---------+------+----------+-------------+
1 row in set (0.00 sec)
mysql> explain select day from InboundSource group by day desc;
+----+-------------+---------------+-------+---------------+------------------+---------+------+------+-----------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+-------+---------------+------------------+---------+------+------+-----------------------------------------------------------+
| 1 | SIMPLE | InboundSource | range | NULL | InboundSourceDay | 4 | NULL | 146 | Using index for group-by; Using temporary; Using filesort |
+----+-------------+---------------+-------+---------------+------------------+---------+------+------+-----------------------------------------------------------+
1 row in set (0.00 sec)
mysql>