0

我有一个关于 group by 和 sum 的查询。我有近 100 万条记录。当我运行查询时,它需要 2.5 秒。如果我删除 group by 子句,则需要 0.89 秒。有什么方法可以使用 group by 和 sum 一起优化查询。

 SELECT aggEI.ei_uuid AS uuid,aggEI.companydm_id AS companyId,aggEI.rating AS       rating,aggEI.ei_name AS name,
 compdm.company_name AS companyName,sum(aggEI.count) AS activity
      FROM AGG_EXTERNALINDIVIDUAL AS aggEI
      JOIN COMPANYDM AS compdm ON aggEI.companydm_id = compdm.companydm_id
      WHERE aggEI.ei_uuid is not null
        and aggEI.companydm_id IN (8)
        and aggEI.datedm_id = 20130506 
        AND aggEI.topicgroupdm_id IN (1,2,3,4,5,6,7)
        AND aggEI.rating >= 0
        AND aggEI.rating <= 100
 GROUP BY aggEI.ei_uuid,aggEI.companydm_id
LIMIT 0,200000

解释结果如下:

  1 SIMPLE  compdm  const   PRIMARY,companydm_id_UNIQUE,comp_idx    PRIMARY 8       const   1   Using temporary; Using filesort
  1 SIMPLE  aggEI   ref       PRIMARY,datedm_id_UNIQUE,agg_ei_comdm_fk_idx,agg_ei_datedm_fk_idx,agg_ei_topgrp_fk_idx,uid_comp_ei_dt_idx,uid_comp_dt_idx,comp_idx    datedm_id_UNIQUE    4   const   197865  Using where

我也不明白为什么首先执行 compdm 表。有人可以解释吗?

我在 AGG_EXTERNALINDIVIDUAL 表上具有 ei_uuid、companydm_id、datedm_id 组合的索引。在 aggEI 表上可能的键下显示为 uid_comp_dt_idx。但是 aggEI 表以 datedmid_UNIQUE 作为键。我不明白这种行为。

有人可以解释吗?

4

1 回答 1

1

Explain 必须先运行依赖查询,然后才能运行主查询。

您需要检查 AGG_EXTERNALINDIVIDUAL 上的索引。

于 2013-05-13T17:34:59.430 回答