Lennart Jonsson wrote:
> From my understanding (which is a bit limited), the selectivity estimate
> for a join is 1 / max(cc1, cc2), AND corresponds to * which would give us:
>
> 1 / max(126, 100) * 1 / max(3107, 3128)
Yes, this is exactly what the optimizer seems to be doing, ignoring the
FIRST2KEYCARDS of 182981 and 4300 and thus arriving at a cardinality
estimate that is far too low. In my mind, it should use the highest
possible estimate, which is
1 / min(FIRST2KEYCARD(M.R1, M.R2), FIRST2KEYCARD(P.F1, P.F2)
= 1 / min(182981, 4300) = 1 / 4300
That comes much closer to the actual value.
I now have access to version 9.7.5 of DB2 and can present the actual
cardinalities of the join, obtained with Serge Rielau's package
(
https://www.ibm.com/developerworks/mydeveloperworks/blogs/SQLTips4DB2LUW/entry/sql_pl_profiler_and_plan_explain_with_actual_row_counts23?lang=en):
24,6238
6,33755e+006
NLJOIN
( 63)
3347
NA
/----------+----------\
36,1058 0,68199
1578 4016,19
FETCH FETCH
( 64) ( 68)
2231,52 30,2802
NA NA
/---+----\ /---+----\
4880,92 2,37706e+007 9,93351 3,83622e+006
12161 NA 22544,3 NA
RIDSCN TABLE: SCHEMA RIDSCN TABLE: SCHEMA
( 65) TABLEMMMMMM ( 69) TABLEPPPPPP
168,626 Q32 22,7089 Q28
NA NA
| |
4880,92 9,93351
10583 18528,1
SORT SORT
( 66) ( 70)
168,625 22,7086
NA NA
| |
4880,92 9,93351
10583 18528,1
IXSCAN IXSCAN
( 67) ( 71)
166,817 22,7075
NA NA
| |
2,37706e+007 3,83622e+006
NA NA
INDEX: SCHEMA INDEX: SCHEMA
IDXMMMMMMN02 IDXPPPPPPQ01
Q32 Q28
The problem can be seen in the IXSCAN (71) on the right, where the
optimizer scans for the two join predicates. The estimated result
cardinality is ~10 (first line), and the actual cardinality is ~18528
(second line).
Sorry, but I cannot post the dblook info for this database, as it is
client confidential. I think, however, that the given numbers together
with the developerworks article I quoted
(
http://www.ibm.com/developerworks/data/library/techarticle/dm-0612chen/) are
sufficient to see how the optimizer arrives at its erroneous estimates.
Regards,
Oliver Schoett