English 中文(简体)
Odd WHERE NOT EXISTS performance on DB2
原标题:

I am experiencing very odd performance on DB2 version 9.1 when running the query below:

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
)
;

What is supposed to happen is that the query accesses partion 97 of nwhd12.chldr_auths, since that is the partition corresponding to cycle 200911. Instead, after accessing partition 97, it starts accessing every other partition in nwhd12.chldr_auths. Now, I was told that this is because of the "WHERE NOT EXISTS", but there is still the restriction on cycles in this statement (a.cycl_num = b.cycl_num), so why is it scanning all the partitions?

If I hard code the cycle in the where not exists, then the query performs as expected.

Thanks, Dave

最佳回答

if the planner is this easily confused, you need to try a few different formulations. this untested (I don t even have DB2, but CTEs originated there):

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
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签