Sql DB2上不存在奇数性能的地方

Sql DB2上不存在奇数性能的地方,sql,performance,db2,Sql,Performance,Db2,在运行以下查询时,我在DB2 9.1版上体验到非常奇怪的性能: select a.CYCL_NUM , a.AC_NUM , a.AUTHS_DTE , a.PL_ID , a.APRVD_RSPN_CDE , a.AUTHS_AMT , a.AUTHS_STS_CDE , a.TRAN_CTGR_CDE , a.MRCHN_CTGR_CDE , d.out_pu_au_amt from nwhd12.chldr_auths a, nwhd12.w_chld

在运行以下查询时,我在DB2 9.1版上体验到非常奇怪的性能:

select  a.CYCL_NUM
,   a.AC_NUM
,   a.AUTHS_DTE
,   a.PL_ID
,   a.APRVD_RSPN_CDE
,   a.AUTHS_AMT
,   a.AUTHS_STS_CDE
,   a.TRAN_CTGR_CDE
,   a.MRCHN_CTGR_CDE
,   d.out_pu_au_amt
from nwhd12.chldr_auths a, nwhd12.w_chldr_ac d 
where cycl_num = 200911
and a.ac_num = d.ac_num
and APRVD_RSPN_CDE = 'APV'
and not exists (
    select 1 from auths_rev_hist b
 where a.cycl_num = b.cycl_num
        and a.auths_dte = b.auths_dte
        and a.TRAN_CTGR_CDE = b.TRAN_CTGR_CDE
        and a.PL_ID = b.pl_id
        and a.APRVD_RSPN_CDE = b.APRVD_RSPN_CDE
 and a.AUTHS_AMT = b.auths_amt
        and a.TRAN_CTGR_CDE = b.TRAN_CTGR_CDE
        and a.MRCHN_CTGR_CDE = MRCHN_CTGR_CDE
)
;
应该发生的是,查询访问nwhd12.chldr_auths的第97部分,因为该部分对应于循环200911。相反,在访问分区97之后,它开始访问nwhd12.chldr_auths中的每个其他分区。现在,我被告知这是因为“WHERE NOT EXISTS”,但在这个语句中仍然有循环限制(a.cycl_num=b.cycl_num),那么为什么它要扫描所有分区呢

如果我在where not exists中硬编码循环,那么查询将按预期执行

谢谢,
Dave

如果计划者很容易混淆,那么您需要尝试几种不同的公式。这是未经测试的(我甚至没有DB2,但CTEs起源于此):


有趣的是,我以前从未以这种格式编写过查询。现在尝试一下,但似乎有一些错误,看看我是否能找出它。听起来规划者好像有点糊涂了!我认为where not exists中的连接应该按设计执行,对吗?
WITH
引入了CTE(公共表表达式),它类似于临时视图。你犯了什么错误?至于混淆:规划者实际上是正确的,是我混淆了:在SQL中,A和B与B和A完全相同(与大多数带有短路的编程语言相反)。
WITH hist AS (
    cycl_num
  , ac_num
  , auths_dte
  , pl_id
  , aprvd_rspn_cde
  , auths_amt
  , auths_sts_cde
  , tran_ctgr_cde
  , mrchn_ctgr_cde
  FROM auths_rev_hist b
)
, auths AS (
  SELECT
    cycl_num
  , ac_num
  , auths_dte
  , pl_id
  , aprvd_rspn_cde
  , auths_amt
  , auths_sts_cde
  , tran_ctgr_cde
  , mrchn_ctgr_cde
  FROM nwhd12.chldr_auths
  WHERE cycl_num = 200911
    AND aprvd_rspn_cde = 'APV'
  EXCEPT
  SELECT ... FROM hist
)
SELECT a.*, d.out_pu_au_amt
FROM auths a, nwhd12.w_chldr_ac d
WHERE a.ac_num = d.ac_num