Advanced tips of dbms statas

600 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
600
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Advanced tips of dbms statas

  1. 1. By Louis liu www.vmcd.orgDBMS_STATS Advanced TipsThis article is major to introduce advanced usage of dbms_stats for CG(column group ) of extened statistics.We start this test on oracle 11.2.0.3 oel5.8 64bit :Connected to:Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit ProductionWith the Partitioning, OLAP, Data Mining and Real Application Testing optionsSQL> show userUSER is "TEST"SQL> CREATE TABLE sampletable( id NUMBER,
  2. 2. By Louis liu www.vmcd.org2 hotelname VARCHAR2(50),city VARCHAR2(50),country VARCHAR2(20));INSERT INTO sampletable VALUES( 1, Pullman,Barcelona,Spain );INSERT INTO sampletable VALUES( 2, Gran Melia Fenix,Madrid,Spain );INSERT INTO sampletable VALUES( 3, Melia Castilla,Madrid,Spain );INSERT INTO sampletable VALUES( 4, Trump International,New York,US );INSERT INTO sampletable VALUES( 5, Four Seasons,New York,US );INSERT INTO sampletable VALUES( 6, Ambasciatori Palace,Rome,Italy );COMMIT; 3 4Table created.
  3. 3. By Louis liu www.vmcd.orgSQL> SQL>1 row created.SQL>1 row created.SQL>1 row created.SQL>1 row created.SQL>
  4. 4. By Louis liu www.vmcd.org1 row created.SQL>1 row created.SQL>Commit complete.Now gather table statistics
  5. 5. By Louis liu www.vmcd.orgSQL> EXEC SYS.DBMS_STATS.GATHER_TABLE_STATS (OwnName => TEST, TabName => sampletable, Method_Opt => FORALL COLUMNS size 254, Cascade => TRUE );PL/SQL procedure successfully completed.SQL> SELECT COLUMN_NAME, NUM_DISTINCT FROM DBA_TAB_COL_STATISTICS WHERE TABLE_NAME = SAMPLETABLE;COLUMN_NAME NUM_DISTINCT------------------------------ ------------COUNTRY 3CITY 4
  6. 6. By Louis liu www.vmcd.orgHOTELNAME 6ID 6SQL> conn test/testConnected.SQL> EXPLAIN PLAN FOR SELECT * FROM sampletable WHERE city = Madrid;Explained.SQL> SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY( format => ALLSTATS ));PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------------------------------------
  7. 7. By Louis liu www.vmcd.org------------------------------------------------------------------------------------------------------------------------------------------Plan hash value: 2813936524--------------------------------------------------| Id | Operation | Name | E-Rows |--------------------------------------------------| 0 | SELECT STATEMENT | | 2 ||* 1 | TABLE ACCESS FULL| SAMPLETABLE | 2 |--------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------
  8. 8. By Louis liu www.vmcd.orgPLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------1 - filter("CITY"=Madrid)Note------ Warning: basic plan statistics not available. These are only collected when:* hint gather_plan_statistics is used for the statement or* parameter statistics_level is set to ALL, at session or system level19 rows selected.
  9. 9. By Louis liu www.vmcd.orgSQL> EXPLAIN PLAN FOR SELECT * FROM sampletable WHERE country = Spain;Explained.SQL> SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY( format => ALLSTATS ));PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Plan hash value: 2813936524--------------------------------------------------
  10. 10. By Louis liu www.vmcd.org| Id | Operation | Name | E-Rows |--------------------------------------------------| 0 | SELECT STATEMENT | | 3 ||* 1 | TABLE ACCESS FULL| SAMPLETABLE | 3 |--------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  11. 11. By Louis liu www.vmcd.org1 - filter("COUNTRY"=Spain)Note------ Warning: basic plan statistics not available. These are only collected when:* hint gather_plan_statistics is used for the statement or* parameter statistics_level is set to ALL, at session or system level19 rows selected.
  12. 12. By Louis liu www.vmcd.orgUse dbms_stats.seed_col_usage turns on monitoring for 5 minutes or 300 secondsSQL> begin2 dbms_stats.seed_col_usage(null,null,300);3 end;4 /PL/SQL procedure successfully completed.SQL> EXPLAIN PLAN FOR SELECT * FROM sampletable WHERE city = Madrid and country = Spain;Explained.
  13. 13. By Louis liu www.vmcd.orgSQL> SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY( format => ALLSTATS ));PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Plan hash value: 2813936524--------------------------------------------------| Id | Operation | Name | E-Rows |--------------------------------------------------| 0 | SELECT STATEMENT | | 1 | ------------------->incorrect|* 1 | TABLE ACCESS FULL| SAMPLETABLE | 1 |--------------------------------------------------
  14. 14. By Louis liu www.vmcd.orgPredicate Information (identified by operation id):---------------------------------------------------PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------1 - filter("CITY"=Madrid AND "COUNTRY"=Spain)Note------ Warning: basic plan statistics not available. These are only collected when:
  15. 15. By Louis liu www.vmcd.org* hint gather_plan_statistics is used for the statement or* parameter statistics_level is set to ALL, at session or system level19 rows selected.SQL> set long 90000SQL> set lines 2000SQL> set pages 500SQL> select dbms_stats.report_col_usage(TEST,SAMPLETABLE) from dual;DBMS_STATS.REPORT_COL_USAGE(TEST,SAMPLETABLE)--------------------------------------------------------------------------------LEGEND:
  16. 16. By Louis liu www.vmcd.org.......EQ : Used in single table EQuality predicateRANGE : Used in single table RANGE predicateLIKE : Used in single table LIKE predicateNULL : Used in single table is (not) NULL predicateEQ_JOIN : Used in EQuality JOIN predicateNONEQ_JOIN : Used in NON EQuality JOIN predicateFILTER : Used in single table FILTER predicateJOIN : Used in JOIN predicateGROUP_BY : Used in GROUP BY expression...............................................................................
  17. 17. By Louis liu www.vmcd.org###############################################################################COLUMN USAGE REPORT FOR TEST.SAMPLETABLE........................................1. CITY : EQ2. COUNTRY : EQ3. (CITY, COUNTRY) : FILTER###############################################################################Add extended col statistics for columns (CITY,COUNTRY)
  18. 18. By Louis liu www.vmcd.orgSQL>select dbms_stats.create_extended_stats(ownname=>TEST,tabname=>SAMPLETABLE,extension=>(CITY,COUNTRY))as sample_extended from dual;SAMPLE_EXTENDED------------------------------------------------------------------------------------------------------------------------------------------------------------------SYS_STU3UJN5IT#IIA5ASKY72Q8V6ZSQL> select column_name, data_default, virtual_column, hidden_column from dba_tab_cols where table_name =SAMPLETABLE;
  19. 19. By Louis liu www.vmcd.orgVirtual column “SYS_STU3UJN5IT#IIA5ASKY72Q8V6Z” use function “SYS_OP_COMBINED_HASH”SQL> select column_name, ENDPOINT_NUMBER, ENDPOINT_VALUE from user_tab_histograms where table_name =SAMPLETABLE;COLUMN_NAME ENDPOINT_NUMBER ENDPOINT_VALUE
  20. 20. By Louis liu www.vmcd.org------------------------------------------------------------ --------------- --------------ID 1 1ID 2 2ID 3 3ID 4 4ID 5 5ID 6 6HOTELNAME 1 3.3972E+35HOTELNAME 2 3.6572E+35HOTELNAME 3 3.7097E+35HOTELNAME 4 4.0186E+35HOTELNAME 5 4.1777E+35
  21. 21. By Louis liu www.vmcd.orgCOLUMN_NAME ENDPOINT_NUMBER ENDPOINT_VALUE------------------------------------------------------------ --------------- --------------HOTELNAME 6 4.3847E+35CITY 1 3.4467E+35CITY 3 4.0178E+35CITY 5 4.0706E+35CITY 6 4.2803E+35COUNTRY 1 3.8140E+35COUNTRY 4 4.3324E+35COUNTRY 6 4.4303E+3519 rows selected.
  22. 22. By Louis liu www.vmcd.orgSQL> EXEC SYS.DBMS_STATS.GATHER_TABLE_STATS (OwnName => TEST, TabName => sampletable, Method_Opt =>FOR ALL COLUMNS, Cascade => TRUE );PL/SQL procedure successfully completed.SQL> select column_name, ENDPOINT_NUMBER, ENDPOINT_VALUE from user_tab_histograms where table_name =SAMPLETABLE;COLUMN_NAME ENDPOINT_NUMBER ENDPOINT_VALUE------------------------------------------------------------ --------------- --------------ID 1 1ID 2 2ID 3 3
  23. 23. By Louis liu www.vmcd.orgID 4 4ID 5 5ID 6 6HOTELNAME 1 3.3972E+35HOTELNAME 2 3.6572E+35HOTELNAME 3 3.7097E+35HOTELNAME 4 4.0186E+35HOTELNAME 5 4.1777E+35COLUMN_NAME ENDPOINT_NUMBER ENDPOINT_VALUE------------------------------------------------------------ --------------- --------------HOTELNAME 6 4.3847E+35CITY 1 3.4467E+35
  24. 24. By Louis liu www.vmcd.orgCITY 3 4.0178E+35CITY 5 4.0706E+35CITY 6 4.2803E+35COUNTRY 1 3.8140E+35COUNTRY 4 4.3324E+35COUNTRY 6 4.4303E+35SYS_STU3UJN5IT#IIA5ASKY72Q8V6Z 1 511644451SYS_STU3UJN5IT#IIA5ASKY72Q8V6Z 3 3090128096SYS_STU3UJN5IT#IIA5ASKY72Q8V6Z 5 9086239935COLUMN_NAME ENDPOINT_NUMBER ENDPOINT_VALUE------------------------------------------------------------ --------------- --------------SYS_STU3UJN5IT#IIA5ASKY72Q8V6Z 6 9933106351
  25. 25. By Louis liu www.vmcd.org23 rows selected.SQL> EXPLAIN PLAN FOR SELECT * FROM sampletable WHERE country = Spain and city = Madrid ;Explained.SQL> SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY( format => ALLSTATS ));PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Plan hash value: 2813936524
  26. 26. By Louis liu www.vmcd.org--------------------------------------------------| Id | Operation | Name | E-Rows |--------------------------------------------------| 0 | SELECT STATEMENT | | 2 | -----------------------------> correct|* 1 | TABLE ACCESS FULL| SAMPLETABLE | 2 |--------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------------------------------------
  27. 27. By Louis liu www.vmcd.org--------------------------------------1 - filter("CITY"=Madrid AND "COUNTRY"=Spain)Note------ Warning: basic plan statistics not available. These are only collected when:* hint gather_plan_statistics is used for the statement or* parameter statistics_level is set to ALL, at session or system level19 rows selected.
  28. 28. By Louis liu www.vmcd.orguse 10053 event to trace correlation of these two columns.SQL> conn /as sysdbaConnected.SQL> oradebug setmypidStatement processed.SQL> oradebug unlimitStatement processed.SQL> oradebug event 10053 trace name context forever, level 1Statement processed.SQL>SQL> EXPLAIN PLAN FOR SELECT * FROM TEST.sampletable WHERE country = Spain and city = Madrid ;
  29. 29. By Louis liu www.vmcd.orgExplained.SQL> oradebug event 10053 trace name context offStatement processed.SQL>SQL> oradebug tracefile_name/data/app1/oracle/diag/rdbms/testdb/testdb/trace/testdb_ora_8233.trc
  30. 30. By Louis liu www.vmcd.orgRounded:2 Computer: 2.00
  31. 31. By Louis liu www.vmcd.orgDelete histogramsSQL> EXEC SYS.DBMS_STATS.GATHER_TABLE_STATS (OwnName => TEST, TabName => sampletable, Method_Opt => FORALL COLUMNS size 1,Cascade => TRUE );
  32. 32. By Louis liu www.vmcd.orgRounded: 2 Computed : 1.50of rows~= total # of rows * (1/NDV for CITY) * (1/NDV for COUNTRY)*corStrength(correlation strength)= 6*(1/4)*(1/3)*2=1.5Delete extended statsSQL> exec dbms_stats.DROP_EXTENDED_STATS(TEST,SAMPLETABLE,(CITY,COUNTRY));PL/SQL procedure successfully completed.
  33. 33. By Louis liu www.vmcd.orgRounded: 1 Computed : 0.50of rows~= total # of rows * (1/NDV for CITY) * (1/NDV for COUNTRY) /*corStrength(correlation strength)*/= 6*(1/4)*(1/3)*1=0.5

×