SQL Tuning Guideline Sidney · 2011 Feb 16
Agenda The Guideline to tune sql Application Logic Table/Column statistics SQL access path SQL join NL or hash Join
Response Time The core of user experience
Why? So Slow! Application Logic Tune at sql level
How to review sql Verifying Optimizer Statistics Reviewing the Execution Plan Restructuring the SQL Statements(code change) Restructuring the Indexes
SQLT: Nice tool review top sql Statistics:Tables/Indexes/Columns Execution plan Predication Bind value SQL profile
Table/Index stats Table stats Last_Analyzed Num_Rows Sample_Size Index stats Last_Analyzed Num_Rows Sample_Size DISTINCT_KEYS BLEVEL Clustering_Factor
Column statistics num_rows  num_distinct density num_nulls num_buckets histogram low_value high_value
How to gather Table/Column statistics Using auto method exec dbms_stats.gather_table_stats('CS2_PARTY_OWNER','CS2_BKG_RQST',method_opt=>' for all columns size auto', cascade=>true); For specific columns exec dbms_stats.gather_table_stats('CS2_PARTY_OWNER','CS2_BKG_RQST',method_opt=>' for columns size 254 SP_COMPANY_ID', cascade=>true);
Column Histogram
Review execution plan Driving table has the best filter Join method: Nested Loop or Hash Join Join order: Fewest number of rows are being returned to the next step Each table is being accessed efficiently index scan full table scan
Nested Loop For small number of rows(< 1000 ), with a good driving condition between the two tables
Hash Function
Hash Join   For large data set(> 1000 ): The optimizer uses the smaller of two tables or data sources to build a hash table on the join key in memory
Access Path Full Table Scans Index Scans Index Unique Scans Index Range Scans Full Scans Fast Full Index Scans Index Joins
SQL Profile SQL Profiles are the new feature from 10g which is managed by Oracle Enterprise Manager as part of the Automatic SQL Tuning process. Apart from OEM, SQL Profiles can be managed through the DBMS_SQLTUNE package.
normal usage of SQL Profile Run the SQL Tuning Advisor  Accept the recommended SQL Profile
advance usage: coe_xfr_sql_profile.sql coe_xfr_sql_profile.sql is from SQLT tool  An easy way to lock the sql plan as outline
 
Output sql format coe_xfr_sql_profile_<SID>_<PLAN_Hash_value>.SQL coe_xfr_sql_profile_chpub18xajb2w_1020905707.SQL
Q&A

Sql tuning guideline

  • 1.
    SQL Tuning GuidelineSidney · 2011 Feb 16
  • 2.
    Agenda The Guidelineto tune sql Application Logic Table/Column statistics SQL access path SQL join NL or hash Join
  • 3.
    Response Time Thecore of user experience
  • 4.
    Why? So Slow!Application Logic Tune at sql level
  • 5.
    How to reviewsql Verifying Optimizer Statistics Reviewing the Execution Plan Restructuring the SQL Statements(code change) Restructuring the Indexes
  • 6.
    SQLT: Nice toolreview top sql Statistics:Tables/Indexes/Columns Execution plan Predication Bind value SQL profile
  • 7.
    Table/Index stats Tablestats Last_Analyzed Num_Rows Sample_Size Index stats Last_Analyzed Num_Rows Sample_Size DISTINCT_KEYS BLEVEL Clustering_Factor
  • 8.
    Column statistics num_rows num_distinct density num_nulls num_buckets histogram low_value high_value
  • 9.
    How to gatherTable/Column statistics Using auto method exec dbms_stats.gather_table_stats('CS2_PARTY_OWNER','CS2_BKG_RQST',method_opt=>' for all columns size auto', cascade=>true); For specific columns exec dbms_stats.gather_table_stats('CS2_PARTY_OWNER','CS2_BKG_RQST',method_opt=>' for columns size 254 SP_COMPANY_ID', cascade=>true);
  • 10.
  • 11.
    Review execution planDriving table has the best filter Join method: Nested Loop or Hash Join Join order: Fewest number of rows are being returned to the next step Each table is being accessed efficiently index scan full table scan
  • 12.
    Nested Loop Forsmall number of rows(< 1000 ), with a good driving condition between the two tables
  • 13.
  • 14.
    Hash Join For large data set(> 1000 ): The optimizer uses the smaller of two tables or data sources to build a hash table on the join key in memory
  • 15.
    Access Path FullTable Scans Index Scans Index Unique Scans Index Range Scans Full Scans Fast Full Index Scans Index Joins
  • 16.
    SQL Profile SQLProfiles are the new feature from 10g which is managed by Oracle Enterprise Manager as part of the Automatic SQL Tuning process. Apart from OEM, SQL Profiles can be managed through the DBMS_SQLTUNE package.
  • 17.
    normal usage ofSQL Profile Run the SQL Tuning Advisor Accept the recommended SQL Profile
  • 18.
    advance usage: coe_xfr_sql_profile.sqlcoe_xfr_sql_profile.sql is from SQLT tool An easy way to lock the sql plan as outline
  • 19.
  • 20.
    Output sql formatcoe_xfr_sql_profile_<SID>_<PLAN_Hash_value>.SQL coe_xfr_sql_profile_chpub18xajb2w_1020905707.SQL
  • 21.