2

我在 DB2 上运行的应用程序中有以下查询:

SELECT COD.POST_CD,CLS.CLASS,COD2.STATUS_CD
FROM DC01.POSTAL_CODES COD
INNER JOIN DC02.STATUS_CODES COD2
ON COD.ORDER=COD2.ORDER
INNER JOIN DC02.VALID_ORDERS ORD
ON ORD.ORDER=COD.ORDER
WHERE
( 
    ( EXISTS (SELECT 1 FROM DC00.PROCESS_ORDER PRD
          WHERE PRD.ORDER=COD.ORDER
              AND PRD.IDNUM=COD.IDNUM
        )
    ) OR
    ( EXISTS (SELECT 1 FROM DC00.PENDING_ORDER PND
          WHERE PND.ORDER=COD.ORDER
              AND PND.IDNUM=COD.IDNUM
        )
    )
)
AND EXISTS (SELECT 1 FROM DC00.CUSTOM_ORDER CRD 
           WHERE CRD.ORDER=COD.ORDER
                  )
;

当我们更改为 UDB (LUW v9.5) 时,我们收到以下警告:

IWAQ0003W SQL warnings were found
SQLState=01602 Performance of this complex query might be sub-optimal.  
Reason code: "3".. SQLCODE=437, SQLSTATE=01602, DRIVER=4.13.111

我知道这个警告是由于 EXISTS () OR EXISTS 语句。但我不确定是否有任何其他方式可以编写此查询来替换。如果是 AND,我可以进行 INNER JOIN,但我无法更改此条件,因为它是 OR。有人可以提出更好的方法来替换这些 EXISTS 语句吗?

4

1 回答 1

0
SELECT COD.POST_CD,CLS.CLASS,COD2.STATUS_CD
FROM DC01.POSTAL_CODES COD
INNER JOIN DC02.STATUS_CODES COD2
ON COD.ORDER=COD2.ORDER
INNER JOIN DC02.VALID_ORDERS ORD
ON ORD.ORDER=COD.ORDER
WHERE
( 
     EXISTS SELECT 1 FROM 
              (SELECT ORDER,IDNUM FROM DC00.PROCESS_ORDER PRD UNION 
               SELECT ORDER,IDNUM FROM DC00.PENDING_ORDER PND) PD
          WHERE PD.ORDER=COD.ORDER
              AND PD.IDNUM=COD.IDNUM
)
AND EXISTS (SELECT 1 FROM DC00.CUSTOM_ORDER CRD 
           WHERE CRD.ORDER=COD.ORDER
              )
;
于 2013-11-13T08:05:23.113 回答