2. Agenda Cardinality The Cardinality under Various Prediction Case study: incorrect high value
3. Cardinality The estimated number of rows a query is expected to return. Number of rows in table x Predicate selectivity
4. Imaging there are 1200 people attend the dba weekly meeting, They are randomly born in 12 month and comes from 10 citites, given the data the even distributed. create table audience as select mod(rownum-1,12) + 1 month_no, mod(rownum-1,10) + 1 city_no from all_objects where rownum <= 1200;
5. sid@CS10G> select month_no, count(*) from audience group by month_no order by month_no; MONTH_NO COUNT(*) ---------- ---------- 1 100 2 100 3 100 4 100 5 100 6 100 7 100 8 100 9 100 10 100 11 100 12 100 12 rows selected.
6. sid@CS10G> select city_no, count(*) from audience group by city_no order by city_no; CITY_NO COUNT(*) ---------- ---------- 1 120 2 120 3 120 4 120 5 120 6 120 7 120 8 120 9 120 10 120 10 rows selected.
8. Critical info NDK: number of distinct keys Density = 1/NDK, (0.1 = 1/10) (0.083333333 = 1/12) NUM_BUCKETS=1, there is no histogram gather NUM_BUCKETS > 1, histogram gathered
9. select month_no from audience where month_no=12; Cardinality 1200 * (1/12) = 100
10. sid@CS10G> select month_no from audience where month_no=12; 100 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 2423062965 ------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| ------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 100 | 300 | 3 (0)| |* 1 | TABLE ACCESS FULL| AUDIENCE | 100 | 300 | 3 (0)| ------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("MONTH_NO"=12)
11. select month_no from audience where city_no=1; Cardinality 1200 * (1/10) = 120
12. sid@CS10G> select month_no from audience where city_no=1; 120 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 2423062965 ------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| ------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 120 | 720 | 3 (0)| |* 1 | TABLE ACCESS FULL| AUDIENCE | 120 | 720 | 3 (0)| ------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("CITY_NO"=1)
13. select month_no from audience where month_no > 9; Cardinality 1200 * ( (12-9)/(12-1) ) = 327
14. sid@CS10G> select month_no from audience where month_no > 9; 300 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 2423062965 ------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| ------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 327 | 981 | 3 (0)| |* 1 | TABLE ACCESS FULL| AUDIENCE | 327 | 981 | 3 (0)| ------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("MONTH_NO">9)
15. Equality: If Out of range explain plan set statement_id = '12' for select month_no from audience where month_no = 12; explain plan set statement_id = '13' for select month_no from audience where month_no = 13; explain plan set statement_id = '14' for select month_no from audience where month_no = 14; explain plan set statement_id = '15' for select month_no from audience where month_no = 15; explain plan set statement_id = '16' for select month_no from audience where month_no = 16; explain plan set statement_id = '17' for select month_no from audience where month_no = 17; explain plan set statement_id = '18' for select month_no from audience where month_no = 18; explain plan set statement_id = '19' for select month_no from audience where month_no = 19; explain plan set statement_id = '20' for select month_no from audience where month_no = 20;
16. sid@CS10G> select statement_id, cardinality from plan_table where id=1 order by statement_id; STATEMENT_ID CARDINALITY ------------ ----------- 12 100 13 91 14 82 15 73 1664 17 55 18 45 19 36 20 27 9 rows selected.
17. Range: If Out of range explain plan set statement_id = '12' for select month_no from audience where month_no > 12; explain plan set statement_id = '13' for select month_no from audience where month_no > 13; explain plan set statement_id = '14' for select month_no from audience where month_no > 14; explain plan set statement_id = '15' for select month_no from audience where month_no > 15; explain plan set statement_id = '16' for select month_no from audience where month_no > 16; explain plan set statement_id = '17' for select month_no from audience where month_no > 17; explain plan set statement_id = '18' for select month_no from audience where month_no > 18; explain plan set statement_id = '19' for select month_no from audience where month_no > 19; explain plan set statement_id = '20' for select month_no from audience where month_no > 20;
18. sid@CS10G> select statement_id, cardinality from plan_table where id=1 order by statement_id; STATEMENT_ID CARDINALITY ------------ ----------- 12 100 13 91 14 82 15 73 1664 17 55 18 45 19 36 20 27 9 rows selected.
19. The far from low/high range, the less you are to find data
20. Bad news If you have sequence, or time-based column in predicate(such as last modified date:last_mod_dt), and haven’t been keeping the statistics up to date The Cardinality will drop as time passes, if you using equality and range on that column
21. sid@CS10G> select month_no from audience where month_no > 16; no rows selected Execution Plan ---------------------------------------------------------- Plan hash value: 2423062965 -------------------------------------+-----------------------------------+ | Id | Operation | Name | Rows | Bytes | Cost | Time | -------------------------------------+-----------------------------------+ | 0 | SELECT STATEMENT | | | | 3 | | | 1 | TABLE ACCESS FULL | AUDIENCE| 64 | 192 | 3 | 00:00:01 | -------------------------------------+-----------------------------------+ Predicate Information: ---------------------- 1 - filter("MONTH_NO"=16)
22. Using 10053 event to confirm sid@CS10G> @53on alter session set events '10053 trace name context forever, level 1'; Session altered. sid@CS10G> explain plan for select month_no from audience where month_no > 16; Explained. sid@CS10G> @53off sid@CS10G> @tracefile TRACEFILE ---------------------------------------------------------------------------------------------------- /home/u02/app/oracle/product/11.1.0/db_1/admin/cs10g/udump/cs10g_ora_24947.trc
23. SINGLE TABLE ACCESS PATH ----------------------------------------- BEGIN Single Table Cardinality Estimation ----------------------------------------- Column (#1): MONTH_NO(NUMBER) AvgLen: 3.00 NDV: 12 Nulls: 0 Density: 0.083333 Min: 1 Max: 12 Using prorated density: 0.05303 of col #1 as selectivity of out-of-range value pred Table: AUDIENCE Alias: AUDIENCE Card: Original: 1200 Rounded: 64 Computed: 63.64 Non Adjusted: 63.64 ----------------------------------------- END Single Table Cardinality Estimation -----------------------------------------
24. Between: If Out of range explain plan set statement_id = '12' for select month_no from audience where month_nobetween 13 and 15; explain plan set statement_id = '14' for select month_no from audience where month_nobetween 14 and 16; explain plan set statement_id = '15' for select month_no from audience where month_nobetween 15 and 17; explain plan set statement_id = '16' for select month_no from audience where month_no between 13 and 20; explain plan set statement_id = '17' for select month_no from audience where month_no between 14 and 21; explain plan set statement_id = '18' for select month_no from audience where month_no between 15 and 22; explain plan set statement_id = '19' for select month_no from audience where month_no between 16 and 23;
25. sid@CS10G> select statement_id, cardinality from plan_table where id=1 order by statement_id; STATEMENT_ID CARDINALITY ------------ ----------- 12 100 13 100 14 100 15 100 16 100 17 100 18 100 19 100 20 100 9 rows selected.
27. SELECT count('1') RECCOUNT FROM Test_ILM_INTERACTIONt0 JOIN Test_ILM_INTERACTION_TYPEt1 ON t0.INTERACTION_TYP = t1.INTERACTION_TYP JOIN Test_ILM_INTERACTION_REFt3 ON t0.interaction_uuid = t3.interaction_uuid WHERE t1.IS_VIEWABLE = 1 AND ((t0.DOMAIN_NME = 'DOMAIN_A') or (T0.DOMAIN_NME = 'DOMAIN_B' AND T0.APPLICATION_NME = 'APPLICATION_C')) AND (t3.REF_CDE = 'BK_NUMBER' AND t3.REF_KEY_VALUE = '2389301444') AND t0.INTERACTION_DT BETWEEN TO_DATE('01-06-2011 16:00:00', 'DD-MM-YYYY HH24:MI:SS') AND TO_DATE('16-06-2011 15:59:59', 'DD-MM-YYYY HH24:MI:SS')
28. ---------------------------------------------------------------------+---------------- | Id | Operation | Name | Rows | Cost | ---------------------------------------------------------------------+---------------- | 0 | SELECT STATEMENT | | | 13 | | 1 | SORT AGGREGATE | | 1 | | | 2 | NESTED LOOPS | | 1 | 13 | | 3 | NESTED LOOPS | | 1 | 12 | | 4 | INDEX RANGE SCAN | Test_ILM_INTERACTION_IDX3 | 4 | 4 | | 5 | INDEX UNIQUE SCAN | Test_ILM_INTERACTION_REF_PK| 1 | 2 | | 6 | TABLE ACCESS BY INDEX ROWID | Test_ILM_INTERACTION_TYPE| 1 | 1 | | 7 | INDEX UNIQUE SCAN | Test_ILM_INTERACTION_TYPE_PK| 1 | 0 | ---------------------------------------------------------------------+---------------- Predicate Information (identified by operation id): --------------------------------------------------- 4 - access("T0"."INTERACTION_DT">=TO_DATE('2011-06-01 05:00:00', 'yyyy-mm-dd hh24:mi:ss') AND "T0"."INTERACTION_DT"<=TO_DATE('2011-06-16 04:59:59', 'yyyy-mm-dd hh24:mi:ss')) filter("T0"."DOMAIN_NME"='DOMAIN_A' OR "T0"."APPLICATION_NME"='APPLICATION_C' AND "T0"."DOMAIN_NME"='DOMAIN_B') 5 - access("T0"."INTERACTION_UUID"="T3"."INTERACTION_UUID" AND "T3"."REF_CDE"='BL_NUMBER' AND "T3"."REF_KEY_VALUE"=2389301444') 6 - filter("T1"."IS_VIEWABLE"=1) 7 - access("T0"."INTERACTION_TYP"="T1"."INTERACTION_TYP")