mysql子查询极其缓慢

时间:2013-09-28 17:09:54

标签: mysql sql performance subquery

我展示了一个涉及多个跟踪表的销售查询。 (仅显示一些数据)

SELECT  
    CONCAT(cam.c_prefix, LPAD(v.id_sale,5,'0')) Code,
    ve.estate_name last_state,
    v.date_instalation, 
    va.date_state date_modification  
FROM sale v 
INNER JOIN headquarters_detail sd ON v.id_headquarters_detail = sd.id_headquarters_detail 
INNER JOIN headquarters se ON sd.id_headquarters = se.id_headquarters 
INNER JOIN campaign cam ON sd.id_campaign = cam.id_campaign 
INNER JOIN sale_activity va ON va.id_sale = v.id_sale 
INNER JOIN state_detail vec ON vec.id_state_detail = va.id_state_detail
INNER JOIN sale_state ve ON ve.iid_sale_state = vec.iid_sale_state
WHERE  v.flag = 1 
AND     EXISTS( SELECT  '1' 
                            FROM sale_history
                            WHERE   id_sale = v.id_sale 
                            AND flag = 1
                            LIMIT 1)  
AND v.id_headquarters_detail = 2 

此查询需要 0.650s 。我想添加不同的列,子查询,我返回分阶段分发的最后一个销售状态。类似的东西:

id_sale | ... | last_state_of_stage_1 | last_state_of_stage_2 | last_state_of_stage_3
01      | ... | new_sale              | distributed_sale      | canceled_sale
02      | ... | new_sale              | distributed_sale      | invoiced_sale
...

每次销售的状态和阶段都存储在历史记录中:

sale_history
------------
id_history  int  -- primary key
id_sale int
id_state_detail int -- contains the states id's and logic
observation tinytext
date_history    datetime
id_user int
id_profile  int
...

索引:

CREATE INDEX indx_id_sale USING BTREE ON sale_history (id_sale);
CREATE INDEX indx_id_state_detail USING BTREE ON sale_history (id_state_detail);

每个历史记录平均有25个待售记录。为了获得销售状态,我认为子查询(以及每个阶段):

SELECT  
    CONCAT(cam.c_prefix, LPAD(v.id_sale,5,'0')) Code,
    ve.estate_name last_state,
    v.date_instalation, 
    va.date_state date_modification,  

IFNULL((SELECT state_name
            FROM sale_history veh 
            INNER JOIN state_detail vec ON  vec.id_state_detail = veh.id_state_detail 
            INNER JOIN sale_state ve ON ve.iid_sale_state= vec.iid_sale_state
            WHERE veh.id_sale = v.id_sale
            AND vec.iid_sale_stage = 5
            AND veh.flag = 1  
            ORDER BY veh.id_history DESC
            LIMIT 1
),'x') last_state_of_stage_1

FROM sale v 
INNER JOIN headquarters_detail sd ON v.id_headquarters_detail = sd.id_headquarters_detail 
INNER JOIN headquarters se ON sd.id_headquarters = se.id_headquarters 
INNER JOIN campaign cam ON sd.id_campaign = cam.id_campaign 
INNER JOIN sale_activity va ON va.id_sale = v.id_sale 
INNER JOIN state_detail vec ON vec.id_state_detail = va.id_state_detail
INNER JOIN sale_state ve ON ve.iid_sale_state = vec.iid_sale_state
WHERE  v.flag = 1 
AND     EXISTS( SELECT  '1' 
                            FROM sale_history
                            WHERE   id_sale = v.id_sale 
                            AND flag = 1
                            LIMIT 1)  
AND v.id_headquarters_detail = 2

但此子查询 115.985s 延迟!我想知道因为它需要这么长时间?

编辑30-09-13

通过fancyPants的评论,我做了一些改进,大大提高了查询的速度:

SELECT  
    CONCAT(cam.c_prefix, LPAD(v.id_sale,5,'0')) Code,
    ve.estate_name last_state,
    v.date_instalation, 
    va.date_state date_modification,
    IFNULL(ve5.state_name,'x') last_state_of_stage_1
FROM sale v 
INNER JOIN headquarters_detail sd ON v.id_headquarters_detail = sd.id_headquarters_detail 
INNER JOIN headquarters se ON sd.id_headquarters = se.id_headquarters 
INNER JOIN campaign cam ON sd.id_campaign = cam.id_campaign 
INNER JOIN sale_activity va ON va.id_sale = v.id_sale 
INNER JOIN state_detail vec ON vec.id_state_detail = va.id_state_detail
INNER JOIN sale_state ve ON ve.iid_sale_state = vec.iid_sale_state
LEFT JOIN sale_history veh5 ON veh5.id_sale = v.id_sale AND veh5.flag = 1 AND veh5.id_history = (SELECT MAX(sh.id_history) 
                                                                                                                                                                                                    FROM sale_history sh 
                                                                                                                                                                                                    INNER JOIN state_detail vecx ON vecx.id_state_detail = sh.id_state_detail
                                                                                                                                                                                                    INNER JOIN sale_state vex ON vex.iid_sale_state= vecx.iid_sale_state
                                                                                                                                                                                                    WHERE sh.id_sale = v.id_sale AND sh.id_sale = veh5.id_sale
                                                                                                                                                                                                    AND sh.flag = 1
                                                                                                                                                                                                    AND vecx.iid_sale_stage = 5
                                                                                                                                                                                                    )
LEFT JOIN state_detail vec5 ON vec5.id_state_detail = veh5.id_state_detail
LEFT JOIN sale_state ve5 ON ve5.iid_sale_state = vec5.iid_sale_state
WHERE  v.flag = 1 
AND     EXISTS( SELECT  '1' 
                            FROM sale_history
                            WHERE   id_sale = v.id_sale 
                            AND flag = 1
                            LIMIT 1)  
AND v.id_headquarters_detail = 2 
AND EXISTS(SELECT '1'
                FROM sale_activity veh
                INNER JOIN state_detail vec ON  vec.id_state_detail = veh.id_state_detail 
                INNER JOIN sale_state ve    ON ve.iid_sale_state= vec.iid_sale_state
                WHERE id_sale = v.id_sale
                AND iid_sale_stage = 4
                LIMIT 1)

现在以 0.609s 显示数据,谢谢!

2 个答案:

答案 0 :(得分:1)

对于表的每一行,内部查询一次又一次地运行,以指数方式呈现您的查询时间(随着更多记录而变得更糟)。这就是为什么在实际应用中要小心处理子查询的原因。

我建议您尝试使用不同的方法来处理此查询(在很多情况下,某些复杂的JOIN组合可以替换子查询)。

答案 1 :(得分:1)

如果此查询效果更好,请试一试:

SELECT  
    CONCAT(cam.c_prefix, LPAD(v.id_sale,5,'0')) Code,
    ve.estate_name last_state,
    v.date_instalation, 
    va.date_state date_modification,  
    COALESCE(state_name, 'x') last_state_of_stage_1
FROM sale v 
INNER JOIN headquarters_detail sd ON v.id_headquarters_detail = sd.id_headquarters_detail 
INNER JOIN headquarters se ON sd.id_headquarters = se.id_headquarters 
INNER JOIN campaign cam ON sd.id_campaign = cam.id_campaign 
INNER JOIN sale_activity va ON va.id_sale = v.id_sale 
INNER JOIN state_detail vec ON vec.id_state_detail = va.id_state_detail
INNER JOIN sale_state ve ON ve.iid_sale_state = vec.iid_sale_state
INNER JOIN sale_history veh ON veh.id_sale = v.id_sale AND vec.iid_sale_stage = 5 AND veh.flag = 1 
WHERE  v.flag = 1 
AND v.id_headquarters_detail = 2
AND veh.id_history = (SELECT MAX(id_history) FROM sale_history sh WHERE sh.id_sale = veh.id_sale);

如果没有(假设它产生了正确的结果),请在其前面再次执行EXPLAIN并发布结果。