优化聚合查询

时间:2013-05-27 17:30:04

标签: mysql

以下是我的聚合查询,需要1.1秒。此查询具有多个连接。列将优化查询的索引。

         EXPLAIN SELECT straight_join
          aggsm.tdm_id AS topid,              
          sum(aggsm.m_count) AS mencnt ,
          sum(aggsm.ps_count) AS pscnt,
          sum(aggsm.ns_count) AS ngscnt,
          topdm.topic_name AS topname
         FROM AGG_MENTION AS aggsm 
         JOIN TOPICDM AS topdm  ON aggsm.topicdm_id = topdm.topicdm_id
         JOIN LOCATIONDM AS locdm ON aggsm.locationdm_id = locdm.locationdm_id
         JOIN CITY AS citydm ON locdm.city_id = citydm.city_id
         JOIN STATE AS statedm ON citydm.state_id = statedm.state_id
         WHERE aggsm.cdm_id = 11
         AND aggsm.ei_type IN (1,2,3,4)
         AND aggsm.datedm_id BETWEEN 20130101 AND 20130522   
         AND statedm.country_id IN (1,2,3,4) 
         AND topdm.topic_group_id IN (1,2,3,4,5,6,7)    
         GROUP BY aggsm.topicdm_id
         -- ORDER BY aggsm.topicdm_id DESC,sum(aggsm.m_count) DESC
         LIMIT 0,200000

以下是解释输出:

   1    SIMPLE  aggsm   ref PRIMARY,datedm_id_UNIQUE,agg_sm_locdm_fk_idx,agg_sm_comdm_fk_idx,agg_sm_topdm_fk_idx,agg_sm_datedm_fk_idx,agg_em_indtype_fk_idx,comp_top_dt,l_idx   comp_top_dt 8   const   202129  Using where; Using index
   1    SIMPLE  topdm   eq_ref  PRIMARY,topicdm_id_UNIQUE,topdm_grp_id_idx,id_idx   PRIMARY 8   opinionleaders.aggsm.topicdm_id 1   Using where
   1    SIMPLE  locdm   eq_ref  PRIMARY,city_id_UNIQUE,locationdm_id_UNIQUE,loc_city_fk_idx,id_idx  PRIMARY 8   opinionleaders.aggsm.locationdm_id  1   
   1    SIMPLE  citydm  eq_ref  PRIMARY,city_id_UNIQUE,city_state_fk_idx,id_idx PRIMARY 8   opinionleaders.locdm.city_id    1   
   1    SIMPLE  statedm eq_ref  PRIMARY,state_id_UNIQUE,state_country_fk_idx,id_idx PRIMARY 8   opinionleaders.citydm.state_id  1   Using where

取消注释order by子句将导致aggsm表使用'using temporary,using filesort'

我们如何优化查询或定义索引

2 个答案:

答案 0 :(得分:0)

1)LIMIT 0,200000绝对不是一个好的解决方案,我几乎看不到你需要如此大量数据的应用程序,将其拆分成块

2)在ORDER BY子句中,您指定并求和(aggsm.m_count) - 这是一个聚合函数,尝试将其作为mencnt(因此ORDER BY aggsm.topicdm_id DESC,mencnt DESC),不确定SQL服务器是否理解它

答案 1 :(得分:0)

稍微重新格式化查询的可读性,并查看关系和相关列以进行索引考虑。由于你的Aggsm表是按topicdm_id分组的,我至少会尝试用这个和列来获取覆盖索引到达你的连接表,这样它就不需要获取原始页面数据,直到它有一个合格的记录跟...共事。另外,包括where子句的组件。我会建议表上的索引

table        index
agg_mention  ( cdm_id, ei_type, datedm_id, topicdm_id, locationdm_id )
topicdm      ( topicdm_id, topic_group_id )
state        ( state_id, country_id )
locationdm   ( locationdm_id )
city         ( city_id )

SELECT straight_join
      aggsm.tdm_id AS topid,
      sum(aggsm.m_count) AS mencnt,
      sum(aggsm.ps_count) AS pscnt,
      sum(aggsm.ns_count) AS ngscnt,
      topdm.topic_name AS topname
   FROM 
      AGG_MENTION AS aggsm 
         JOIN TOPICDM AS topdm  
            ON aggsm.topicdm_id = topdm.topicdm_id
            AND topdm.topic_group_id IN (1,2,3,4,5,6,7)    
         JOIN LOCATIONDM AS locdm 
            ON aggsm.locationdm_id = locdm.locationdm_id
            JOIN CITY AS citydm 
               ON locdm.city_id = citydm.city_id
               JOIN STATE AS statedm 
                  ON citydm.state_id = statedm.state_id
                  AND statedm.country_id IN (1,2,3,4) 
   WHERE 
          aggsm.cdm_id = 11
      AND aggsm.ei_type IN (1,2,3,4)
      AND aggsm.datedm_id BETWEEN 20130101 AND 20130522   
   GROUP BY 
      aggsm.topicdm_id
   ORDER BY 
      aggsm.topicdm_id DESC,
      sum(aggsm.m_count) DESC
   LIMIT 
      0,200000