今天同事给我一条2秒的SQL看看能不能优化。
原始SQL:
SELECT pk_dept FROM aaaa WHERE 1 = 1 AND ((pk_group = '0001A110000000000JQ6' AND pk_org IN ('0001A110000000001M09'))) AND (PK_DEPT IN (SELECT t1.ORGID FROM xxxxx t1 INNER JOIN (SELECT (CASE WHEN ORGID3 IS NULL THEN ORGID2 ELSE ORGID3 END) orgid FROM xxxxx WHERE ORGID = '1001A110000000001U8S') t2 ON t1.ORGID2 = t2.orgid OR t1.ORGID3 = t2.orgid)) AND (enablestate IN (2)) ORDER BY code;
4 loops=1) Heap Fetches: 0 Planning Time: 258.024 ms Execution Time: 2493.882 ms (1583 rows)
xxxxx 表是一张非常复杂的视图,上面SQL执行计划大概2千行左右,所以本案例就不放整体的计划了。
如果经常做优化的同学对于简单的SQL,相信可以使用瞪眼大法基本定位到语句慢的位置
标签:t2,t1,改写,恒等式,orgid,SQL,ORGID,SELECT From: https://www.cnblogs.com/yuzhijian/p/17638128.html