Presented By Akin S Walter-Johnson Ms Principal PeerLabs, Inc

Slides:



Advertisements
Similar presentations
Physical Database Design and Tuning R&G - Chapter 20 Although the whole of this life were said to be nothing but a dream and the physical world nothing.
Advertisements

Tuning Oracle SQL The Basics of Efficient SQLThe Basics of Efficient SQL Common Sense Indexing The Optimizer –Making SQL Efficient Finding Problem Queries.
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
Understanding SQL Server Query Execution Plans
Introduction to SQL Tuning Brown Bag Three essential concepts.
SQL Tuning Briefing Null is not equal to null but null is null.
Denny Cherry Manager of Information Systems MVP, MCSA, MCDBA, MCTS, MCITP.
CpSc 3220 File and Database Processing Lecture 17 Indexed Files.
1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
EXECUTION PLANS By Nimesh Shah, Amit Bhawnani. Outline  What is execution plan  How are execution plans created  How to get an execution plan  Graphical.
INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Performance And Tuning – Lecture 7 Copyright System Managers LLC 2007 all rights reserved.
Query Evaluation. An SQL query and its RA equiv. Employees (sin INT, ename VARCHAR(20), rating INT, age REAL) Maintenances (sin INT, planeId INT, day.
Agenda Overview of the optimizer How SQL is executed Identifying statements that need tuning Explain Plan Modifying the plan.
Introduction to Structured Query Language (SQL)
David Konopnicki Choosing Access Path ä The basic methods. ä The access paths and when they are available. ä How the optimizer chooses among the.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 11 Database Performance Tuning and Query Optimization.
1 Indexing Structures for Files. 2 Basic Concepts  Indexing mechanisms used to speed up access to desired data without having to scan entire.
Introduction to Structured Query Language (SQL)
Relational Database Performance CSCI 6442 Copyright 2013, David C. Roberts, all rights reserved.
Indexing. Goals: Store large files Support multiple search keys Support efficient insert, delete, and range queries.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 11 Database Performance Tuning and Query Optimization.
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.
1 Physical Data Organization and Indexing Lecture 14.
Oracle Database Administration Lecture 6 Indexes, Optimizer, Hints.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Database Management 9. course. Execution of queries.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Chapter 6 1 © Prentice Hall, 2002 The Physical Design Stage of SDLC (figures 2.4, 2.5 revisited) Project Identification and Selection Project Initiation.
1 Index Structures. 2 Chapter : Objectives Types of Single-level Ordered Indexes Primary Indexes Clustering Indexes Secondary Indexes Multilevel Indexes.
Query Processing. Steps in Query Processing Validate and translate the query –Good syntax. –All referenced relations exist. –Translate the SQL to relational.
Copyright © Curt Hill Query Evaluation Translating a query into action.
DBMS Implementation Chapter 6.4 V3.0 Napier University Dr Gordon Russell.
M1G Introduction to Database Development 5. Doing more with queries.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
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.
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.
Marwan Al-Namari Hassan Al-Mathami. Indexing What is Indexing? Indexing is a mechanisms. Why we need to use Indexing? We used indexing to speed up access.
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
9-1 © Prentice Hall, 2007 Topic 9: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
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.
SCALING AND PERFORMANCE CS 260 Database Systems. Overview  Increasing capacity  Database performance  Database indexes B+ Tree Index Bitmap Index 
Query Processing – Implementing Set Operations and Joins Chap. 19.
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.
 CONACT UC:  Magnific training   
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.
Indexing Structures for Files and Physical Database Design
CS 540 Database Management Systems
Indexing and hashing.
Database Management System
Choosing Access Path The basic methods.
Database Performance Tuning and Query Optimization
Introduction to Execution Plans
Lecture 2- Query Processing (continued)
Advance Database Systems
Introduction to Execution Plans
Implementation of Relational Operations
Chapter 11 Database Performance Tuning and Query Optimization
CS222P: Principles of Data Management Notes #13 Set operations, Aggregation, Query Plans Instructor: Chen Li.
Diving into Query Execution Plans
Introduction to Execution Plans
Introduction to Execution Plans
Presentation transcript:

Presented By Akin S Walter-Johnson Ms Principal PeerLabs, Inc http://www.peerlabs.com Oracle SQL Tuning Presented By Akin S Walter-Johnson Ms Principal PeerLabs, Inc Email akin@peerlabs.com

SCOPE How data is accessed and reconstituted joins Inform the user on how identify problems with SQL Repair of SQL Tuning can occur at 2 levels Server ( DBA) SQL level ( User)

IMPORTANCE OF TUNING Reduce response time for SQL processing To find a more efficient way to process workload Improve search time by using indexes Join data efficiently between 2 or more tables

HOW TO TUNE Review the access path, Join methods and index usage Test response through SQPLUS directly ( May mask performance ) Test response through an Application front end ( Usually takes longer ) Test response through a web interface

ROLE OF HARDWARE & DESIGN All the hardware in world will not save you Memory, Disk & CPU speed can improve performance Increased hardware does not always result into better performance Poor application design accounts for over 70% of performance issues Do Performance design review early in development

OVERVIEW OF SQL PROCESSING

OVERVIEW OF SQL PROCESSING The Parser checks both syntax and semantic analysis of SQL statement Optimizer determines the most efficient way of producing the result of the query also known as the EXPLAIN PLAN. How best to get the data. Oracle Optimizer types ( Cost Based and Rule Based ) CBO based Optimizer uses cost associated with each execution requires you to analyze objects for statistics RULE based Optimizer internal rules ( not encouraged by oracle) The SQL Execution Engine operates on the execution plan associated with a SQL statement and then produces the results of the query.

SETTING OPTIMIZER SERVER Level by DBA in parameter file (init.ora) CLIENT Level SQLPLUS command < alter session set optimizer_mode=choose> STATEMENT Level using hints a. select /*+RULE */ * from dual ; b. select /*+ CHOOSE */ * from dual ; Order of Precedence SERVER->CLIENT->STATEMENT Users can set both client and statement To use CBO you need to analyze the tables (see Analyze objects)

OPTIMIZER OPERATIONS THAT AFFECT PERFORMANCE The Optimizer is the brain behind the process of returning data to user it needs to make the following choices. OPTIMIZER APPROACH ACCESS PATH JOIN ORDER JOIN METHOD Choice of optimizer approaches CBO or RULE Choice of Access Paths ( How data is Scanned ) Use an index if not reading all records ( faster) Read or scan all records Choice of Join Orders Determine which table to join first when you have more than two tables in an SQL Choice of Join Methods Determine how to join the tables ( Merge, Sort, Hash )

SQLPLUS ENVIRONMENT LAB Log on Set timing Auto Trace to see plan ( How SQL is processed ) Set optimizer Review Plan

ANALYZE OBJECT STATISTICS Statistics describe physical attributes of an object such as Number of rows, average space, empty blocks All objects need to have statistics to use CBO Stored in user_tables and user_indexes Not update automatically use analyze Table Statistics Table Name Number of rows Average space Total number of blocks Empty blocks Index Statistics Index_Name Index_Type Table_Name Distinct_Keys Avg_Leaf_Blocks_Per_Key Avg_Data_Blocks_Per_Key

ANALYZE OBJECT STATISTICS LAB Create Table Create Index Review tables Review indexes

TABLE TUNING (i) A Table in oracle store data Resides in a schema within a Table-space Contains actual data stored in oracle blocks An oracle block is a multiple of the OS block (Ask your DBA) Row Chaining (Performance killer) A row is too large to fit into on data block so oracle uses more than one chaining them Chaining occurs when you try to inset or update Row migration (Performance killer) There is not enough place in the BLOCK for UPDATES Oracle tries to find another Block with enough free space to hold the entire row.( Unnecessary scanning) If a free block is available Oracle moves the entire ROW to the NEW BLOCK. Oracle keeps the original Row piece of a Migrated row row to POINT to the NEW BLOCK Queries that select from chained or migrated rows must perform double read and write (I/O. To find Chained or Migrated table run SQL> ANALYZE TABLE SCHEMA_NAME.TABLE_NAME LIST CHAINED ROWS; SQL> select CHAIN_CNT from user_tables ;

TABLE TUNING (ii) Too many empty blocks Occurs after a massive delete then inserting few records Select statement takes a very long time with only one record in table Solution is to TRUNCATE the table and copy to new table

TABLE TUNNING LAB

WHY USE AN INDEX What is an Index Oracle Index A pointer or a hand that directs to something Similar to index at the end of a book Oracle Index Binary tree Structure with entries know as ROWID Left nodes contain key and rowid ROWID is internal and points to direct location of record on disk ROWID is fasted way to reach a record. SQL> Select rowid, id, name from mytable ;

OPTIMIZER ACCESS by ROWID ROWID SCAN The fastest way to get a row Based on the file and the data block where record is located Used also during an index scan

OPTIMIZER ACCESS by INDEX UNIQUE SCAN The scan returns only one row It requires an index (Primary key)on the Table Index is automatically created for primary key Used by Optimizer When an index exist on a column with a where clause When the optimizer is told to use an index (hint) Index hints are not really used. Reading Explain Plan Do a unique scan of the index and obtain ROWID Access the table by ROWID

OPTIMIZER ACCESS by INDEX RANGE SCAN The scan may return more than one row Used by optimizer when where clause has > or < sign where clause has between 10 and 20 where clause has like * ( wild card)

OPTIMIZER ACCESS by MULTIPLE UNIQUE SCAN Optimizer will search for ROWID in the statement Concatenate all records into one row set Combining all rows selected by the unique scan into I row set Used by Optimizer when where clause has an in condition id IN ( 123, 456, 678 )

OPTIMIZER ACCESS by MULTIPLE UNIQUE SCAN

OPTIMIZER ACCESS by FULL TABLE SCAN Each record is read one by one A sequential search for data no index is used The slowest search Occurs when There is no index or index disabled When the Optimizer is hinted not to use the index

OPTIMIZER ACCESS by FAST FULL INDEX SCAN Alternative to a full table scan Used by optimizer when Index contains all the columns that are needed for the query If I want to display only your SSN, you don’t have to access the table if I have SSN as an index A fast full scan accesses the data in the index itself, without accessing the table

OPTIMIZER JOIN METHOD A query with more than one table requires to have a Join Order Join Order are steps taken to assemble rows of data from more than one table Select From A,B,C Where A.col1 = B.Col1 And B.Col2 = C.Col2 NESTED LOOP SORT-MERGE HASH JOIN

OPTIMIZER JOIN METHOD NESTED LOOP Uses a Looping method to join 2 table For every record in A we look thru all rows in B using an index to find a match Table A is Outer Loop or Driving table Table B is Inner Loop

Nested Loop

Nested Loop Good when you expect a small number of rows back Good for Small driving table so not Good if driving table is large Good when Index on B exist ( will perform poor when no index on B ) Good if you want to quickly returns data to the screen ( ONLINE USERS ) HINT select SELECT /*+ ORDERED USE_NL(DEPT) to get first row faster */ EMPNO, ENAME, DEPT.DEPTNO FROM EMP, DEPT WHERE EMP.DEPTNO = DEPT.DEPTNO ;

OPTIMIZER JOIN METHOD SORT MERGE JOIN In a merge join, there is no concept of a driving table The join consists of two steps: Sort join operation: Both the inputs are sorted on the join key. Merge join operation: The sorted lists are merged together.

OPTIMIZER JOIN METHOD SORT MERGE JOIN

OPTIMIZER JOIN METHOD SORT MERGE JOIN The Merge can’t begin until data sorted from both tables Since there is a waiting period, this join method will not be good for ONLINE users Good when you don’t have an index on the join columns, if Index exist a NESTED LOOP is done Good when NESTED LOOP does not perform when Good if rows are loaded in a sorted fashion Not Good if you want to quickly return data to the screen ( ONLINE USERS ) need to wait for sorting. Not Good is one of the tables is very,very large because a Full table scan will be done. Good if working with a oracle parallel options because SORTING can be done in parallel

OPTIMIZER JOIN METHOD SORT MERGE JOIN Optimizer will use SORT JOIN when index does not exist (May be a warning) Optimizer will use SORT JOIN when OPTIMIZER_MODE is Rule Optimizer will use SORT JOIN when HASH_JOIN_ENABLED is false. HINT SELECT /*+ USE_NL(l h) */

OPTIMIZER JOIN METHOD HASH JOIN HASH join compares tow tables in memory to find matching rows Must set HASH_JOIN_ENABLED to True(DBA ) Read first table into memory via Full table scan Apply hashing function to data to prepare for join on key fields Read second table via Full table scan Apply hashing function to compare the second to the first table

OPTIMIZER JOIN METHOD HASH JOIN

OPTIMIZER JOIN METHOD HASH JOIN Good only when you have parallel options for oracle because of FTS Good if you have more memory set aside for hashing functions Good if you indexes don't perform well with NESTED LOOP May be faster than NESTED LOOP because you are reading in memory as supposed to using index Better than SORT MERGE because only on table has to be sorted

OPTIMIZER JOIN METHOD HASH JOIN HINT SELECT /*+use_hash(emp, dept )*/ EMPNO, ENAME, DEPT.DEPTNO FROM EMP, DEPT WHERE EMP.DEPTNO = DEPT.DEPTNO ;

OPTIMIZER JOIN ORDER Avoid performing unnecessary work to access rows that do not affect the result. Choose the best join order, driving to the best unused filters earliest.

OPTIMIZER JOIN ORDER Query 1 SELECT info FROM taba a, tabb b, tabc c WHERE a.key1 = b.key1 AND a.key2 = c.key2 AND a.acol BETWEEN 100 AND 200 AND b.bcol BETWEEN 10000 AND 20000 AND c.ccol BETWEEN 10000 AND 20000

OPTIMIZER JOIN ORDER Query 2 SELECT info FROM taba a, tabb b, tabc c WHERE a.acol BETWEEN 100 AND 200 AND b.bcol BETWEEN 10000 AND 20000 AND c.ccol BETWEEN 10000 AND 20000 AND a.key1 = b.key1 AND a.key2 = c.key2;

OPTIMIZER JOIN ORDER Query3 SELECT info FROM taba a, tabb b, tabc c WHERE b.bcol BETWEEN 10000 AND 20000 AND c.ccol BETWEEN 10000 AND 20000 AND a.acol BETWEEN 100 AND 200 AND a.key1 = b.key1 AND a.key2 = c.key2;

OPTIMIZER JOIN ORDER The work of the following join can be reduced by first joining to the table with the best still-unused filter. Thus, if "bcol BETWEEN ..." is more restrictive (rejects a higher percentage of the rows seen) than "ccol BETWEEN ...", the last join can be made easier (with fewer rows) if tabb is joined before tabc.

OPTIMIZER JOIN ORDER The driving table is the one containing the filter condition that eliminates the highest percentage of the table. Thus, because the range of 100 to 200 is narrow compared with the range of acol, but the ranges of 10000 and 20000 are relatively large, taba is the driving table, all else being equal. HINT The ORDERED hint causes Oracle to join tables in the order in which they appear in the FROM clause.

INDEX TUNING Rebuild you index often ( index oil change) Gather statistics Do not over use Indexes Restrict to cols that return a few records Use Bitmapped index when number of values is small e.g ( Sex male, female) Suppression of index Select * from mytable where total + 3 = 20

REPAIR YOUR SQL STATEMENT LAB Understand the purpose of a statement re-writing may improve performance. Use equi joins on where clause Avoid column transformation Where to_number(a.id) = b.id Do not use function in predicate Where to_string(a.id) = b.id col1 = NVL (:b1,col1) NVL (col1,-999) = .... TO_DATE(), TO_NUMBER(), and so on

REPAIR YOUR SQL STATEMENT LAB WRITE SEPARATE SQL STATEMENTS FOR SPECIFIC TASKS It is better to use IN rather than EXISTS. , if the selective predicate is in the subquery, then use IN. If the selective predicate is in the parent query, then use EXISTS.

SQL TUNING CHECK LIST Ask DBA if Cost based optimizer is default in init.ora Check if you have statistics for tables and indexes Check if you have a high number of empty blocks on tables(due to large deletes) Check if you have row chaining or row migration on Tables Check index cluster Review SQL plan Use more packages and stored procedures