我有一个查询,正在33秒的有没有更好的方式来改写它,我怎么可以把它转换到程序需要优化MySQL查询
select ut.templateId,
(
case
when ut.reportTypeId=4 then 'Account'
when ut.reportTypeId=5 then 'Campaign'
when ut.reportTypeId=6 then 'AdGroup'
when ut.reportTypeId=7 then 'Ad'
when ut.reportTypeId=8 then 'Keyword'
end
)as ReportType ,
ur.reportId,
(case when timestampdiff(SECOND,b.createdTS,a.createdTS) < 5 then a.reportId else 0 end) as '<5secs',
(case when timestampdiff(SECOND,b.createdTS,a.createdTS) between 5 and 10 then a.reportId else 0 end) as '5-10secs',
(case when timestampdiff(SECOND,b.createdTS,a.createdTS) between 11 and 20 then a.reportId else 0 end) as '11-20secs',
(case when timestampdiff(SECOND,b.createdTS,a.createdTS) between 21 and 30 then a.reportId else 0 end) as '21-30secs',
(case when timestampdiff(SECOND,b.createdTS,a.createdTS) between 31 and 60 then a.reportId else 0 end) as '31-60secs',
(case when timestampdiff(SECOND,b.createdTS,a.createdTS) between 61 and 120 then a.reportId else 0 end) as '61-120secs',
(case when timestampdiff(SECOND,b.createdTS,a.createdTS) between 121 and 1800 then a.reportId else 0 end) as '2-30mins',
(case when timestampdiff(SECOND,b.createdTS,a.createdTS) > 1800 then a.reportId else 0 end) as '>30mins'
from
(select reportId,createdTS from T_ReportMonitor where status='EndSP')a,
(select reportId,createdTS from T_ReportMonitor where status='BeginSP')b,
(select templateId,reportTypeId,reportConsoleType from T_UserTemplate) ut,
(select reportId,templateId,createdTS,modifiedTS,isDeleted from T_UserReport) ur
where a.reportId=b.reportId
and date(ur.createdTS) = 20120731
and ut.templateId=ur.templateId
and reportConsoleType in ('Adser','APIAdser')
and ur.isDeleted=false
and a.reportId=ur.reportId
and ur.reportId!=313509 AND ur.reportId!=313510 AND ur.reportId!=313511 AND ur.reportId!=313512 AND ur.reportId!=313509 AND ur.reportId!=313510 AND ur.reportId!=313511 AND ur.reportId!=313512 AND ur.reportId!=313520;
查询的解释结果是
+----+-------------+-----------------+------+---------------+------+---------+------+--------+--------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------+------+---------------+------+---------+------+--------+--------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 20071 | |
| 1 | PRIMARY | <derived3> | ALL | NULL | NULL | NULL | NULL | 20072 | Using where; Using join buffer |
| 1 | PRIMARY | <derived5> | ALL | NULL | NULL | NULL | NULL | 148591 | Using where; Using join buffer |
| 1 | PRIMARY | <derived4> | ALL | NULL | NULL | NULL | NULL | 154030 | Using where; Using join buffer |
| 5 | DERIVED | T_UserReport | ALL | NULL | NULL | NULL | NULL | 124008 | |
| 4 | DERIVED | T_UserTemplate | ALL | NULL | NULL | NULL | NULL | 151745 | |
| 3 | DERIVED | T_ReportMonitor | ALL | NULL | NULL | NULL | NULL | 60849 | Using where |
| 2 | DERIVED | T_ReportMonitor | ALL | NULL | NULL | NULL | NULL | 60849 | Using where |
+----+-------------+-----------------+------+---------------+------+---------+------+--------+--------------------------------+
我对那些在使用的列密钥的where子句和任何其他的比较,但他们都不是在查询中使用,这是由于它们导出查询的原因。
请与发布数据的结构,使我们能够做出优化的查询使用http://sqlfiddle.com –
并解释你想得到的结果 –
不要使用子查询,而要使用连接。 – Nin