SQL Tuning Scripts Bobby Durrett US Foodservice
Tuning a slow SQL statement Create directory with tuning scripts Capture problem SQL Get execution plan Query DBA_ views Run select count(*) queries Try different plans Test execution of improved SQL
Why these scripts? DBA view columns used by optimizer Tables in problem SQL only Records current state of statistics Readable format Only needs SQL*Plus Free!
Script names tablelist.sql – tables in query tablestats.sql – table level statistics indexstats.sql – index level statistics indexcolumns.sql – columns in indexes columnstats.sql – column level stats histograms.sql – histogram details all.sql – runs all of the above
Example Table TEST – 2,000,001 rows Columns A, B – NUMBER 1,000,000 rows (1,1) 1,000,000 rows (2,2) 1 row (1,2) Query where condition A=1, B=2 chooses the one row
tablelist.sql Edit to include owner, table_name for each table in problem SQL May have to expand views to get base tables Can also get table names from plan
tablestats.sql OWNER TABLE_NAME NUM_ROWS BLOCKS AVG_ROW_LEN SAMPLE_SIZE LAST_ANALYZED
indexstats.sql TABLE_OWNER TABLE_NAME INDEX_NAME NUM_ROWS SAMPLE_SIZE CLUSTERING_FACTOR LAST_ANALYZED LEAF_BLOCKS DISTINCT_KEYS AVG_LEAF_BLOCKS_PER_KEY AVG_DATA_BLOCKS_PER_KEY BLEVEL
indexcolumns.sql TABLE_NAME INDEX_NAME COLUMN_NAME COLUMN_EXPRESSION
columnstats.sql TABLE_NAME COLUMN_NAME LO HI NUM_DISTINCT NUM_BUCKETS DENSITY NUM_NULLS AVG_COL_LEN LAST_ANALYZED SAMPLE_SIZE
histograms.sql HISTOGRAM TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER ENDPOINT_VALUE ENDPOINT_ACTUAL_VALUE
References sqltuning.zip - My talk on cardinality issues sqltuning.zip Cost Based Optimizer Fundamentals, Jonathan Lewis Metalink Note: , Limitations of the Oracle Cost Based Optimizer Metalink Note: , Predicate Selectivity Histograms – Myths and Facts, Wolfgang Breitling, Select Journal, Volume 13, Number 3
SQL Tuning Scripts Bobby Durrett US Foodservice