如何提高此查询的性能?

时间:2011-06-10 09:36:03

标签: mysql

SELECT
DATE(DTM) AS 'Dia',
ROUND(AVG(TMP)-10) AS 'Graus Dia',
(SELECT 
ROUND(AVG(TMP),1) 
FROM dados_meteo 
WHERE HOUR(DTM)>=18 AND HOUR(DTM)<=22 AND DATE(DTM)=Dia/* AND POM = '[VARIABLE]'*/) AS 'Temp. Crepuscular'
FROM dados_meteo
/*WHERE POM = '[VARIABLE]'*/
GROUP BY DATE(DTM)

表的结构是:

POM Varchar 
DTM datetime 
RNF double 
WET double 
HMD double 
TMP double 
DEW double 
INF int

explain select ...的输出是:

1 PRIMARY dados_meteo ALL NULL NULL NULL NULL 18684 Using where; Using temporary; Using filesort 
2 DEPENDENT SUBQUERY dados_meteo ALL NULL NULL NULL NULL 18684 Using where 

2 个答案:

答案 0 :(得分:1)

在日期和时间部分中分割dtm

datem date
timem time

SELECT
  datem AS 'Dia'
  ,ROUND(AVG(TMP)-10) AS 'Graus Dia'
  ,(SELECT ROUND(AVG(TMP),1) 
     FROM dados_meteo 
     WHERE timem BETWEEN maketime(18,0,0) AND maketime(22,0,0) 
     AND datem = Dia) AS 'Temp. Crepuscular'
FROM dados_meteo
GROUP BY datem

确保您使用timemdatem

上的索引

答案 1 :(得分:-1)

至少为DTM创建一个索引。