0

以下是我的聚合查询,需要 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 表使用“使用临时,使用文件排序”

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

4

2 回答 2

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
于 2014-02-06T14:13:14.587 回答
0

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

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

于 2014-02-06T13:49:09.430 回答