The CBO has difficulties in recognizing (by default) that some combinations of column values are far more common than other combinations, resulting in incorrect cardinality estimates and resulting in poor execution plans.
2 nologging as
3 select mod(rownum,100)+1 as x1,
4 mod(rownum,100)+1 as x2,
5 rownum as id, substr(a.object_name,1,30) as object_name
6 from all_objects a ,
7 all_objects b, all_users c
8 where rownum <= 10000000;
2 from DEMO018a
3 where x1 = 17
4 and x2 = 17;
----------------------------------------------------------
Plan hash value: 3250860338
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000 | 30000 | 15208 (1)| 00:00:01 |
|* 1 | TABLE ACCESS STORAGE FULL| DEMO018A | 1000 | 30000 | 15208 (1)| 00:00:01 |
--------------------------------------------------------------------------------------
---------------------------------------------------
filter("X1"=17 AND "X2"=17)
----------------------------------------------------------
32 recursive calls
26 db block gets
55549 consistent gets
55525 physical reads
4628 redo size
2835431 bytes sent via SQL*Net to client
7872 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
100000 rows processed
= 10000000 x ( 1/100 ) * (1/100 ) = 1000
2 from DEMO018a
3 where x1 = 1
4 and x2 = 2;
----------------------------------------------------------
Plan hash value: 3250860338
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000 | 30000 | 15208 (1)| 00:00:01 |
|* 1 | TABLE ACCESS STORAGE FULL| DEMO018A | 1000 | 30000 | 15208 (1)| 00:00:01 |
--------------------------------------------------------------------------------------
---------------------------------------------------
filter("X1"=1 AND "X2"=2)
----------------------------------------------------------
2 recursive calls
0 db block gets
55527 consistent gets
55520 physical reads
0 redo size
369 bytes sent via SQL*Net to client
535 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
-------------------------------------------------------------------------------
Index candidates : 2
Indexes created (visible / invisible) : 2 (2 / 0)
Space used (visible / invisible) : 268.44 MB (268.44 MB / 0 B)
Indexes dropped : 0
SQL statements verified : 4
SQL statements improved (improvement factor) : 2 (132665x)
SQL plan baselines created : 0
Overall improvement factor : 2.3x
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Unused indexes : 0
Space used : 0 B
Unusable indexes : 0
-------------------------------------------------------------------------------
INDEX DETAILS
-------------------------------------------------------------------------------
1. The following indexes were created:
-------------------------------------------------------------------------------
---------------------------------------------------------------------------
| Owner | Table | Index | Key | Type | Properties |
---------------------------------------------------------------------------
| AI_DEMO | DEMO018 | SYS_AI_22nyp7ufnm6j4 | X1,X2 | B-TREE | NONE |
| AI_DEMO | DEMO018A | SYS_AI_gawwp6r5fuzfr | X1,X2 | B-TREE | NONE |
---------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
1. The performance of the following statements improved:
-------------------------------------------------------------------------------
Parsing Schema Name : AI_DEMO
SQL ID : 2h9jguqu62nz4
SQL Text : select x1,x2,object_name from DEMO018a where x1 = 1 and
x2 = 2
Improvement Factor : 55524x
-----------------------------
Original Plan Auto Index Plan
---------------------------- ----------------------------
Elapsed Time (s): 81939 1249
CPU Time (s): 48057 950
Buffer Gets: 166575 3
Optimizer Cost: 15208 4
Disk Reads: 166560 2
Direct Writes: 0 0
Rows Processed: 0 0
Executions: 3 1
---------------------------------------------------------------------------------------------
-----------------------------
Plan Hash Value : 3250860338
| Id | Operation | Name | Rows | Bytes | Cost | Time |
---------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 15208 | |
| 1 | TABLE ACCESS STORAGE FULL | DEMO018A | 1000 | 30000 | 15208 | 00:00:01 |
---------------------------------------------------------------------------------
-----------------------------
Plan Hash Value : 2414111795
| Id | Operation | Name | Rows | Bytes | Cost | Time |
-------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5 | 150 | 4 | 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED | DEMO018A | 5 | 150 | 4 | 00:00:01 |
| * 2 | INDEX RANGE SCAN | SYS_AI_gawwp6r5fuzfr | 1 | | 3 | 00:00:01 |
-------------------------------------------------------------------------------------------------------
------------------------------------------
* 2 - access("X1"=1 AND "X2"=2)
-----
- Dynamic sampling used for this statement ( level = 11 )
2 from user_ind_columns
3 where table_name ='DEMO018A'
4 order by 1,3;
-------------------- ---------- ---------------
SYS_AI_gawwp6r5fuzfr X1 1
SYS_AI_gawwp6r5fuzfr X2 2
2 status,visibility,clustering_factor, leaf_blocks
3 from all_indexes
4 where table_name ='DEMO018A'
5 and owner ='AI_DEMO';
---------- ------------------------- ------- --- -------- --------- ----------------- -----------
AI_DEMO SYS_AI_gawwp6r5fuzfr FULL YES VALID VISIBLE 5552000 15362
------------------------------------ ----------- ------------------------------
optimizer_dynamic_sampling integer 2
ai_demo@PDB19> show parameter optimizer_adaptive_statistics
------------------------------------ ----------- ------------------------------
optimizer_adaptive_statistics boolean FALSE
2 from DEMO018a
3 where x1 = 1
4 and x2 = 2;
----------------------------------------------------------
Plan hash value: 2414111795
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000 | 30000 | 560 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| DEMO018A | 1000 | 30000 | 560 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | SYS_AI_gawwp6r5fuzfr | 1000 | | 4 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------------------
---------------------------------------------------
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
369 bytes sent via SQL*Net to client
535 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
2 from DEMO018a
3 where x1 = 17
4 and x2 = 17;
----------------------------------------------------------
Plan hash value: 2414111795
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000 | 30000 | 560 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| DEMO018A | 1000 | 30000 | 560 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | SYS_AI_gawwp6r5fuzfr | 1000 | | 4 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------------------
---------------------------------------------------
----------------------------------------------------------
3 recursive calls
0 db block gets
56203 consistent gets
0 physical reads
0 redo size
2835214 bytes sent via SQL*Net to client
7872 bytes received via SQL*Net from client
668 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
100000 rows processed
No comments:
Post a Comment