oracle 当在WHERE中使用“OR”时查询中的性能问题

jjhzyzn0  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(114)

此查询在700 ms内执行:

SELECT pcl.COL1,
       pcl.COL2,
       pcl.COL3,
       pcl.COL4,
       (SELECT cc.COL5
          FROM T3 cc
          JOIN T4 cvp
            ON cvp.COL10 = cc.COL10
           AND cvp.COL2 = pcl.COL2) AS COL10,
       pp.COL11
FROM T1 ba
JOIN T2 pcl
  ON pcl.COL1 = ba.COL1
 AND (pcl.COL2 = ba.COL2 OR ba.COL2 = 0)
JOIN T5 pscl
  ON pscl.COL2 = pcl.COL2
 AND pscl.COL4 <> 3
 AND (pscl.COL6 = ba.COL6 OR ba.COL6 = 'ALL')
LEFT JOIN T6 bba
  ON bba.COL7 = ba.COL7
LEFT JOIN T7 bsa
  ON bsa.COL7 = ba.COL7
LEFT JOIN T8 pp
  ON pp.COL1 = pcl.COL1
WHERE (ba.COL8 = 617617 OR bsa.COL8 = 617617 OR bba.COL9 = 617617)

问题是:

(ba.COL8 = 617617 OR bsa.COL8 = 617617 OR bba.COL9 = 617617)

COL 8和COL 9有索引。如果我保持3个条件中的任何一个,那么它需要30 ms来执行。如果我保留其中两个,那么执行需要300 ms。但我需要他们三个。如何改进/重写我的SELECT?

ivqmmu1c

ivqmmu1c1#

您没有使用bbabsa表,除非将其用作筛选器,因此您可以从使用JOIN s更改为使用EXISTS

SELECT pcl.COL1,
       pcl.COL2,
       pcl.COL3,
       pcl.COL4,
       (SELECT cc.COL5
          FROM T3 cc
          JOIN T4 cvp
            ON cvp.COL10 = cc.COL10
           AND cvp.COL2 = pcl.COL2) AS COL10,
       pp.COL11
FROM   T1 ba
       JOIN T2 pcl
       ON    pcl.COL1 = ba.COL1
         AND (pcl.COL2 = ba.COL2 OR ba.COL2 = 0)
       JOIN T5 pscl
       ON     pscl.COL2 = pcl.COL2
          AND pscl.COL4 <> 3
          AND (pscl.COL6 = ba.COL6 OR ba.COL6 = 'ALL')
       LEFT JOIN T8 pp
       ON pp.COL1 = pcl.COL1
WHERE  ba.COL8 = 617617
OR     EXISTS(SELECT 1 FROM T6 bba WHERE bba.COL7 = ba.COL7 AND bba.COL9 = 617617)
OR     EXISTS(SELECT 1 FROM T7 bsa WHERE bsa.COL7 = ba.COL7 AND bsa.COL8 = 617617)

相关问题