Advanced SQL Application Tuning: Find the Proverbial Needle in the Haystack Bert Scalzo, Ph.D.

Slides:



Advertisements
Similar presentations
Youre Smarter than a Database Overcoming the optimizers bad cardinality estimates.
Advertisements

The Basics of Efficient SQL Written for myself –Writing doesnt make you rich Proof of what works –and what doesnt Three parts: –Data Model Tuning –SQL.
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,
Copyright © SoftTree Technologies, Inc. DB Tuning Expert.
SQL Tuning Briefing Null is not equal to null but null is null.
Chapter 9. Performance Management Enterprise wide endeavor Research and ascertain all performance problems – not just DBMS Five factors influence DB performance.
Module 13: Performance Tuning. Overview Performance tuning methodologies Instance level Database level Application level Overview of tools and techniques.
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
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.
Copyright © 2006 Quest Software Toad Tips & Tricks for DBA’s Bert Scalzo, Domain Expert, Oracle Solutions
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
Introduction to Structured Query Language (SQL)
Agenda Overview of the optimizer How SQL is executed Identifying statements that need tuning Explain Plan Modifying the plan.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 11 Database Performance Tuning and Query Optimization.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Copyright © 2006 Quest Software Data Modeling: It’s All About the Relationships Presenter: Bert Scalzo, Oracle Domain Expert AUDIO.
© 2008 Quest Software, Inc. ALL RIGHTS RESERVED. Managing Database Change with Data Modeling Bert Scalzo, PhD
Optimizing Data Warehouse Loads via Parallel Pro-C and Parallel/Direct-Mode SQL Bert Scalzo, Ph.D.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
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.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Improving Efficiency of I/O Bound Systems More Memory, Better Caching Newer and Faster Disk Drives Set Object Access (SETOBJACC) Reorganize (RGZPFM) w/
Oracle9i Performance Tuning Chapter 1 Performance Tuning Overview.
Department of Computer Science and Engineering, HKUST Slide Query Processing and Optimization Query Processing and Optimization.
1 Optimizing Your ColdFusion Applications for Oracle Justin Fidler, CNA, CPS, CCFD Chief Technology Officer Bantu, Inc. 8 May 2001.
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.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
SEMANTEC 1 Oracle Performance Tuning - Part I Krasen Paskalev Oracle 8i Certified DBA.
Building and Optimizing Data Warehouse "Star Schemas" with MySQL Bert Scalzo, Ph.D.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
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.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
Module 4 Database SQL Tuning Section 3 Application Performance.
DATABASE ADMINISTRATION Pertemuan ke-7. Application Performance source : Database Administration the complete guide to practices and procedures chapter.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
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.
IMS 4212: Database Implementation 1 Dr. Lawrence West, Management Dept., University of Central Florida Physical Database Implementation—Topics.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Optimization and Administartion of a Database Management Systems Krystian Zieja.
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
Copyright © 2006 Quest Software Quest RAC Tools Bert Scalzo, Domain Expert, Oracle Solutions
 CONACT UC:  Magnific training   
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
Tuning Oracle SQL The Basics of Efficient SQL Common Sense Indexing
Tuning Transact-SQL Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using the Set Operators
Database Performance Tuning and Query Optimization
Writing Correlated Subqueries
Using the Set Operators
Chapter 11 Database Performance Tuning and Query Optimization
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using the Set Operators
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

Advanced SQL Application Tuning: Find the Proverbial Needle in the Haystack Bert Scalzo, Ph.D.

About the Author Oracle DBA from 4 through 10g Worked for Oracle Education Worked for Oracle Consulting Holds several Oracle Masters BS, MS and PhD in Computer Science MBA and insurance industry designations Articles in Oracle Magazine Oracle Informant PC Week (now E-Magazine)

About Quest Software

Where Can We Look What is Tunable: Hardware Operating System Database Network Application Too often people attempt to improve database application performance by focusing on just the first three: hardware, OS and RDBMS …

Performance Pyramid Application DBMS OS Hardware Network

Golden Rule #1: It’s the application stupid! Most application performance problems result from an application’s poor general design or grossly inefficient SQL Where Should We Look Do your developers know: Sub-queries (correlated and not) Outer Joins Minus Union and Union-All

Performance Pyramid DBMS OS Hardware Network Application

Hardware Won’t Compensate Often people have unrealistic expectation that using expensive hardware is only way to obtain optimal application performance CPU SMP MPP Disk IO 15,000 RPM RAID (EMC) OS UNIX 64-bit Oracle OPS / PQO 64-bit

Hardware Tuning Example Problem: Data load runtime 4+ hours MHz 64-bit CPU’s 4 Gigabytes UNIX RAM 2 Gigabytes EMC Cache RAID 5 (slower on writes) Attempt #1: Bought more hardware MHz 64-bit CPU’s 8 Gigabytes UNIX RAM 4 Gigabytes EMC Cache RAID 1 (faster on writes) Runtime still 4+ hours !!!

Application Tuning Example Attempt #2: Redesigned application Convert PL/SQL to Pro-C Run 16 streams in parallel Better utilize UNIX capabilities Run time = 20 minutes !!! Attempt #3: Tuned the SQL code Tune individual SQL statements Use Dynamic SQL method # 2 Prepare SQL outside loop Execute Prep SQL in loop Run time = 15 minutes !!!

Lesson Learned Hardware: Cost approximately $1,000,000 System downtime for upgrades Zero runtime improvement Loss of credibility with customer Redesign: 4 hours DBA $150/hour = $ hours Developer $100/hour = $2000 Total cost = $2600 or 385 times less Golden Rule #2: Application redesign much cheaper than hardware!

What Exactly is Tuning How you define tuning implicates how you will approach it. And the approach directly effects the tuning ROI that you can expect Tuning is an art requiring creativity Tuning is a science requiring regimen Thus, we must use both intuition and rules …

How to Approach Tuning Tuning Approaches: Reactively (after the fact)- intuition Proactively (as you go)- rules Hybrid (combination)- both Tuning as you go may be impractical, and after you’re done it may be too late. Often it’s best to apply general tuning guidelines as you go, and then identify problem areas for improvement. Golden Rule #3: Best to tune before, during, and after!

Who will find the needle first?

Carpenter Needs Tools Too many people try to tune applications the hard way, by visual inspection based upon their own limited SQL knowledge But SQL as a language is far too complex, and Oracle’s optimizers too dynamic to rely solely on the intuition of developers and DBA’s … Golden Rule #4: Utilize tools to tune SQL!

Commercial Tuning Tools Reactive Quest’s Spotlight on Oracle Oracle’s Performance Overview & Manager (OEM) Embarcadero’s DBArtisan & Performance Center BMC’s SmartDBA Cockpit & DBXray Proactive Quest’s SQL Navigator & TOAD with SQLab Xpert option Oracle’s UTLPLP.SQL script (parallel explain plan script) Oracle’s SQL Analyze (OEM) Embarcadero’s Rapid SQL BMC’s SQL Programmer CA’s SQL Station (Plan Analyzer) Hybrid Quest’s SQLab Xpert Oracle’s trace files and TKPROF utility Oracle’s UTLBSTAT and UTLESTAT scripts Oracle’s Stats Pack (only for versions 8i and newer) Quest’s SQL Expert (formerly LECCO Technology)

Reactive: Spotlight

High Logical & Physical IO’s

High Read & Write Waits

Examine Top SQL Consumers

Explain Plan for Top SQL

Reactive Summary After the fact tuning dumped on the DBA. Often too little, too late... Pros: DBA’s tend to know SQL very well Cons: DBA’s often focus more on OS and RDBMS DBA’s often don’t know the application code DBA’s often don’t know the data’s nature DBA’s often don’t know the business rules DBA’s generally have way too much to do

Proactive: TOAD Proc Editor

Proactive: TOAD SQL Editor

Proactive: TOAD SQL Tuning

Proactive Summary Tuning performed during coding by Developer. Often too much, too soon... Pros: Developers focus is application code & data Developers know the relevant business rules Cons: Developers must take time to identify problem code Developers often don’t tune SQL as well as DBA Developers often measured by lines of code Developers often not part of production support

Hybrid: SQLab

Explain Plan for Top SQL

SQLab can Advise & Fix!!!

Hybrid Summary Best of both worlds for DBA’s and Developers.

SQL Guidelines Rule #1: Watch Indexed WHERE Conditions Assume index on address (city, state) non-leading index column references cannot use indexes where state = 'TX'[Index Not used] where city = 'DALLAS'[Index Used] where state = 'TX' and city = 'DALLAS'[Index Used] NOT, != and <> disable index use where state not in ('TX', 'FL','OH')[Index Not used] where state != 'TX'[Index Not used] NULL value references can never use indexes where state IS NULL[Index Not used] where state IS NOT NULL[Index Not used] expression references can never use indexes where substr(city,1,3) = 'DAL'[Index Not used] where city like 'DAL%'[Index Used] where city || state = 'DALLASTX'[Index Not used] where city = 'DALLAS' and state = 'TX‘[Index Used] where salary * 12 >= 24000[Index Not used] where salary >= 2000[Index Used]

SQL Guidelines Rule #2:Watch Non-Indexed WHERE Conditions Oracle evaluates Non-Indexed conditions linked by AND bottom up Bad: select * from address where areacode = 972 and type_nr = (select seq_nr from code_table where type = ‘HOME’) Good: select * from address where type_nr = (select seq_nr from code_table where type = ‘HOME’) and areacode = 972 Oracle evaluates Non-Indexed conditions linked by OR top down Bad: select * from address where type_nr = (select seq_nr from code_table where type = ‘HOME’) or areacode = 972 Good: select * from address where areacode = 972 or type_nr = (select seq_nr from code_table where type = ‘HOME’)

SQL Guidelines Rule #3:Order Table in the FROM Clause important under rule based optimizer, and won't hurt under cost based optimizer order FROM clauses in descending order of table sizes based upon row counts for example select * from larger table, smaller table select * from larger table, smaller table, smallest table select * from larger table, smaller table, associative table Note – rule based optimizer only

SQL Guidelines Rule #4:Consider IN or UNION in place of OR if columns are not indexed, stick with OR if columns are indexed, use IN or UNION in place of OR IN example Bad: select * from address where state = 'TX‘ or state = 'FL‘ or state = 'OH‘ Good: select * from address where state in ('TX','FL','OH') UNION example Bad: select * from address where state = ‘TX’ or areacode = 972 Good: select * from address where state = ‘TX’ union select * from address where areacode = 972

SQL Guidelines Rule #5:Weigh JOIN versus EXISTS Sub-Query use table JOIN instead of EXISTS sub-query when the percentage of rows returned from the outer sub-query is high select e.name, e.phone, e.mailstop from employee e, department d where e.deptno = d.deptno and d.status = ‘ACTIVE’ use EXISTS sub-query instead of table JOIN when the percentage of rows returned from the outer sub-query is low select e.name, e.phone, e.mailstop from employee e where e.deptno in (select d.deptno from department d where d.status != ‘ACTIVE’)

SQL Guidelines Rule #6:Consider EXISTS in place of DISTINCT avoid joins that use DISTINCT, use EXISTS sub-query instead Bad: select distinct deptno, deptname from emp, dept where emp.deptno = dept.deptno Good: select deptno, deptname from dept where exists (select ‘X’ from emp where emp.deptno = dept.deptno) Note – only has to find one match

SQL Guidelines Rule #7:Consider NOT EXISTS in place of NOT IN avoid sub-queries that use NOT IN, use NOT EXISTS instead Bad: select * from emp where deptno not in (select deptno from dept where deptstatus = ‘A’) Good: select * from emp where not exists (select ‘X’ from dept where deptstatus = ‘A’ and dept.deptno = emp.deptno) Note – only has to find one non-match

SQL Guidelines Rule #8:COUNT Using Indexed Column or Asterisk when counting rows, use COUNT on indexed column or asterisk select count(indexed_column) from table[Most Efficient] select count(*) from table Select count(non_indexed_column) from table select count(1) from table Note – rule based optimizer only

SQL Guidelines Rule #9:Ordering Via the WHERE Clause a dummy WHERE clause referencing an indexed column will retrieve all records in ascending order (descending for 8i descending index) not perform a costly sort operation Bad: select * from address order by city Good: select * from address where city > ‘’

SQL Guidelines Rule #10:Use PL/SQL to reduce network traffic Utilize PL/SQL to group related SQL commands and thereby reduce network traffic Bad: select city_name, state_code into :v_city, :v_sate from zip_codes where zip_code = ‘75022’; insert into customer (‘Bert Scalzo’,’75022’, :v_city, v_state); Good: begin select city_name, state_code into :v_city, :v_sate from zip_codes where zip_code = ‘75022’; insert into customer (‘Bert Scalzo’,’75022’, :v_city, v_state); end; /