# cbo basics: cardinality

Post on 11-May-2015

1.688 views

Category:

Embed Size (px)

TRANSCRIPT

• 1.CBO Basics: Cardinality
Sidney Chen@zha-dba

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) + 1month_no,
mod(rownum-1,10) + 1 city_no
from
all_objects
where
rownum select month_no, count(*) from audience group by month_no order by month_no;
MONTH_NO COUNT(*)
---------- ----------
1100
2100
3100
4100
5100
6100
7100
8100
9100
10100
11100
12100
12 rows selected.
6. sid@CS10G> select city_no, count(*) from audience group by city_no order by city_no;
CITY_NO COUNT(*)
---------- ----------
1120
2120
3120
4120
5120
6120
7120
8120
9120
10120
10 rows selected.
7. sid@CS10G> @indsid.audience;
OWNER TABLE_NAME BLOCKS NUM_ROWS AVG_ROW_LEN
----- ---------- ---------- ---------- -----------
SID AUDIENCE5 1200 6
sid@CS10G> @descsid.audience;
------------ ------------ ---------- ----------- ---- ----
MONTH_NO 12 .083333333 1 112
CITY_NO10 .1 1 110
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
1391
1482
1573
1664
1755
1845
1936
2027
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
1391
1482
1573
1664
1755
1845
1936
2027
9 rows selected.
19. The far from low/high range, the less you are to find data
If you have sequence, or time-based column in predicate(such as last modified date:last_mod_dt), and havent 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
----------------------------------------------------------------------------------------------------
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: AUDIENCEAlias: AUDIENCE
Card: Original: 1200Rounded: 64Computed: 63.64Non 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

Recommended