SQL Tuning Ohio Oracle User’s Group October 2002 © Copyright, Kris T. Mason, 2002.

Slides:



Advertisements
Similar presentations
Tuning Oracle SQL The Basics of Efficient SQLThe Basics of Efficient SQL Common Sense Indexing The Optimizer –Making SQL Efficient Finding Problem Queries.
Advertisements

Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
13 Copyright © 2005, Oracle. All rights reserved. Monitoring and Improving Performance.
Module 13: Performance Tuning. Overview Performance tuning methodologies Instance level Database level Application level Overview of tools and techniques.
Overview of performance tuning strategies Oracle Performance Tuning Allan Young June 2008.
What Happens when a SQL statement is issued?
Exadata Distinctives Brown Bag New features for tuning Oracle database applications.
1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
© IBM Corporation Informix Chat with the Labs John F. Miller III Unlocking the Mysteries Behind Update Statistics STSM.
Copyright © 200\8 Quest Software High Performance PL/SQL Guy Harrison Chief Architect, Database Solutions.
INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Performance And Tuning – Lecture 7 Copyright System Managers LLC 2007 all rights reserved.
Semantec Ltd. Oracle Performance Tuning Boyan Pavlov Indexes Indexes.
Agenda Overview of the optimizer How SQL is executed Identifying statements that need tuning Explain Plan Modifying the plan.
Harvard University Oracle Database Administration Session 2 System Level.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 11 Database Performance Tuning and Query Optimization.
Optimization Exercises. Question 1 How do you think the following query should be computed? What indexes would you suggest to use? SELECT E.ename, D.mgr.
ORACLE ONLINE TRAINING Contact our Support Team : SOFTNSOL India: Skype id : softnsoltrainings id:
Executing Explain Plans and Explaining Execution Plans Craig Martin 01/20/2011.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 11 Database Performance Tuning and Query Optimization.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
IT The Relational DBMS Section 06. Relational Database Theory Physical Database Design.
Chapter Oracle Server An Oracle Server consists of an Oracle database (stored data, control and log files.) The Server will support SQL to define.
Oracle Database Administration Lecture 6 Indexes, Optimizer, Hints.
1 Robert Wijnbelt Health Check your Database A Performance Tuning Methodology.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Materialized Views Acknowledgement to Author: Willie Albino.
Database Systems Slide 1 Database Systems Lecture 5 Overview of Oracle Database Architecture - Concept Manual : Chapters 1,8 Lecturer : Dr Bela Stantic.
Materialized Views. 2 Materialized Views – Agenda What is a Materialized View? – Advantages and Disadvantages How Materialized Views Work – Parameter.
Oracle Tuning Ashok Kapur Hawkeye Technology, Inc.
Basic Optimization DB Workshop for LHC online/offline developers CERN January
11-1 Improve response time of interactive programs. Improve batch throughput. To ensure scalability of applications load vs. performance. Reduce system.
Oracle9i Performance Tuning Chapter 12 Tuning Tools.
Star Transformations Tony Hasler, UKOUG Birmingham 2012 Tony Hasler, Anvil Computer Services Ltd.
Mark Inman U.S. Navy (Naval Sea Logistics Center) Session #213 Analytic SQL for Beginners.
SQL Performance and Optimization l SQL Overview l Performance Tuning Process l SQL-Tuning –EXPLAIN PLANs –Tuning Tools –Optimizing Table Scans –Optimizing.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Oracle tuning: a tutorial Saikat Chakraborty. Introduction In this session we will try to learn how to write optimized SQL statements in Oracle 8i We.
Module 4 Database SQL Tuning Section 3 Application Performance.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
Chapter 5 Index and Clustering
1 Copyright © 2005, Oracle. All rights reserved. Following a Tuning Methodology.
1 Chapter 9 Tuning Table Access. 2 Overview Improve performance of access to single table Explain access methods – Full Table Scan – Index – Partition-level.
Chap 5. Disk IO Distribution Chap 6. Index Architecture Written by Yong-soon Kwon Summerized By Sungchan IDS Lab
Sorting and Joining.
Ad Hoc User or Application Cost-based Data Dictionary Statistics Rule-based Execution Plan Asks the question: All people and their grades in a list giving.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
Database Systems, 8 th Edition SQL Performance Tuning Evaluated from client perspective –Most current relational DBMSs perform automatic query optimization.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
SQL Basics Review Reviewing what we’ve learned so far…….
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
How is data stored? ● Table and index Data are stored in blocks(aka Page). ● All IO is done at least one block at a time. ● Typical block size is 8Kb.
Tuning Oracle SQL The Basics of Efficient SQL Common Sense Indexing
Module 11: File Structure
EXPLAIN and AUTOTRACE.
Database Performance Tuning &
Database Performance Tuning and Query Optimization
Physical Database Design
Oracle Memory Internals
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Chapter 11 Database Performance Tuning and Query Optimization
MATERI PL/SQL Procedures Functions Packages Database Triggers
Diving into Query Execution Plans
Introduction to the Optimizer
Presentation transcript:

SQL Tuning Ohio Oracle User’s Group October 2002 © Copyright, Kris T. Mason, 2002

2 Kris T. Mason Information Control Corp (ICC) Consultant –10+ years with Oracle –Oracle 7x, 8x, 9i –70% Application – 30% Production CSCC Adjunct Faculty –Oracle Programming –Computer Information Systems Concepts

3 Introduction Overview of the SQL Tuning process Database Internals Schema Objects involved with queries SQL Optimizer and Execution Plans Application construction

4 Why Tune?

5 Philosophy Be a Good Neighbor –Consume only those resources you need –Release resources you are not using The more you know...

6 Reality Check Applications can not treat the database as a ‘black-box’ and expect optimal performance. They must contribute to efficiency. Overall efficiency can be gained by tuning small, frequently executing queries as well as large infrequent ones Tuning can’t overcome bad design Tune SQL before tuning the DB Instance Politics, Balance, Judgement

7 Preparing to tune Programming –Languages –Application segmentation –Bind Variables Server Configuration –Hardware (CPUs, Memory, Disks, Network) –Non-Database applications Database Configuration –Init.ora –Gather statistics – select value, name from v$sysstat order by name; –Analyze Tables & Indexes (DBMS_STATS package) –DB Links and Replication Know where you are, want to be and how to get there –Current performance –What is ‘acceptable’ performance –How long it will take

8 Minimizing Logical Reads Disk Reads Temp Space Memory Sorts Parsing Network Traffic Application Round Trips Response Time (Perceived & Real)

9 Database Internals System Global Area (SGA) –Database Block Buffers Block Size usually 8k - 16k Holds portions of Tables and Indexes Minimizes Expensive Disk Reads V$DB_CACHE_ADVICE Buffer Cache Hit Ratio –Library Cache – Dictionary and recent SQL Two SQL statements are identical only if they match byte for byte Programmers and Dynamic Query Rewrite (Cursor Sharing) can force queries to be identical via bind variables Identical Queries accumulate statistics in V$SQLAREA providing better visibility to statement use

10 Oracle Features Dynamic Query Rewrite (Cursor Sharing) Parallel Query Optimizer Modes Hints Alter Session statement Table & Index Partitioning Statspack

11 Dynamic Query Rewrite (Cursor Sharing) Init.ora –QUERY_REWRITE_ENABLED=TRUE –QUERY_REWRITE_INTEGRITY=TRUSTED Turns literal strings and values into bind variables –Bypasses the programmer Different queries become identical –WHERE STATUS = 1 AND NAME = ‘SMITH’ –WHERE STATUS = 3 AND NAME = ‘JONES’ –WHERE STATUS = :b1 AND NAME = :b2 Statistics are accumulated under the single query

12 Parallel Query Allows multiple CPUs to work parts of the same query (divide and conquer) My experience: –Best when 6+ CPUs –Can be over done, causing different concurrently executing queries to compete for resources Configure PQ Servers (#CPUs – 1)*2

13 Optimizer Rule / Choose / Cost Generates alternative execution plans Plans can be seen and understood –SET AUTOTRACE ON –SET AUTOTRACE TRACEONLY

14 Primary Schema Objects Tables –Full Table Scan –Random Data Block Read ROWID – Direct Access Index –Index Organized Table Indexes –B*Tree / Bitmap / Index-Only Table / Function Based –Ideal when highly selective –Index Only Processing avoids Data Block Read –Entirely NULL values are not stored in the index –ALTER INDEX REBUILD Tables & Indexes require Data block reads if not in memory already

15 Indexes Selectivity = (# of Distinct Values / # of Rows) * 100 Selectivity = 0% when all the values have the same value Selectivity = 100% when all the values have different values (PKs) Indexes loose effectiveness when Selectivity falls below 80% Indexes gain effectiveness when Selectivity approaches 100% Index use can be invalidated (Index Invalidation) when the column reference in the WHERE is involved with a function SELECT... WHERE ID = 5 /* Uses an index */ SELECT... WHERE ID + 0 = 5 /* Does not use an index */ SELECT... WHERE NAME BETWEEN ‘SMITH’ AND ‘THOMAS’ /* Uses an index */ SELECT... WHERE UPPER(NAME) BETWEEN ‘SMITH’ AND ‘THOMAS’ /* Does not use an index */

16 Secondary Schema Objects Views –Stored SELECT statement –Some Hints do not propagate inside a complex view Triggers –PL/SQL code –Registered against a specific table –Fired upon specific DML events

17 Tuning Process Find SQL –SQL Analyze & Top SQL –Top Sessions –SQL*Plus & SQL Worksheet V$SQLAREA Look For –High Disk Reads vs. Low Rows Returned –High Executions –DISTINCT Identify Problem –Selectivity –Access Path –Join Method and Order Consider Possible Alternatives –Hints –Indexes Document, Measure & Test Alternatives –Show your work Deploy Solution –Get credit for your work

18 Finding SQL to tune V$SQLAREA set linesize 1000 set pagesize 1000 column disk_reads format 999,999,999,999 column executions format 999,999,999,999 column rows_processed format 999,999,999,999 column sql_text format a800 word_wrapped select executions, disk_reads, rows_processed, sorts, sql_text from v$sqlarea where disk_reads > 3000 order by disk_reads DESC; Look for high resource consumers DISK_READS EXECUTIONS SORTS Limited to 1000 bytes of SQL Statement including white space. Be sure you have the whole statement! The entire statement exists in v$sqltext but it is broken into pieces

19 Helper PL/SQL Functions CREATE OR REPLACE FUNCTION SQUISH (P in varchar2) return varchar2 as I number; V varchar2(32000); Begin /* Change special chars to blanks */ V := trim(rtrim(translate(P,chr(9)||chr(10)||chr(13), ' '))); /* One blank between quotes */ Loop I := instr (V,' '); /* Two blanks between quotes */ Exit when I = 0; /* V := replace two blanks with one */ V := replace (V,’ ‘,’ ‘); End loop; Return V; End Squish; CREATE OR REPLACE FUNCTION CAT_SQL_TEXT (P in varchar2) return varchar2 as V varchar2(32000) := ''; Begin for C in (select sql_text from v$sqltext where address = P order by piece) Loop V := V || C.sql_text; End loop; Return V; End cat_sql_text; Grant select on v$sqltext to [function owner];

20 Finding SQL To Tune with Helper Functions V$SQLTEXT set linesize 1000 set pagesize 1000 column disk_reads format 999,999,999,999 column executions format 999,999,999,999 column rows_processed format 999,999,999,999 column sql_text format a800 word_wrapped select address, hash_value, executions, disk_reads, rows_processed, sorts, squish(cat_sql_text(address)) sql_text from v$sqlarea where disk_reads > 3000 order by disk_reads DESC;

21 Finding SQL to tune DISTINCT EXECUTIONS DISK_READS ROWS_PROCESSED SORTS SQL_TEXT ,499 5, SELECT DISTINCT TABLE_B.NAME FROM TABLE_A, TABLE_B WHERE ( TABLE_A.ID = TABLE_B.ID ) The Intent of this query is to show the names in Table_B that have any Matching rows in Table_A

22 Original Document the query as originally written SELECT DISTINCT TABLE_B.NAME FROM TABLE_A, TABLE_B WHERE ( TABLE_A.ID = TABLE_B.ID ); Execution Plan SELECT STATEMENT Optimizer=CHOOSE (Cost=921 Card=306 Bytes=5814) 1 0 SORT (UNIQUE) (Cost=921 Card=306 Bytes=5814) 2 1 HASH JOIN (Cost=33 Card=73605 Bytes= ) 3 2 TABLE ACCESS (FULL) OF ‘TABLE_B' (Cost=1 Card=307 Bytes=4298) 4 2 INDEX (FAST FULL SCAN) OF 'FK_TABLE_A_ID' (NON-UNIQUE) (Cost=14 Card=73605 Bytes=368025)

23 Original Statistics Statistics recursive calls 8 db block gets 247 consistent gets 0 physical reads 0 redo size 1368 bytes sent via SQL*Net to client 591 bytes received via SQL*Net from client 6 SQL*Net roundtrips to/from client 5 sorts (memory) 0 sorts (disk) 63 rows processed

24 Alternative Document each version of the query as rewritten SELECT TABLE_B.NAME FROM TABLE_B WHERE EXISTS ( SELECT 1 FROM TABLE_A WHERE TABLE_A.ID = TABLE_B.ID ); Execution Plan SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=16 Bytes=224) 1 0 FILTER 2 1 TABLE ACCESS (FULL) OF ‘TABLE_B' (Cost=1 Card=16 Bytes=224) 3 1 INDEX (RANGE SCAN) OF 'FK_TABLE_A_ID' (NON-UNIQUE) (Cost=4 Card=1169 Bytes=5845)

25 Alternative’s Statistics Statistics recursive calls 4 db block gets 638 consistent gets 0 physical reads 0 redo size 1409 bytes sent via SQL*Net to client 591 bytes received via SQL*Net from client 6 SQL*Net roundtrips to/from client 2 sorts (memory) 0 sorts (disk) 63 rows processed

26 Documentation Original version Reformed version –Rewrite by hand in a form comfortable to you –Make sure it is equivalent to the original –Match query’s joins to data model Each alternative –A record of your work Recommended solution

27 Testing Full volume production testing Test or Personal copy of Database DBMS_STATS –Import Statistics from Production

28 What Can Be Done? Analyze Tables & Indexes –Histograms See Oracle’s Execution Plan –SET AUTOTRACE [ON, TRACEONLY] –PLAN table –Query Costs (Oradollars) Hints – You know more than the Optimizer! –Optimizer Goal FIRST_ROWS ALL_ROWS –Access Path FULL INDEX –Query Transformation ORDERED STAR –Join Operation USE_NL USE_MERGE USE_HASH DRIVING_SITE LEADING

29 Execution Plans A roadmap for executing the query –Top to bottom –Inside to outside Execution Techniques –Full Table Scan – Not necessarily a bad thing –Unique Index Scan –Index Range Scan –Concatenation –Sort Aggregation Order By Group By –Nested Loops –Merge Join –Hash Join –IN-List Iterator

30 Hints SELECT /*+ hint goes here */... Features: –Syntax errors are not reported and not used –Specify the table alias not the table name –Put preceding and trailing blank –You can use multiple hints –Hints don’t have to be used by the optimizer

31 Other Techniques to Optimize Performance ALTER SESSION –SET SORT_AREA_SIZE= –SET ROLLBACK_SEGMENT=RBS_BIG Minimized Chained Rows How full are the data blocks? –Random Deletes reduce efficiency of table scans Use Plan Stability to insure optimizer does not change the plan over time

32 Distributed Queries Queries across multiple instances Via a DB_Link Create a view of the remote tables Minimize ‘round-trips’ to the remote DB Pull data to the local DB via a snapshot Wire the local & remote servers together

33 Effects of tuning

34 Applications Start with optimized queries so when code is cloned it has a better chance of working well Review 3 rd party produced code such as PowerBuilder Write code for the proper platform (application segmentation) PL/SQL is not ‘cool’ but it is fast and effective Use Bind Variables whenever possible Avoid Dynamic SQL if possible

35 References Oracle’s Technet –Database Performance Tuning Guide & Reference se2/server.920/a96533/toc.htm

36 Summary SQL Tuning is not necessarily hard, it just takes some practice and the willingness to try