连接条件中日期字段的oracle查询性能问题

问题描述 投票:0回答:1

Oracle SQL 查询表 A 中有 400 万条记录,表 B 中有 4700 万条记录,存在性能问题。如果我最后删除日期条件,它运行得更快。请提供建议?

   `select a.* from TBL_ZIP_CD A
    INNER JOIN
    TBL_ZIP4_PLUS_CD B
    ON A.ZIPCD = B.ZIP_CD
    AND
     (B.EFCT_DT BETWEEN A.EFCT_DT and A.EXPR_DT
    AND
        B.EXPR_DT BETWEEN A.EFCT_DT and B.EXPR_DT)
    OR (A.EFCT_DT BETWEN B.EFCT_DT and B.EXPR_DT
    AND A.EXPR_DT BETWEEN B.EFCT_DT and B.EXPR_DT)`
oracle-database
1个回答
0
投票

AND
的优先级高于
OR
,您的查询与:

相同
SELECT a.*
FROM   TBL_ZIP_CD A
       INNER JOIN TBL_ZIP4_PLUS_CD B
       ON (   A.ZIPCD = B.ZIP_CD
          AND B.EFCT_DT BETWEEN A.EFCT_DT and A.EXPR_DT
          AND B.EXPR_DT BETWEEN A.EFCT_DT and B.EXPR_DT
          )
          OR
          (   A.EFCT_DT BETWEEN B.EFCT_DT and B.EXPR_DT
          AND A.EXPR_DT BETWEEN B.EFCT_DT and B.EXPR_DT
          )

您可能想要的是:

SELECT a.*
FROM   TBL_ZIP_CD A
       INNER JOIN TBL_ZIP4_PLUS_CD B
       ON (   A.ZIPCD = B.ZIP_CD
          AND (
                 (
                     B.EFCT_DT BETWEEN A.EFCT_DT and A.EXPR_DT
                 AND B.EXPR_DT BETWEEN A.EFCT_DT and B.EXPR_DT
                 )
              OR (
                     A.EFCT_DT BETWEEN B.EFCT_DT and B.EXPR_DT
                 AND A.EXPR_DT BETWEEN B.EFCT_DT and B.EXPR_DT
                 )
              )
          )

或者也许(取决于您是否期望

B
匹配多行并且不希望
A
中出现重复项(如果有多个匹配项):

SELECT *
FROM   TBL_ZIP_CD A
WHERE  EXISTS(
         SELECT 1
         FROM   TBL_ZIP4_PLUS_CD B
         WHERE  A.ZIPCD = B.ZIP_CD
         AND    (
                   (
                       B.EFCT_DT BETWEEN A.EFCT_DT and A.EXPR_DT
                   AND B.EXPR_DT BETWEEN A.EFCT_DT and B.EXPR_DT
                   )
                OR (
                       A.EFCT_DT BETWEEN B.EFCT_DT and B.EXPR_DT
                   AND A.EXPR_DT BETWEEN B.EFCT_DT and B.EXPR_DT
                   )
                )
       )
© www.soinside.com 2019 - 2024. All rights reserved.