SlideShare a Scribd company logo
MythBusters - performance tuning 101 Paul Guerin Bachelor of Engineering (Computer) Oracle DBA Certified Professional DBA 10+ years Employers: Origin Energy, Bluescope Steel, BHP Billiton 21 November 2007
Topics Internet references Basics Cost Based Optimiser Cost Based Optimiser statistics Constraints Sorting Indexes SQL tuning and subquery factoring Partitions Partition pruning Coding practices Materialised Views and Parallelisation Other factors influencing performance Import and Export utilities
Internet references Amongst the best reference material is from Oracle: http://asktom.oracle.com http://tahiti.oracle.com http://www.oracle.com/technology/pub/articles/tech_dba.html Numerous other sources on the internet. http://www.mga.com.au http://www.dbspecialists.com http://www.hotsos.com http://www.singingsql.com http://www.oaktable.net/
Basics “ The objective of tuning a system is either to reduce the response time for end users of the system, or to reduce the resources used to process the same work.” The general goals in order are: Minimise the workload (i.e. SQL tuning). Minimise logical I/O (which in turn reduces physical I/O). Minimise sorting. Minimise output returned. Balance the workload. Execute jobs outside normal business hours (including materialised views). Parallelise the workload.
Cost Based Optimiser The database (Oracle, DB2, SQLserver) features an optimiser that predicts the quickest way that the data will be processed. The Cost Based Optimiser (CBO) supersedes the Rule Based Optimiser (RBO). Unlike the RBO, the CBO is not influenced by the order of tables and predicates in the SQL statement. For each SQL statement several execution plans are generated, and the execution plan estimated to have the lowest cost is chosen. The lowest cost execution plan may be comprised of index accesses or full table scans. View the lowest cost execution plan with: explain plan, autotrace, tkprof utility, or v$sql_plan.
Cost Based Optimiser Hints are available to influence the execution plan for an SQL statement. Every database we manage has an override on the nologging hint /*+ APPEND */ to allow: Recovery from a hot backup. Dataguard to function correctly.
Cost Based Optimiser statistics Statistics gathered by the DBMS_STATS package: System: average read time, average number of CPU cycles/sec, max I/O throughput. Table and columns: Number of rows, average row length, number distinct values Index: Average leaf blocks per key, average data blocks per key. Myth: Old statistics always lead to poor performance. Fact: If the data distribution of the object has not changed then the old statistics remain accurate. Myth: Recent statistics always lead to improved performance. Fact: If the data distribution of the object changes after the statistics are gathered then the new statistics become inaccurate.  e.g1. gather statistics, then truncate the table. e.g2. truncate the table, gather statistics, insert rows. Fact: If the data distribution of the object has not changed then the new statistics will not lead to the CBO to choose a more efficient execution plan. Note1: Cached SQL is invalidated every time new object statistics are gathered. Note2: ANALYZE TABLE|INDEX will not gather CBO stats in future Oracle releases. Note3: Specify the MONITORING clause when creating tables.
Constraints CBO uses contraints to determine the best execution plans. e.g. use an index with the correct sort instead of sorting from scratch. Use column constraints instead of constraining in the code. Not Null (all columns should be Not Null constrained unless nulls are legitimate) Check Foreign key Primary Note: Create the index before creating the column constraint. Format for constraint: <tablename>_<columnname>_<constraintmnemonic> TIME_PK = PRIMARY KEY for the TIME table. TIM_DAY_NAME_NN = NOT NULL constraint for the TIME table DAY_NAME column. CUSTOMERS_COUNTRY_FK = FOREIGN KEY constraint for the COUNTRIES table COUNTRY_ID column.
Sorting Best practice is to avoid unnecessary sorting. Be aware of implicit sorting using the following clauses: DISTINCT, GROUP BY, UNION Prefer to use a UNION ALL clause instead of a UNION clause. UNION clause: append result sets together then remove duplicates via a sort. UNION ALL clause: only append result sets together – no sorting. If a sort is explicitly required then use an ORDER BY clause. Use ROWNUM to retrieve only the top rows of the sort: SELECT * FROM (SELECT … FROM … ORDER BY …) WHERE rownum<=5; Note: In 9i, the GROUP BY will implicitly sort in the GROUP BY column order where an appropriate index does not exist. However in 10g the GROUP BY will not sort implicitly where an appropriate index does not exist. Indexes can be created to minimise the workload by avoiding a sort….
Indexes Two main types: B-tree non-unique ascending (default) – does not index NULL values. Bitmap – does index NULL values. A b-tree index is often used to enforce data integrity. e.g. Primary key (or unique key) constraints. However a b-tree index may also improve SQL performance by reducing logical I/O and eliminating sorting. Foreign key - important if deleting or updating the primary key of the parent table. Predicate of a WHERE clause. ORDER BY clause providing the index is in the same sort order (ASC or DESC). GROUP BY clause providing the index is in the same sort order. MIN() or MAX() function: index can be ascending or descending (key can be anywhere in concatenated index).
Indexes Indexes to create will depend on how the data is accessed. Single column index ideally for predicates separated by OR operators. Concatenated column index ideally for predicates separated by AND operators: Leading column of the index must match a predicate for the index to be used. Table access is eliminated where the index includes all the columns in the SELECT and WHERE clauses. Column order dependent on WHERE clause: most selective predicates first (generally equalities before ranges). Function-based indexes CREATE INDEX income_ix ON employees(salary + (salary*commission_pct)); CREATE UNIQUE INDEX t_idx ON t ( CASE WHEN source_id IS NOT NULL THEN source_id END, CASE WHEN source_id IS NULL THEN name END );
Indexes Bitmap index (single or concatenated): Columns of low cardinality. i.e. few unique values. Good for the COUNT() function. Performs poorly when column experiences high DML.
Indexes Oracle recommends that unique indexes be created explicitly, and not through enabling a unique constraint on a table (Concepts pg10-30) Use the following naming format for: primary key index: <tablename>_PK unique index: <tablename>_<columnname>_UK non-unique index: <tablename>_<columnname>_IX bitmap index: <tablename>_<columnname>_BIX Note: Don’t unnecessarily create indexes as each one reduces performance for INSERTS, DELETES, and UPDATES of the indexed column.
SQL tuning Minimise the workload: Use a WHERE clause. Improve the selectivity of the WHERE clause. A selective predicate should result in an index access (if available). A non-selective predicate should result in a full table scan. When joining: Add predicates to eliminate many-to-many joins. Remove all unreferenced objects from the FROM clause. GROUP BY, DISTINCT, UNION – all can hide a cartesian product. Minimise rows returned: Do not use a wildcard (*) in the SELECT clause. Use the ROWNUM pseudo column to limit the result set returned. Only update when the value has changed. The 2 nd  statement below is potentially faster. UPDATE TABLE … SET x=0; UPDATE TABLE … SET x=0 WHERE x<0 OR x>0; SQL that use the following are not able to use an index: Functions, including implicit type conversions (except if a function-based index exists) Leading wildcard character (%). NULL, <>, !=. SQL that use the following are able to use an index: =, >, <, LIKE, IN, ||, +, NVL Use >-9.99*POWER(10,125) instead of IS NOT NULL. Trailing wildcard character (%). Conditions that compare columns with constants. e.g. use salary>2000 instead of salary*12>24000
SQL tuning …  WHERE SUBSTR(ACCOUNT_NAME,1,7) = 'CAPITAL'; -- Full-table scan only: function on the indexed column. …  WHERE ACCOUNT_NAME LIKE 'CAPITAL%'; -- Index access possible. …  WHERE TRUNC(TRANS_DATE) = TRUNC(SYSDATE); -- Full-table scan only: function …  WHERE TRANS_DATE BETWEEN TRUNC(SYSDATE) AND TRUNC(SYSDATE) + .99999; -- Index access possible. …  WHERE ACCOUNT_NAME || ACCOUNT_TYPE = 'AMEXA';  -- Full-table scan only: operator with the indexed column. …  WHERE ACCOUNT_NAME='AMEX' AND ACCOUNT_TYPE = 'A‘; -- Index access possible. …  WHERE AMOUNT + 3000 < 5000; -- Full-table scan only: operator with the indexed column. …  WHERE AMOUNT < 2000;   -- Index access possible.
SQL tuning …  WHERE SUBSTR(ACCOUNT_NAME,1,7) = 'CAPITAL'; -- Full-table scan only: function on the indexed column. …  WHERE salary*12 > 24000; -- Full-table scan only: operator with the indexed column. …  WHERE salary > 24000/12; -- Index access possible as automatically simplified to: salary > 2000 …  WHERE salary > 2000; -- Index access possible. …  WHERE contract != 0; -- Full-table scan only: inequality used. …  WHERE contract < 0 OR contract > 0; -- Index access possible. …  WHERE status != ‘TRUE’; …  WHERE status < ‘TRUE’ OR status > ‘TRUE’; -- Index access possible. …  WHERE EMP_TYPE = ‘123’;  -- Full-table scan only if implicit type casting occurs. …  WHERE EMP_TYPE = 123;  --
SQL tuning The identical parts of the following DML…. SELECT count(*) FROM all_objects, (select distinct owner username from all_objects ) owners WHERE all_objects.owner = owners.username UNION ALL SELECT count(*) FROM dba_objects, (select distinct owner username from all_objects ) owners WHERE dba_objects.owner = owners.username; … . Are placed in the WITH clause (subquery factoring): WITH owners AS ( select distinct owner username from all_objects ) SELECT count(*) FROM all_objects, owners WHERE all_objects.owner = owners.username UNION ALL SELECT count(*) FROM dba_objects, owners WHERE dba_objects.owner = owners.username;
Partitions A partitioned table can have partitioned or non-partitioned indexes. A non-partitioned table can have partitioned or non-partitioned indexes. A partition can have sub-partitions. Partitioned and non-partitioned index options: Local partitioned index on a partition of a table. Global partitioned index on any or all partitions of a table, and the index partitions can be independent of the partitioned table. e.g. global partitioned index on contract ID column of a partitioned table on date column. Global index on a whole table. i.e. simply an ordinary index.
Partition Pruning Partition pruning is the skipping of unnecessary index and data partitions or subpartitions in a query. Note: the optimizer cannot prune partitions if the SQL statement applies a function to the partitioning column (with the exception of the TO_DATE function). Similarly, the optimizer cannot use an index if the SQL statement applies a function to the indexed column, unless it is a function-based index.
Partition Pruning SELECT * FROM tx.hh_rtl WHERE day BETWEEN '01-OCT-2007' AND ‘31-OCT-2007';  -- Note: day is not a leading column of an index ----------------------------------------------------------------------------------------------------------- | Id  | Operation  |  Name  | Rows  | Bytes | Cost (%CPU)|  Pstart |  Pstop  | ----------------------------------------------------------------------------------------------------------- |  0 | SELECT STATEMENT  |  |  1158 | 45162 |  399K  (1)|  |  | |  1 |  PARTITION RANGE ITERATOR  |  |  |  |  |  KEY  |  KEY  | |  2 |  TABLE ACCESS BY LOCAL INDEX ROWID| HH_RTL  |  1158 | 45162 |  399K  (1)|  KEY  |  KEY  | |*  3 |  INDEX SKIP SCAN  | HH_RTL$IDX2  |  901 |  |  1374  (4)|  KEY  |  KEY  | ----------------------------------------------------------------------------------------------------------- SELECT * FROM tx.hh_rtl WHERE day BETWEEN TO_DATE('01-10-2007','dd-mm-yyyy') AND TO_DATE('31-10-2007','dd-mm-yyyy'); ----------------------------------------------------------------------------------------- | Id  | Operation  |  Name  | Rows  | Bytes | Cost (%CPU)|  Pstart |  Pstop  | ----------------------------------------------------------------------------------------- |  0 | SELECT STATEMENT  |  | 60860 |  2317K| 11082  (1)|  |  | |*  1 |  TABLE ACCESS FULL  | HH_RTL  | 60860 |  2317K| 11082  (1)|  100  |  100  | ----------------------------------------------------------------------------------------- SELECT * FROM tx.hh_rtl WHERE cnt_id=1;  -- Note: tx.hh_rtl is partitioned by day --------------------------------------------------------------------------------------------------------- | Id  | Operation  |  Name  | Rows  | Bytes | Cost (%CPU)|  Pstart |  Pstop  | --------------------------------------------------------------------------------------------------------- |  0 | SELECT STATEMENT  |  |  643 | 25077 |  342  (1)|  |  | |  1 |  PARTITION RANGE ALL  |  |  |  |  |  1  |  163  | |  2 |  TABLE ACCESS BY LOCAL INDEX ROWID| HH_RTL  |  643 | 25077 |  342  (1)|  1  |  163  | |*  3 |  INDEX RANGE SCAN  | PK_HH_RTL  |  643 |  |  332  (1)|  1  |  163  | ---------------------------------------------------------------------------------------------------------
Coding practices Use static SQL (preferably implicit) whenever possible. Also static PL/SQL already bind results in parse once, reuse many times: INSERT INTO t VALUES ( i ); Minimise SQL parsing by using bind variables in dynamic PL/SQL: Native dynamic SQL without binds results in a full hard parse every time: EXECUTE IMMEDIATE ‘INSERT INTO t VALUES (' || i || ')'; Native dynamic SQL with binds results in a hard parse but reuse execution plan: EXECUTE IMMEDIATE ‘INSERT INTO t VALUES (:x)'  USING  i; Use non-native dynamic SQL (i.e. DBMS_SQL package) over native (i.e. execute immediate) if executing the dynamic statement repeatedly. Use arrays over DBMS_SQL. Move SQL out of triggers into procedures. To reduce parsing: move the SQL into a package, and call the package from the trigger.
Coding practices Do as a single SQL statement, instead of procedurally. Use an exception instead of first checking for the existence of data i.e. count(*)>1. Commit only at transaction boundaries. e.g. at end of procedure. Sequences: Sequences eliminate serialization and improve the concurrency of your application. When an application accesses a sequence in the sequence cache, the sequence numbers are read quickly. Synonyms are for end users not for application schemas.
Coding practices - monitoring Label at start of transaction to identify entry in v$transaction: SET TRANSACTION NAME '<string>'; Populate the v$session and v$sql dynamic views: Module: dbms_application_info.set_module() Action: dbms_application_info.set_action() Client: dbms_application_info.set_client_info() Populate the v$session_longops dynamic view: dbms_application_info.set_session_longops()
Materialised Views and Parallelisation Remember the rules: Minimise the workload. Minimise logical I/O (which in turn reduces physical I/O). Minimise sorting. Minimise output returned. Balance the workload. Execute jobs outside normal business hours (including materialised views). Parallelise the workload. Materialised views do not minimise the workload, so are not a substitute for SQL tuning. Parallel processing does not minimise the workload, so is not a substitute for SQL tuning. Additionally, parallel processing is resource intensive which may impact other users.
Other factors influencing performance Larger blocksizes result in more efficient data reads. Maximum block size for Windows is 16kbytes. Cached data (logical I/O) is accessed quicker than from disk (physical I/O). Database performance is often slower after a cold backup. Row locking. Library cache latching. Object types other than tables and indexes can improve performance: Clustered objects. Index Organised Tables.
Other factors influencing performance Full table scans are influenced by the high water mark. Deleting or truncating does not reset the high water mark. Move the table to reset the high water mark. Note: Resetting the high water mark does not free space to the operating system. i.e. moving the table does not free space to the operating system.
Export and Import utilities Distributed database links are appropriate for small data migrations but don’t migrate metadata. Export and import utilities are best for migrating data and metadata. Export and import objects: Tables with all or some rows Tables without rows Indexes Grants Triggers Constraints Also available: Transportable tablespaces for migrating large data quickly. RMAN duplicates for migrating huge data quickly.
Minimise the workload by: Create indexes that reduce I/O and sorting. Partition large tables and indexes. Write SQL statements to permit index accesses and partition pruning. Use column constraints – especially Not Null. Use DBMS_STATS instead of ANALYZE. Only use materialised views and parallel processing as a last resort.

More Related Content

What's hot

SQL Views
SQL ViewsSQL Views
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
Dhananjay Goel
 
Oracle SQL Advanced
Oracle SQL AdvancedOracle SQL Advanced
Oracle SQL Advanced
Dhananjay Goel
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
Swapnali Pawar
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
Manikanda kumar
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
Alessandro Baratella
 
Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6
MYXPLAIN
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding Guidelines
Chris Adkin
 
Webinar 2013 advanced_query_tuning
Webinar 2013 advanced_query_tuningWebinar 2013 advanced_query_tuning
Webinar 2013 advanced_query_tuning
晓 周
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
Swapnali Pawar
 
Sequences and indexes
Sequences and indexesSequences and indexes
Sequences and indexes
Balqees Al.Mubarak
 
Optimizing queries MySQL
Optimizing queries MySQLOptimizing queries MySQL
Optimizing queries MySQL
Georgi Sotirov
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
Vikas Gupta
 
Mysql cheatsheet
Mysql cheatsheetMysql cheatsheet
Mysql cheatsheet
Adolfo Nasol
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
Amrit Kaur
 
MySQL index optimization techniques
MySQL index optimization techniquesMySQL index optimization techniques
MySQL index optimization techniques
kumar gaurav
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
Achmad Solichin
 
Oracle views
Oracle viewsOracle views
Oracle views
Balqees Al.Mubarak
 
Oraclesql
OraclesqlOraclesql
Oraclesql
Priya Goyal
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle Sql
Ahmed Yaseen
 

What's hot (20)

SQL Views
SQL ViewsSQL Views
SQL Views
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
Oracle SQL Advanced
Oracle SQL AdvancedOracle SQL Advanced
Oracle SQL Advanced
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding Guidelines
 
Webinar 2013 advanced_query_tuning
Webinar 2013 advanced_query_tuningWebinar 2013 advanced_query_tuning
Webinar 2013 advanced_query_tuning
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
Sequences and indexes
Sequences and indexesSequences and indexes
Sequences and indexes
 
Optimizing queries MySQL
Optimizing queries MySQLOptimizing queries MySQL
Optimizing queries MySQL
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
Mysql cheatsheet
Mysql cheatsheetMysql cheatsheet
Mysql cheatsheet
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
 
MySQL index optimization techniques
MySQL index optimization techniquesMySQL index optimization techniques
MySQL index optimization techniques
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
 
Oracle views
Oracle viewsOracle views
Oracle views
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle Sql
 

Viewers also liked

Explaining the explain_plan
Explaining the explain_planExplaining the explain_plan
Explaining the explain_plan
arief12H
 
In Sync11 Presentation The Biggest Loser
In Sync11 Presentation The Biggest LoserIn Sync11 Presentation The Biggest Loser
In Sync11 Presentation The Biggest Loser
paulguerin
 
Oracle Database 11g vs 12c
Oracle Database 11g vs 12cOracle Database 11g vs 12c
Oracle Database 11g vs 12c
Deiby Gómez
 
Oracle R12 EBS Performance Tuning
Oracle R12 EBS Performance TuningOracle R12 EBS Performance Tuning
Oracle R12 EBS Performance Tuning
Scott Jenner
 
Cooper Oracle 11g Overview
Cooper Oracle 11g OverviewCooper Oracle 11g Overview
Cooper Oracle 11g Overview
moin_azeem
 
All of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperAll of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL Developer
Jeff Smith
 
Cost Based Optimizer - Part 1 of 2
Cost Based Optimizer - Part 1 of 2Cost Based Optimizer - Part 1 of 2
Cost Based Optimizer - Part 1 of 2
Mahesh Vallampati
 
Ten query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should knowTen query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should know
Kevin Kline
 
Oracle RAC 12c Release 2 - Overview
Oracle RAC 12c Release 2 - OverviewOracle RAC 12c Release 2 - Overview
Oracle RAC 12c Release 2 - Overview
Markus Michalewicz
 

Viewers also liked (9)

Explaining the explain_plan
Explaining the explain_planExplaining the explain_plan
Explaining the explain_plan
 
In Sync11 Presentation The Biggest Loser
In Sync11 Presentation The Biggest LoserIn Sync11 Presentation The Biggest Loser
In Sync11 Presentation The Biggest Loser
 
Oracle Database 11g vs 12c
Oracle Database 11g vs 12cOracle Database 11g vs 12c
Oracle Database 11g vs 12c
 
Oracle R12 EBS Performance Tuning
Oracle R12 EBS Performance TuningOracle R12 EBS Performance Tuning
Oracle R12 EBS Performance Tuning
 
Cooper Oracle 11g Overview
Cooper Oracle 11g OverviewCooper Oracle 11g Overview
Cooper Oracle 11g Overview
 
All of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperAll of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL Developer
 
Cost Based Optimizer - Part 1 of 2
Cost Based Optimizer - Part 1 of 2Cost Based Optimizer - Part 1 of 2
Cost Based Optimizer - Part 1 of 2
 
Ten query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should knowTen query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should know
 
Oracle RAC 12c Release 2 - Overview
Oracle RAC 12c Release 2 - OverviewOracle RAC 12c Release 2 - Overview
Oracle RAC 12c Release 2 - Overview
 

Similar to Myth busters - performance tuning 101 2007

Sydney Oracle Meetup - indexes
Sydney Oracle Meetup - indexesSydney Oracle Meetup - indexes
Sydney Oracle Meetup - indexes
paulguerin
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
Nirav Shah
 
Ground Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_planGround Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_plan
Maria Colgan
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain Plan
Maria Colgan
 
Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008
paulguerin
 
Mysqlppt
MysqlpptMysqlppt
Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
Dave Stokes
 
Mysqlppt
MysqlpptMysqlppt
SQL Server 2008 Performance Enhancements
SQL Server 2008 Performance EnhancementsSQL Server 2008 Performance Enhancements
SQL Server 2008 Performance Enhancements
infusiondev
 
Oracle Join Methods and 12c Adaptive Plans
Oracle Join Methods and 12c Adaptive PlansOracle Join Methods and 12c Adaptive Plans
Oracle Join Methods and 12c Adaptive Plans
Franck Pachot
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
Reka
 
Application sql issues_and_tuning
Application sql issues_and_tuningApplication sql issues_and_tuning
Application sql issues_and_tuning
Anil Pandey
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
Abhishek590097
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersSQL Server 2008 Development for Programmers
SQL Server 2008 Development for Programmers
Adam Hutson
 
Sql tuning guideline
Sql tuning guidelineSql tuning guideline
Sql tuning guideline
Sidney Chen
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
Yanli Liu
 
Sq lite
Sq liteSq lite
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
Hemant Kumar Singh
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
InSync Conference
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
PavithSingh
 

Similar to Myth busters - performance tuning 101 2007 (20)

Sydney Oracle Meetup - indexes
Sydney Oracle Meetup - indexesSydney Oracle Meetup - indexes
Sydney Oracle Meetup - indexes
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Ground Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_planGround Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_plan
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain Plan
 
Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
SQL Server 2008 Performance Enhancements
SQL Server 2008 Performance EnhancementsSQL Server 2008 Performance Enhancements
SQL Server 2008 Performance Enhancements
 
Oracle Join Methods and 12c Adaptive Plans
Oracle Join Methods and 12c Adaptive PlansOracle Join Methods and 12c Adaptive Plans
Oracle Join Methods and 12c Adaptive Plans
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Application sql issues_and_tuning
Application sql issues_and_tuningApplication sql issues_and_tuning
Application sql issues_and_tuning
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersSQL Server 2008 Development for Programmers
SQL Server 2008 Development for Programmers
 
Sql tuning guideline
Sql tuning guidelineSql tuning guideline
Sql tuning guideline
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
 
Sq lite
Sq liteSq lite
Sq lite
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
 

Recently uploaded

Top 12 AI Technology Trends For 2024.pdf
Top 12 AI Technology Trends For 2024.pdfTop 12 AI Technology Trends For 2024.pdf
Top 12 AI Technology Trends For 2024.pdf
Marrie Morris
 
FIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptx
FIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptxFIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptx
FIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptx
FIDO Alliance
 
Cracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Cracking AI Black Box - Strategies for Customer-centric Enterprise ExcellenceCracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Cracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Quentin Reul
 
FIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Munich Seminar: FIDO Tech Principles.pptxFIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Alliance
 
Redefining Cybersecurity with AI Capabilities
Redefining Cybersecurity with AI CapabilitiesRedefining Cybersecurity with AI Capabilities
Redefining Cybersecurity with AI Capabilities
Priyanka Aash
 
What's New in Copilot for Microsoft 365 June 2024.pptx
What's New in Copilot for Microsoft 365 June 2024.pptxWhat's New in Copilot for Microsoft 365 June 2024.pptx
What's New in Copilot for Microsoft 365 June 2024.pptx
Stephanie Beckett
 
What's New in Teams Calling, Meetings, Devices June 2024
What's New in Teams Calling, Meetings, Devices June 2024What's New in Teams Calling, Meetings, Devices June 2024
What's New in Teams Calling, Meetings, Devices June 2024
Stephanie Beckett
 
Keynote : AI & Future Of Offensive Security
Keynote : AI & Future Of Offensive SecurityKeynote : AI & Future Of Offensive Security
Keynote : AI & Future Of Offensive Security
Priyanka Aash
 
NVIDIA at Breakthrough Discuss for Space Exploration
NVIDIA at Breakthrough Discuss for Space ExplorationNVIDIA at Breakthrough Discuss for Space Exploration
NVIDIA at Breakthrough Discuss for Space Exploration
Alison B. Lowndes
 
FIDO Munich Seminar: Biometrics and Passkeys for In-Vehicle Apps.pptx
FIDO Munich Seminar: Biometrics and Passkeys for In-Vehicle Apps.pptxFIDO Munich Seminar: Biometrics and Passkeys for In-Vehicle Apps.pptx
FIDO Munich Seminar: Biometrics and Passkeys for In-Vehicle Apps.pptx
FIDO Alliance
 
Demystifying Neural Networks And Building Cybersecurity Applications
Demystifying Neural Networks And Building Cybersecurity ApplicationsDemystifying Neural Networks And Building Cybersecurity Applications
Demystifying Neural Networks And Building Cybersecurity Applications
Priyanka Aash
 
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Zilliz
 
Scaling Vector Search: How Milvus Handles Billions+
Scaling Vector Search: How Milvus Handles Billions+Scaling Vector Search: How Milvus Handles Billions+
Scaling Vector Search: How Milvus Handles Billions+
Zilliz
 
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
Fwdays
 
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptxFIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Alliance
 
Retrieval Augmented Generation Evaluation with Ragas
Retrieval Augmented Generation Evaluation with RagasRetrieval Augmented Generation Evaluation with Ragas
Retrieval Augmented Generation Evaluation with Ragas
Zilliz
 
The History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal EmbeddingsThe History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal Embeddings
Zilliz
 
UiPath Community Day Amsterdam: Code, Collaborate, Connect
UiPath Community Day Amsterdam: Code, Collaborate, ConnectUiPath Community Day Amsterdam: Code, Collaborate, Connect
UiPath Community Day Amsterdam: Code, Collaborate, Connect
UiPathCommunity
 
Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024
siddu769252
 
Finetuning GenAI For Hacking and Defending
Finetuning GenAI For Hacking and DefendingFinetuning GenAI For Hacking and Defending
Finetuning GenAI For Hacking and Defending
Priyanka Aash
 

Recently uploaded (20)

Top 12 AI Technology Trends For 2024.pdf
Top 12 AI Technology Trends For 2024.pdfTop 12 AI Technology Trends For 2024.pdf
Top 12 AI Technology Trends For 2024.pdf
 
FIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptx
FIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptxFIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptx
FIDO Munich Seminar Blueprint for In-Vehicle Payment Standard.pptx
 
Cracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Cracking AI Black Box - Strategies for Customer-centric Enterprise ExcellenceCracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
Cracking AI Black Box - Strategies for Customer-centric Enterprise Excellence
 
FIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Munich Seminar: FIDO Tech Principles.pptxFIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Munich Seminar: FIDO Tech Principles.pptx
 
Redefining Cybersecurity with AI Capabilities
Redefining Cybersecurity with AI CapabilitiesRedefining Cybersecurity with AI Capabilities
Redefining Cybersecurity with AI Capabilities
 
What's New in Copilot for Microsoft 365 June 2024.pptx
What's New in Copilot for Microsoft 365 June 2024.pptxWhat's New in Copilot for Microsoft 365 June 2024.pptx
What's New in Copilot for Microsoft 365 June 2024.pptx
 
What's New in Teams Calling, Meetings, Devices June 2024
What's New in Teams Calling, Meetings, Devices June 2024What's New in Teams Calling, Meetings, Devices June 2024
What's New in Teams Calling, Meetings, Devices June 2024
 
Keynote : AI & Future Of Offensive Security
Keynote : AI & Future Of Offensive SecurityKeynote : AI & Future Of Offensive Security
Keynote : AI & Future Of Offensive Security
 
NVIDIA at Breakthrough Discuss for Space Exploration
NVIDIA at Breakthrough Discuss for Space ExplorationNVIDIA at Breakthrough Discuss for Space Exploration
NVIDIA at Breakthrough Discuss for Space Exploration
 
FIDO Munich Seminar: Biometrics and Passkeys for In-Vehicle Apps.pptx
FIDO Munich Seminar: Biometrics and Passkeys for In-Vehicle Apps.pptxFIDO Munich Seminar: Biometrics and Passkeys for In-Vehicle Apps.pptx
FIDO Munich Seminar: Biometrics and Passkeys for In-Vehicle Apps.pptx
 
Demystifying Neural Networks And Building Cybersecurity Applications
Demystifying Neural Networks And Building Cybersecurity ApplicationsDemystifying Neural Networks And Building Cybersecurity Applications
Demystifying Neural Networks And Building Cybersecurity Applications
 
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
 
Scaling Vector Search: How Milvus Handles Billions+
Scaling Vector Search: How Milvus Handles Billions+Scaling Vector Search: How Milvus Handles Billions+
Scaling Vector Search: How Milvus Handles Billions+
 
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
 
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptxFIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
 
Retrieval Augmented Generation Evaluation with Ragas
Retrieval Augmented Generation Evaluation with RagasRetrieval Augmented Generation Evaluation with Ragas
Retrieval Augmented Generation Evaluation with Ragas
 
The History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal EmbeddingsThe History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal Embeddings
 
UiPath Community Day Amsterdam: Code, Collaborate, Connect
UiPath Community Day Amsterdam: Code, Collaborate, ConnectUiPath Community Day Amsterdam: Code, Collaborate, Connect
UiPath Community Day Amsterdam: Code, Collaborate, Connect
 
Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024
 
Finetuning GenAI For Hacking and Defending
Finetuning GenAI For Hacking and DefendingFinetuning GenAI For Hacking and Defending
Finetuning GenAI For Hacking and Defending
 

Myth busters - performance tuning 101 2007

  • 1. MythBusters - performance tuning 101 Paul Guerin Bachelor of Engineering (Computer) Oracle DBA Certified Professional DBA 10+ years Employers: Origin Energy, Bluescope Steel, BHP Billiton 21 November 2007
  • 2. Topics Internet references Basics Cost Based Optimiser Cost Based Optimiser statistics Constraints Sorting Indexes SQL tuning and subquery factoring Partitions Partition pruning Coding practices Materialised Views and Parallelisation Other factors influencing performance Import and Export utilities
  • 3. Internet references Amongst the best reference material is from Oracle: http://asktom.oracle.com http://tahiti.oracle.com http://www.oracle.com/technology/pub/articles/tech_dba.html Numerous other sources on the internet. http://www.mga.com.au http://www.dbspecialists.com http://www.hotsos.com http://www.singingsql.com http://www.oaktable.net/
  • 4. Basics “ The objective of tuning a system is either to reduce the response time for end users of the system, or to reduce the resources used to process the same work.” The general goals in order are: Minimise the workload (i.e. SQL tuning). Minimise logical I/O (which in turn reduces physical I/O). Minimise sorting. Minimise output returned. Balance the workload. Execute jobs outside normal business hours (including materialised views). Parallelise the workload.
  • 5. Cost Based Optimiser The database (Oracle, DB2, SQLserver) features an optimiser that predicts the quickest way that the data will be processed. The Cost Based Optimiser (CBO) supersedes the Rule Based Optimiser (RBO). Unlike the RBO, the CBO is not influenced by the order of tables and predicates in the SQL statement. For each SQL statement several execution plans are generated, and the execution plan estimated to have the lowest cost is chosen. The lowest cost execution plan may be comprised of index accesses or full table scans. View the lowest cost execution plan with: explain plan, autotrace, tkprof utility, or v$sql_plan.
  • 6. Cost Based Optimiser Hints are available to influence the execution plan for an SQL statement. Every database we manage has an override on the nologging hint /*+ APPEND */ to allow: Recovery from a hot backup. Dataguard to function correctly.
  • 7. Cost Based Optimiser statistics Statistics gathered by the DBMS_STATS package: System: average read time, average number of CPU cycles/sec, max I/O throughput. Table and columns: Number of rows, average row length, number distinct values Index: Average leaf blocks per key, average data blocks per key. Myth: Old statistics always lead to poor performance. Fact: If the data distribution of the object has not changed then the old statistics remain accurate. Myth: Recent statistics always lead to improved performance. Fact: If the data distribution of the object changes after the statistics are gathered then the new statistics become inaccurate. e.g1. gather statistics, then truncate the table. e.g2. truncate the table, gather statistics, insert rows. Fact: If the data distribution of the object has not changed then the new statistics will not lead to the CBO to choose a more efficient execution plan. Note1: Cached SQL is invalidated every time new object statistics are gathered. Note2: ANALYZE TABLE|INDEX will not gather CBO stats in future Oracle releases. Note3: Specify the MONITORING clause when creating tables.
  • 8. Constraints CBO uses contraints to determine the best execution plans. e.g. use an index with the correct sort instead of sorting from scratch. Use column constraints instead of constraining in the code. Not Null (all columns should be Not Null constrained unless nulls are legitimate) Check Foreign key Primary Note: Create the index before creating the column constraint. Format for constraint: <tablename>_<columnname>_<constraintmnemonic> TIME_PK = PRIMARY KEY for the TIME table. TIM_DAY_NAME_NN = NOT NULL constraint for the TIME table DAY_NAME column. CUSTOMERS_COUNTRY_FK = FOREIGN KEY constraint for the COUNTRIES table COUNTRY_ID column.
  • 9. Sorting Best practice is to avoid unnecessary sorting. Be aware of implicit sorting using the following clauses: DISTINCT, GROUP BY, UNION Prefer to use a UNION ALL clause instead of a UNION clause. UNION clause: append result sets together then remove duplicates via a sort. UNION ALL clause: only append result sets together – no sorting. If a sort is explicitly required then use an ORDER BY clause. Use ROWNUM to retrieve only the top rows of the sort: SELECT * FROM (SELECT … FROM … ORDER BY …) WHERE rownum<=5; Note: In 9i, the GROUP BY will implicitly sort in the GROUP BY column order where an appropriate index does not exist. However in 10g the GROUP BY will not sort implicitly where an appropriate index does not exist. Indexes can be created to minimise the workload by avoiding a sort….
  • 10. Indexes Two main types: B-tree non-unique ascending (default) – does not index NULL values. Bitmap – does index NULL values. A b-tree index is often used to enforce data integrity. e.g. Primary key (or unique key) constraints. However a b-tree index may also improve SQL performance by reducing logical I/O and eliminating sorting. Foreign key - important if deleting or updating the primary key of the parent table. Predicate of a WHERE clause. ORDER BY clause providing the index is in the same sort order (ASC or DESC). GROUP BY clause providing the index is in the same sort order. MIN() or MAX() function: index can be ascending or descending (key can be anywhere in concatenated index).
  • 11. Indexes Indexes to create will depend on how the data is accessed. Single column index ideally for predicates separated by OR operators. Concatenated column index ideally for predicates separated by AND operators: Leading column of the index must match a predicate for the index to be used. Table access is eliminated where the index includes all the columns in the SELECT and WHERE clauses. Column order dependent on WHERE clause: most selective predicates first (generally equalities before ranges). Function-based indexes CREATE INDEX income_ix ON employees(salary + (salary*commission_pct)); CREATE UNIQUE INDEX t_idx ON t ( CASE WHEN source_id IS NOT NULL THEN source_id END, CASE WHEN source_id IS NULL THEN name END );
  • 12. Indexes Bitmap index (single or concatenated): Columns of low cardinality. i.e. few unique values. Good for the COUNT() function. Performs poorly when column experiences high DML.
  • 13. Indexes Oracle recommends that unique indexes be created explicitly, and not through enabling a unique constraint on a table (Concepts pg10-30) Use the following naming format for: primary key index: <tablename>_PK unique index: <tablename>_<columnname>_UK non-unique index: <tablename>_<columnname>_IX bitmap index: <tablename>_<columnname>_BIX Note: Don’t unnecessarily create indexes as each one reduces performance for INSERTS, DELETES, and UPDATES of the indexed column.
  • 14. SQL tuning Minimise the workload: Use a WHERE clause. Improve the selectivity of the WHERE clause. A selective predicate should result in an index access (if available). A non-selective predicate should result in a full table scan. When joining: Add predicates to eliminate many-to-many joins. Remove all unreferenced objects from the FROM clause. GROUP BY, DISTINCT, UNION – all can hide a cartesian product. Minimise rows returned: Do not use a wildcard (*) in the SELECT clause. Use the ROWNUM pseudo column to limit the result set returned. Only update when the value has changed. The 2 nd statement below is potentially faster. UPDATE TABLE … SET x=0; UPDATE TABLE … SET x=0 WHERE x<0 OR x>0; SQL that use the following are not able to use an index: Functions, including implicit type conversions (except if a function-based index exists) Leading wildcard character (%). NULL, <>, !=. SQL that use the following are able to use an index: =, >, <, LIKE, IN, ||, +, NVL Use >-9.99*POWER(10,125) instead of IS NOT NULL. Trailing wildcard character (%). Conditions that compare columns with constants. e.g. use salary>2000 instead of salary*12>24000
  • 15. SQL tuning … WHERE SUBSTR(ACCOUNT_NAME,1,7) = 'CAPITAL'; -- Full-table scan only: function on the indexed column. … WHERE ACCOUNT_NAME LIKE 'CAPITAL%'; -- Index access possible. … WHERE TRUNC(TRANS_DATE) = TRUNC(SYSDATE); -- Full-table scan only: function … WHERE TRANS_DATE BETWEEN TRUNC(SYSDATE) AND TRUNC(SYSDATE) + .99999; -- Index access possible. … WHERE ACCOUNT_NAME || ACCOUNT_TYPE = 'AMEXA'; -- Full-table scan only: operator with the indexed column. … WHERE ACCOUNT_NAME='AMEX' AND ACCOUNT_TYPE = 'A‘; -- Index access possible. … WHERE AMOUNT + 3000 < 5000; -- Full-table scan only: operator with the indexed column. … WHERE AMOUNT < 2000; -- Index access possible.
  • 16. SQL tuning … WHERE SUBSTR(ACCOUNT_NAME,1,7) = 'CAPITAL'; -- Full-table scan only: function on the indexed column. … WHERE salary*12 > 24000; -- Full-table scan only: operator with the indexed column. … WHERE salary > 24000/12; -- Index access possible as automatically simplified to: salary > 2000 … WHERE salary > 2000; -- Index access possible. … WHERE contract != 0; -- Full-table scan only: inequality used. … WHERE contract < 0 OR contract > 0; -- Index access possible. … WHERE status != ‘TRUE’; … WHERE status < ‘TRUE’ OR status > ‘TRUE’; -- Index access possible. … WHERE EMP_TYPE = ‘123’; -- Full-table scan only if implicit type casting occurs. … WHERE EMP_TYPE = 123; --
  • 17. SQL tuning The identical parts of the following DML…. SELECT count(*) FROM all_objects, (select distinct owner username from all_objects ) owners WHERE all_objects.owner = owners.username UNION ALL SELECT count(*) FROM dba_objects, (select distinct owner username from all_objects ) owners WHERE dba_objects.owner = owners.username; … . Are placed in the WITH clause (subquery factoring): WITH owners AS ( select distinct owner username from all_objects ) SELECT count(*) FROM all_objects, owners WHERE all_objects.owner = owners.username UNION ALL SELECT count(*) FROM dba_objects, owners WHERE dba_objects.owner = owners.username;
  • 18. Partitions A partitioned table can have partitioned or non-partitioned indexes. A non-partitioned table can have partitioned or non-partitioned indexes. A partition can have sub-partitions. Partitioned and non-partitioned index options: Local partitioned index on a partition of a table. Global partitioned index on any or all partitions of a table, and the index partitions can be independent of the partitioned table. e.g. global partitioned index on contract ID column of a partitioned table on date column. Global index on a whole table. i.e. simply an ordinary index.
  • 19. Partition Pruning Partition pruning is the skipping of unnecessary index and data partitions or subpartitions in a query. Note: the optimizer cannot prune partitions if the SQL statement applies a function to the partitioning column (with the exception of the TO_DATE function). Similarly, the optimizer cannot use an index if the SQL statement applies a function to the indexed column, unless it is a function-based index.
  • 20. Partition Pruning SELECT * FROM tx.hh_rtl WHERE day BETWEEN '01-OCT-2007' AND ‘31-OCT-2007'; -- Note: day is not a leading column of an index ----------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Pstart | Pstop | ----------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1158 | 45162 | 399K (1)| | | | 1 | PARTITION RANGE ITERATOR | | | | | KEY | KEY | | 2 | TABLE ACCESS BY LOCAL INDEX ROWID| HH_RTL | 1158 | 45162 | 399K (1)| KEY | KEY | |* 3 | INDEX SKIP SCAN | HH_RTL$IDX2 | 901 | | 1374 (4)| KEY | KEY | ----------------------------------------------------------------------------------------------------------- SELECT * FROM tx.hh_rtl WHERE day BETWEEN TO_DATE('01-10-2007','dd-mm-yyyy') AND TO_DATE('31-10-2007','dd-mm-yyyy'); ----------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Pstart | Pstop | ----------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 60860 | 2317K| 11082 (1)| | | |* 1 | TABLE ACCESS FULL | HH_RTL | 60860 | 2317K| 11082 (1)| 100 | 100 | ----------------------------------------------------------------------------------------- SELECT * FROM tx.hh_rtl WHERE cnt_id=1; -- Note: tx.hh_rtl is partitioned by day --------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Pstart | Pstop | --------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 643 | 25077 | 342 (1)| | | | 1 | PARTITION RANGE ALL | | | | | 1 | 163 | | 2 | TABLE ACCESS BY LOCAL INDEX ROWID| HH_RTL | 643 | 25077 | 342 (1)| 1 | 163 | |* 3 | INDEX RANGE SCAN | PK_HH_RTL | 643 | | 332 (1)| 1 | 163 | ---------------------------------------------------------------------------------------------------------
  • 21. Coding practices Use static SQL (preferably implicit) whenever possible. Also static PL/SQL already bind results in parse once, reuse many times: INSERT INTO t VALUES ( i ); Minimise SQL parsing by using bind variables in dynamic PL/SQL: Native dynamic SQL without binds results in a full hard parse every time: EXECUTE IMMEDIATE ‘INSERT INTO t VALUES (' || i || ')'; Native dynamic SQL with binds results in a hard parse but reuse execution plan: EXECUTE IMMEDIATE ‘INSERT INTO t VALUES (:x)' USING i; Use non-native dynamic SQL (i.e. DBMS_SQL package) over native (i.e. execute immediate) if executing the dynamic statement repeatedly. Use arrays over DBMS_SQL. Move SQL out of triggers into procedures. To reduce parsing: move the SQL into a package, and call the package from the trigger.
  • 22. Coding practices Do as a single SQL statement, instead of procedurally. Use an exception instead of first checking for the existence of data i.e. count(*)>1. Commit only at transaction boundaries. e.g. at end of procedure. Sequences: Sequences eliminate serialization and improve the concurrency of your application. When an application accesses a sequence in the sequence cache, the sequence numbers are read quickly. Synonyms are for end users not for application schemas.
  • 23. Coding practices - monitoring Label at start of transaction to identify entry in v$transaction: SET TRANSACTION NAME '<string>'; Populate the v$session and v$sql dynamic views: Module: dbms_application_info.set_module() Action: dbms_application_info.set_action() Client: dbms_application_info.set_client_info() Populate the v$session_longops dynamic view: dbms_application_info.set_session_longops()
  • 24. Materialised Views and Parallelisation Remember the rules: Minimise the workload. Minimise logical I/O (which in turn reduces physical I/O). Minimise sorting. Minimise output returned. Balance the workload. Execute jobs outside normal business hours (including materialised views). Parallelise the workload. Materialised views do not minimise the workload, so are not a substitute for SQL tuning. Parallel processing does not minimise the workload, so is not a substitute for SQL tuning. Additionally, parallel processing is resource intensive which may impact other users.
  • 25. Other factors influencing performance Larger blocksizes result in more efficient data reads. Maximum block size for Windows is 16kbytes. Cached data (logical I/O) is accessed quicker than from disk (physical I/O). Database performance is often slower after a cold backup. Row locking. Library cache latching. Object types other than tables and indexes can improve performance: Clustered objects. Index Organised Tables.
  • 26. Other factors influencing performance Full table scans are influenced by the high water mark. Deleting or truncating does not reset the high water mark. Move the table to reset the high water mark. Note: Resetting the high water mark does not free space to the operating system. i.e. moving the table does not free space to the operating system.
  • 27. Export and Import utilities Distributed database links are appropriate for small data migrations but don’t migrate metadata. Export and import utilities are best for migrating data and metadata. Export and import objects: Tables with all or some rows Tables without rows Indexes Grants Triggers Constraints Also available: Transportable tablespaces for migrating large data quickly. RMAN duplicates for migrating huge data quickly.
  • 28. Minimise the workload by: Create indexes that reduce I/O and sorting. Partition large tables and indexes. Write SQL statements to permit index accesses and partition pruning. Use column constraints – especially Not Null. Use DBMS_STATS instead of ANALYZE. Only use materialised views and parallel processing as a last resort.