我有一个很大的查询,有多个连接和一个子查询。由于海量数据查询耗费了大量时间。当我试图解析查询时,我发现查询没有正确使用索引,尽管它们存在于相应的表中 对于我的一个表,Explain的Extra列显示为NULL。有人可以帮助我理解它的含义。
以下是EXPLAIN的详细信息。
+----+-------------+----------------+-------+-------------------------------------+-------------------------+---------+--------------------------------+--------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+-------+-------------------------------------+-------------------------+---------+--------------------------------+--------+--------------------------+
| 1 | PRIMARY | results | index | PRIMARY | value | 202 | NULL | 253 | Using temporary; Using filesort |
| 1 | PRIMARY | report | ref | Index_Result,Index_TimeRecord | Index_Result | 8 | SCHEMA.results.id | 6056 | NULL |
| 1 | PRIMARY | user | ref | Index_user | Index_user | 192 | func | 1 | Using index |
| 1 | PRIMARY | <derived2> | ref | <auto_key0> | <auto_key0> | 8 | SCHEMA.report.time_record.id | 10 | Using where |
| 2 | DERIVED | time_record | ref | index_monthyear | index_monthyear | 4 | const | 139200 | Using where |
+----+-------------+----------------+-------+-------------------------------------+-------------------------+---------+--------------------------------+--------+--------------------------+
5 rows in set (0.09 sec)
以下是查询:
SELECT `currentmonth`.`year` AS `c0`, `currentmonth`.`monthdisplay` AS `c1`, `currentmonth`.`month` AS `c2`, `currentmonth`.`day` AS `c3`,
`user`.`userid` AS `c4`, `user`.`name` AS `c5`, `results`.`id` AS `c6`, `results`.`name` AS `c7`
FROM
(SELECT * FROM time_record WHERE
`month`=DATE_FORMAT(@curr_date:=CONVERT_TZ(UTC_TIMESTAMP(),'GMT',olsentimezone),'%m') - 0 AND
`year`=DATE_FORMAT(@curr_date,'%Y'))
AS `currentmonth`, `report` AS `report`, `user` AS `user`,
`results` AS `results` WHERE `report`.`timeid` = `currentmonth`.`id`
AND `report`.`userid` = `user`.`userid` AND
`report`.`resultid` = `results`.`id`
GROUP BY
`currentmonth`.`year`, `currentmonth`.`monthdisplay`,
`currentmonth`.`month`, `currentmonth`.`day`, `user`.`userid`, `user`.`name`, `results`.`id`,
`results`.`name`
ORDER BY
ISNULL(`currentmonth`.`year`) ASC, `currentmonth`.`year` ASC, ISNULL(`currentmonth`.`month`) ASC,
`currentmonth`.`month` ASC, ISNULL(`currentmonth`.`day`) ASC, `currentmonth`.`day` ASC, ISNULL(`user`.`userid`) ASC, `user`.`userid` ASC,
ISNULL(`results`.`id`) ASC, `results`.`id` ASC;