Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MySQL中为具有限制的groupby语句的表应用索引?_Mysql_Sql_Indexing - Fatal编程技术网

如何在MySQL中为具有限制的groupby语句的表应用索引?

如何在MySQL中为具有限制的groupby语句的表应用索引?,mysql,sql,indexing,Mysql,Sql,Indexing,我正在尝试使用索引来提高我正在开发的应用程序的性能 当我对结果施加限制时,我的索引似乎不起作用 我在表上创建了一个简单的索引 create table InboundSource (saddress int unsigned, day date, eventname char(80), count int unsigned); create index InboundSourceDay on InboundSource (day); 如果我对表进行查询,索引会大大加快查询速度 mysql>

我正在尝试使用索引来提高我正在开发的应用程序的性能

当我对结果施加限制时,我的索引似乎不起作用

我在表上创建了一个简单的索引

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)
解释每个查询:

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> 

为每一个问题发布您的解释。您是否尝试在第二个问题之后再次运行第一个问题?执行时间是多少?重复查询在时间上没有区别。解释似乎表明它没有为group by使用索引,但我的SQL fu不够强大,无法理解原因。您没有使用临时表,而是使用派生表并强制使用索引。在这种情况下,您的表统计信息可能已过期,数据插入到每日通宵cron上,并且索引是在几小时前生成的。我将更正我的问题:派生表,谢谢。