码迷,mamicode.com
首页 > 数据库 > 详细

数据库查询优化

时间:2020-12-01 12:38:30      阅读:19      评论:0      收藏:0      [点我收藏+]

标签:cut   lse   pre   语句   HERE   思路   ase   cancel   explain   

原SQL语句:(execution: 2 s 28 ms, fetching: 25 ms)

explain
select
    pr.cid,
    case when pr.type = 2 then ac.auto_cancel else ca.auto_cancel end auto_cancel,
    pr.proxy_id proxy_id
from
    crm_scm_cust_proxy_rel pr LEFT JOIN
    crm_company_config_air ca on pr.cid = ca.cid and ca.auto_cancel >0 LEFT JOIN
    pf_air_config ac on ac.face_cid = pr.cid and ac.auto_cancel >0
where
        pr.status = 1 and
    (( pr.type = 1 and ca.auto_cancel >0) or (pr.type = 2 and ac.auto_cancel >0));

改进后:(execution: 156 ms, fetching: 41 ms)

EXPLAIN
select
    pr.cid,
    case when pr.type = 2 then ac.auto_cancel else ca.auto_cancel end auto_cancel,
    pr.proxy_id proxy_id
from
    crm_scm_cust_proxy_rel pr LEFT JOIN
    (select ca.* from crm_company_config_air ca   where ca.auto_cancel >0)ca  on pr.cid = ca.cid  LEFT JOIN
    (select ac.* from pf_air_config   ac where   ac.auto_cancel >0) ac on ac.face_cid = pr.cid
where
        pr.status = 1 and
    (( pr.type = 1 and ca.auto_cancel >0) or (pr.type = 2 and ac.auto_cancel >0));

第二种思路:

explain
select pr.cid,
       case when pr.type = 2 then ac.auto_cancel else ca.auto_cancel end auto_cancel,
       pr.proxy_id                                                       proxy_id
from crm_scm_cust_proxy_rel pr
         LEFT JOIN
     crm_company_config_air ca on pr.cid = ca.cid and ca.auto_cancel > 0
         LEFT JOIN
     pf_air_config ac on ac.face_cid = pr.cid and ac.auto_cancel > 0
where pr.status = 1
  and ((pr.type = 1 and ca.auto_cancel > 0))
union all
select pr.cid,
       case when pr.type = 2 then ac.auto_cancel else ca.auto_cancel end auto_cancel,
       pr.proxy_id                                                       proxy_id
from crm_scm_cust_proxy_rel pr
         LEFT JOIN
     crm_company_config_air ca on pr.cid = ca.cid and ca.auto_cancel > 0
         LEFT JOIN
     pf_air_config ac on ac.face_cid = pr.cid and ac.auto_cancel > 0
where pr.status = 1
  and (pr.type = 2 and ac.auto_cancel > 0);

 

数据库查询优化

标签:cut   lse   pre   语句   HERE   思路   ase   cancel   explain   

原文地址:https://www.cnblogs.com/Payne-SeediqBale/p/14045367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!