MySQL查询坚持使用州"发送数据"

时间:2013-08-20 18:40:25

标签: mysql

我有跟随MySQL查询,该查询与状态“发送数据”相关并且从过去14小时开始运行。查询有3个部分。第一部分是表AGG_EI上的自联接,它为最大日期提取uuid,cmp_id,lTypeId的DISTINCT组合。第二部分从表CUL中获取uuid,cmp_id,lType_id,date。第一和第二部分连接并存储在临时表中。第三部分从临时表中选择数据并存储在CUL表中。 AGG_EI表有450万条记录(重复的uuid,cmp_id,lTypeId,与查询中未使用的其他列的日期组合)和CUL有70万条记录(uuid,cmp_id,lTypeId,日期的唯一组合)

  CREATE TEMPORARY TABLE IF NOT EXISTS temp_lt 
  SELECT cLType.uuid AS uuid, 
    cLType.cmp_id AS cmp_id, 
    cLType.lTypeId AS lTypeId, 
    (CASE WHEN cLType.lTypeId = eLType.lTypeId THEN eLType.lFrom 
             WHEN eLType.lFrom IS NULL THEN DATE_FORMAT(now(),'%Y%m%d') ELSE cLType.lFrom END) AS lFrom 
    FROM
         (
          SELECT DISTINCT d1.ei_uuid AS uuid, 
                          d1.cmp_id AS cmp_id,
                          d1.ei_type AS lTypeId,
                          d1.datedm_id AS lFrom
                          FROM AGG_EI d1 
                              LEFT OUTER JOIN AGG_EI d2 
                                              ON (d1.ei_uuid = d2.ei_uuid AND d1.cmp_id = d2.cmp_id AND d1.datedm_id < d2.datedm_id ) 
                          WHERE d2.ei_uuid IS NULL AND d2.cmp_id IS NULL
          ) AS cLType
          LEFT OUTER JOIN 
          (
            SELECT uuid AS uuid, 
                   cmp_id AS cmp_id,
                   ei_type AS lTypeId,
                   lFrom AS lFrom
                   FROM 
                       CUL
           ) AS eLType
           ON cLType.uuid = eLType.uuid AND cLType.cmp_id = eLType.cmp_id;



    INSERT INTO `CUL` (`uuid`,`cmp_id`,`ei_type`,`lFrom`)
    SELECT uuid,cmp_id,lTypeId,lFrom FROM temp_lt;

为什么此查询会被置于“发送数据”状态

解释:

1   PRIMARY <derived2>  ALL                 2420567 
1   PRIMARY <derived3>  ALL                 687777  
3   DERIVED CUL index       comp_uuid   468     688032  Using index
2   DERIVED d1  index       datedm_id_UNIQUE    492     4046120 Using index; Using temporary
2   DERIVED d2  ref datedm_id_UNIQUE,agg_ei_comdm_fk_idx,agg_ei_datedm_fk_idx,comp_uuid comp_uuid   460 ol.d1.com_id,ol.d1.ei_uuid  1   Using where; Using index; Not exists

2 个答案:

答案 0 :(得分:0)

使用DESCRIBE来解释你的查询并查看它的减速速度。有时发送数据意味着Mysql的数据存储在尚未读取的磁盘上。

答案 1 :(得分:0)

我已经删除了CUL的子查询,并且查询在150s内执行了......不确定是什么原因

     CREATE TEMPORARY TABLE IF NOT EXISTS temp_lt 
     SELECT cLType.uuid AS uuid, 
cLType.cmp_id AS cmp_id, 
cLType.lTypeId AS lTypeId, 
(CASE WHEN cLType.lTypeId = eLType.lTypeId THEN eLType.lFrom 
         WHEN eLType.lFrom IS NULL THEN DATE_FORMAT(now(),'%Y%m%d') ELSE cLType.lFrom END) AS lFrom 
FROM
     (
      SELECT DISTINCT d1.ei_uuid AS uuid, 
                      d1.cmp_id AS cmp_id,
                      d1.ei_type AS lTypeId,
                      d1.datedm_id AS lFrom
                      FROM AGG_EI d1 
                          LEFT OUTER JOIN AGG_EI d2 
                                          ON (d1.ei_uuid = d2.ei_uuid AND d1.cmp_id = d2.cmp_id AND d1.datedm_id < d2.datedm_id ) 
                      WHERE d2.ei_uuid IS NULL AND d2.cmp_id IS NULL
      ) AS cLType
      LEFT OUTER JOIN 
      (
        SELECT uuid AS uuid, 
               cmp_id AS cmp_id,
               ei_type AS lTypeId,
               lFrom AS lFrom
               FROM 
                   CUL
       ) AS eLType
       ON cLType.uuid = eLType.uuid AND cLType.cmp_id = eLType.cmp_id;



INSERT INTO `CUL` (`uuid`,`cmp_id`,`ei_type`,`lFrom`)
SELECT uuid,cmp_id,lTypeId,lFrom FROM temp_lt;

说明: 新方法

    1   PRIMARY <derived2>  ALL                 2420567 
1   PRIMARY eLType  eq_ref  PRIMARY,uuid_cmp_id,comp_fk_idx,comp_uuid   PRIMARY 460 cLType.uuid,cLType.cmpId    1   
2   DERIVED d1  index       datedm_id_UNIQUE    492     4046120 Using index; Using temporary
2   DERIVED d2  ref datedm_id_UNIQUE,agg_ei_comdm_fk_idx,agg_ei_datedm_fk_idx,comp_uuid comp_uuid   460 ol.d1.comp_id,ol.d1.ei_uuid 1   Using where; Using index; Not exists