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.

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

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
Overview of performance tuning strategies Oracle Performance Tuning Allan Young June 2008.
Exadata Distinctives Brown Bag New features for tuning Oracle database applications.
1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Performance And Tuning – Lecture 7 Copyright System Managers LLC 2007 all rights reserved.
Agenda Overview of the optimizer How SQL is executed Identifying statements that need tuning Explain Plan Modifying the plan.
1 - Oracle Server Architecture Overview
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 11 Database Performance Tuning and Query Optimization.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 11 Database Performance Tuning and Query Optimization.
Chapter 9 Overview  Reasons to monitor SQL Server  Performance Monitoring and Tuning  Tools for Monitoring SQL Server  Common Monitoring and Tuning.
1 Tuning PL/SQL procedures using DBMS_PROFILER 20-August 2009 Tim Gorman Evergreen Database Technologies, Inc. Northern California Oracle.
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
ORACLE ONLINE TRAINING Contact our Support Team : SOFTNSOL India: Skype id : softnsoltrainings id:
SQL Tuning Ohio Oracle User’s Group October 2002 © Copyright, Kris T. Mason, 2002.
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.
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.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
March 19981© Dennis Adams Associates Tuning Oracle: Key Considerations Dennis Adams 25 March 1998.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
© Dennis Shasha, Philippe Bonnet – 2013 Communicating with the Outside.
1 Oracle Architectural Components. 1-2 Objectives Listing the structures involved in connecting a user to an Oracle server Listing the stages in processing.
Copyright  Oracle Corporation, All rights reserved. 1 Oracle Architectural Components.
Oracle Tuning Ashok Kapur Hawkeye Technology, Inc.
11-1 Improve response time of interactive programs. Improve batch throughput. To ensure scalability of applications load vs. performance. Reduce system.
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.
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.
Introduction MySQL won't actually execute the query, just analyse it EXPLAIN helps us understand how and when MySQL will use indexes EXPLAIN returns a.
© IBM Corporation 2005 Informix User Forum 2005 John F. Miller III Explaining SQLEXPLAIN ®
1 Copyright © 2005, Oracle. All rights reserved. Following a Tuning Methodology.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Oracle 11g: SQL Chapter 7 User Creation and Management.
1 Chapter 9 Tuning Table Access. 2 Overview Improve performance of access to single table Explain access methods – Full Table Scan – Index – Partition-level.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
DB Tuning : Chapter 10. Optimizer Center for E-Business Technology Seoul National University Seoul, Korea 이상근 Intelligent Database Systems Lab School of.
Optimization and Administartion of a Database Management Systems Krystian Zieja.
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 7 User Creation and Management Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga.
Database Systems, 8 th Edition SQL Performance Tuning Evaluated from client perspective –Most current relational DBMSs perform automatic query optimization.
 CONACT UC:  Magnific training   
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Execution Plans Detail From Zero to Hero İsmail Adar.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
 Reviewing basic architecture concepts  Oracle 10g Architecture  Main features of 9i and 10g
Oracle Database Architectural Components
D Copyright © 2009, Oracle. All rights reserved. Using SQL*Plus.
Practical Database Design and Tuning
SQL Trace and TKPROF.
Indexes By Adrienne Watt.
EXPLAIN and AUTOTRACE.
Database Performance Tuning &
Database Performance Tuning and Query Optimization
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Transactions, Locking and Query Optimisation
Oracle Memory Internals
Oracle Architectural Components
Chapter 11 Database Performance Tuning and Query Optimization
Diving into Query Execution Plans
Introduction to the Optimizer
Presentation transcript:

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 sname and fname and grade SELECT fname, sname, grade FROM persons p, grades g WHERE p.GRADEID = g.GRADEID ; Is cursor in shared pool already? Which optimiser mode? Parser No Returned data set Re-use Yes

School of Computing and Management Sciences © Sheffield Hallam University Query efficiency: Parser Decomposes what has been passed and makes sense of it by: –verifying it to be correct in syntax –looking up any table and column definitions in the data dictionary to check for accuracy –Breaking the string up and looking for: –what is being selected, and is it distinct? –What are the prefixes all about? –What are the conditions (WHERE) –What sorts and grouping needs to be carried out? –Checking privileges

School of Computing and Management Sciences © Sheffield Hallam University Query efficiency: Parser Re-using SQL saves parsing overhead BUT: –Select * from Persons ; is not the same as: –Select * from PERSONS ; So adopt a naming convention and stick with it! However, by using bind variables: –Select * from Persons where personid=:pid ; IS the same whatever value is bound. So use bind variables wherever possible

School of Computing and Management Sciences © Sheffield Hallam University Query efficiency: Optimiser The optimiser determines the most efficient way to execute a SQL statement For any SQL statement there are a finite number of possible 'execution plans'. Best can be: –uses least resources to process all rows affected by the statement. –returns the first row of a statement the quickest

School of Computing and Management Sciences © Sheffield Hallam University Query efficiency: Optimiser First decision: How to decide what is efficient? Up until Oracle V7 the optimiser was Rule based (RBO). That is: –It chooses an execution plan based on the access paths available and the ranks of these access paths. –If there is more than one way to execute a SQL statement, RBO always uses the operation with the lower rank because this will, probably, execute faster

School of Computing and Management Sciences © Sheffield Hallam University Query efficiency: RBO

School of Computing and Management Sciences © Sheffield Hallam University Query efficiency: RBO The problem with the RBO approach is that it doesn’t recognise context as important: –“If there is more than one way to execute a SQL statement, RBO always uses the operation with the lower rank because this will, probably, execute faster” –Different data distributions could cause the actual ranking to be inaccurate in the first place. –It is quite possible that what is the most efficient access path today, will not be the most efficient next week, just because the data distribution has changed.

School of Computing and Management Sciences © Sheffield Hallam University Query efficiency: CBO From V7 onwards you can choose to use CBO CBO is a mathematical processor, a sort of expert system It calculates the cost of a SQL statement, based upon statistics, in terms of I/o This gets around the RBO problem of context But is only as good as the statistics And the assumptions made by the “expert” Adds both admin and process overhead because of the need to Analyze all tables regularly. –ANALYZE TABLE persons COMPUTE STATISTICS;

School of Computing and Management Sciences © Sheffield Hallam University Query efficiency: Optimiser For an Oracle instance you can choose which with the OPTIMZER_MODE init.ora param: ALL_ROWS, FIRST_ROWS, CHOOSE, RULE At the query level you can override using a hint after the SELECT: Select /*+FULL*/ Fname, Sname, Grade…. –Forces a full table scan, even if optimser thinks using an index would be better - well most of the time! –The Optimiser is empowered to ignore hints!

School of Computing and Management Sciences © Sheffield Hallam University SET AUTOTRACE ON STAT* SQL> select count(sname) from persons; COUNT(SNAME) Statistics recursive calls 4 db block gets 20 consistent gets 0 physical reads 0 redo size 372 bytes sent via SQL*Net to client 425 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 1 sorts (memory) 0 sorts (disk) 1 rows processed SQL> *NOTE: you must have the PLUSTRACE role granted to you and a PLAN_TABLE table created in your schema.

-- log in as the test user and change --the settings connect a_user/a_password ; SET TERMOUT OFF SET AUTOTRACE ON STAT --get rid of any existing Person drop table Person ; --create the grade table and pop some data in it START create_grades -- first do the stuff with a foreign key -- create a Person table, with foreign key START create_forkey --start the timer Timing START forkey_ins --run the insert script and then commit START person_inserts Commit ; --spool output to a file SPOOL testresults_withFK.txt -- CONTINUED --stop timer Timing STOP forkey_ins --now do the update test Timing START forkey_update START person_update Commit ; Timing STOP forkey_update SPOOL off SET TERMOUT ON

Garbage!

School of Computing and Management Sciences © Sheffield Hallam University Testing Always remember to run tests many times, but especially remember: –the first time a query runs is likely to take longer than subsequent runs because there is no need to parse if the cursor is still in SGA –Timing, in particular, can be dramatically impacted by what else the database server is doing

Using Explain Plan Execution plan –Whichever optimiser is used, its final output will be an execution plan. An execution plan includes access methods for each table that the statement accesses and the ordering of any joins. –The explain plan displays execution plans chosen by the Oracle optimiser, listing the optimiser decisions. –At a base level a user needs to have created a table called PLAN_TABLE by running the script: ORA_HOME\rdbms\admin\utlxplan.sql. –The data from an explain is stored in that table. You then use some utility to read that table. Oracle provide a script: ORA_HOME\rdbms\admin\UTLXPLS.SQL.

Using Explain Plan EXPLAIN PLAN FOR SELECT a.composer, b.cdid, b.title FROM composers a, cds b WHERE a.composer like 'V%' AND b.composerid = a.composerid ORDER BY a.composer ;

Using Explain Plan

School of Computing and Management Sciences © Sheffield Hallam University Old Wives Tales “ The driving table is the last table in the FROM list ” This is only true if: –the optimiser can’t decide which table to drive from based on the WHERE clause “Unions GOOD, Ors BAD” –IT Depends! (stock tuning response) Primarily: is there an index?

School of Computing and Management Sciences © Sheffield Hallam University Union/OR and Indexing set autotrace on stat set timing on spool uniontest drop index i_grade ; select personid, sname from persons where sname like 'SA%' or gradeid = 2 ; select personid, sname from persons where sname like 'SA%' UNION ALL select personid, sname from persons where gradeid = 2 ; create index i_grade on persons(gradeid) ; select personid, sname from persons where sname like 'SA%' or gradeid = 2 ; select personid, sname from persons where sname like 'SA%' UNION ALL select personid, sname from persons where gradeid = 2 ; spool off

School of Computing and Management Sciences © Sheffield Hallam University Union/OR and Indexing OR, no Index Elapsed: 00:00: db block gets 72 consistent gets 7 sorts (memory) UNION, no Index Elapsed: 00:00: db block gets 62 consistent gets 2 sorts (memory) OR, with Index Elapsed: 00:00: db block gets 42 consistent gets 3 sorts (memory) UNION, with Index Elapsed: 00:00: db block gets 62 consistent gets 2 sorts (memory) No IndexIndexed

School of Computing and Management Sciences © Sheffield Hallam University More Query efficiency Reduce the number of trips to the database –Example scenario: You need to pull the name back to a client application from Persons table for 2 people. –Do you: Write 2 selects Write a select using an IN Write a self join? -- Two distinct goes spool twogoes select sname, fname from persons where personid = 406 ; select sname, fname from persons where personid = 447 ; Select sname, fname from persons where personid in (406, 447) ; select a.sname, a.fname, b.sname, b.fname from persons a, persons b where a.personid = 406 and b.personid = 447 ;

School of Computing and Management Sciences © Sheffield Hallam University More efficiency tips Avoid HAVING if you can filter with WHERE –having clause filters only after all rows fetched Use Stored Procedures Client or Server processing of cursors?

More efficiency tips Test Test again.

School of Computing and Management Sciences © Sheffield Hallam University Further reading Corrigan and Garry, Oracle Performance Tuning, O’Reilly 1995 Cary V. Millsap, Performance Management: Myths & Facts, Oracle Corporation June 28, The Oracle documentation! (There is a whole section on Performance Tuning)