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.

Slides:



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

12 Copyright © 2005, Oracle. All rights reserved. Query Rewrite.
Tuning Oracle SQL The Basics of Efficient SQLThe Basics of Efficient SQL Common Sense Indexing The Optimizer –Making SQL Efficient Finding Problem Queries.
Database Tuning. Overview v After ER design, schema refinement, and the definition of views, we have the conceptual and external schemas for our database.
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
SQL Tuning Briefing Null is not equal to null but null is null.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Clarity Educational Community Clarity Educational Community Creating and Tuning SQL Queries that Engage Users.
Set operators Union Minus Intersect match ALL COLUMNS must have same ID, column names.
Introduction to Structured Query Language (SQL)
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
Introduction to Structured Query Language (SQL)
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Microsoft Access 2010 Chapter 7 Using SQL.
AN INTRODUCTION TO EXECUTION PLAN OF QUERIES These slides have been adapted from a presentation originally made by ORACLE. The full set of original slides.
Introduction to SQL Structured Query Language Martin Egerhill.
Executing Explain Plans and Explaining Execution Plans Craig Martin 01/20/2011.
Oracle Database Administration Lecture 6 Indexes, Optimizer, Hints.
Getting SQL Right the First Try (Most of the Time!) May, 2008 ©2007 Dan Tow, All rights reserved SingingSQL Presents.
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.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
Query Optimization Arash Izadpanah. Introduction: What is Query Optimization? Query optimization is the process of selecting the most efficient query-evaluation.
1 Optimizing Your ColdFusion Applications for Oracle Justin Fidler, CNA, CPS, CCFD Chief Technology Officer Bantu, Inc. 8 May 2001.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
11-1 Improve response time of interactive programs. Improve batch throughput. To ensure scalability of applications load vs. performance. Reduce system.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
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.
Database Management COP4540, SCS, FIU Physical Database Design (2) (ch. 16 & ch. 6)
Module 4 Database SQL Tuning Section 3 Application Performance.
Copyright © Curt Hill Joins Revisited What is there beyond Natural Joins?
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
1 Copyright © 2005, Oracle. All rights reserved. Following a Tuning Methodology.
Sorting and Joining.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
Database Systems, 8 th Edition SQL Performance Tuning Evaluated from client perspective –Most current relational DBMSs perform automatic query optimization.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
 CONACT UC:  Magnific training   
LAB: Web-scale Data Management on a Cloud Lab 11. Query Execution Plan 2011/05/27.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Tuning Oracle SQL The Basics of Efficient SQL Common Sense Indexing
CHAPTER 19 Query Optimization. CHAPTER 19 Query Optimization.
Tuning Transact-SQL Queries
MySQL Subquery Source: Dev.MySql.com
Indexes By Adrienne Watt.
Indices.
CHAPTER 7: ADVANCED SQL.
Tuning Queries from (E&N)
Four Rules For Columnstore Query Performance
Lecture 3 Finishing SQL
Contents Preface I Introduction Lesson Objectives I-2
All about Indexes Gail Shaw.
Presentation transcript:

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 Code Tuning –Phys. / Config. Tuning Written for myself –Writing doesnt make you rich Proof of what works –and what doesnt Three parts: –Data Model Tuning –SQL Code Tuning –Phys. / Config. Tuning

The Basics of Efficient SQL SQL Code TuningSQL Code Tuning –What is SQL? –The Basics of Efficient SQL –Common Sense Indexing –The Optimizer: Making SQL Efficient –Finding Problem Queries –Oracle Enterprise Manager SQL Code TuningSQL Code Tuning –What is SQL? –The Basics of Efficient SQL –Common Sense Indexing –The Optimizer: Making SQL Efficient –Finding Problem Queries –Oracle Enterprise Manager

Some General Guidelines KISS (Keep It Simple Stupid)KISS (Keep It Simple Stupid) –easier for the Optimizer depends on query complexity –easier for you to tune Dont go too far with granularityDont go too far with granularity –connecting and disconnecting for all queries KISS (Keep It Simple Stupid)KISS (Keep It Simple Stupid) –easier for the Optimizer depends on query complexity –easier for you to tune Dont go too far with granularityDont go too far with granularity –connecting and disconnecting for all queries

KISS –easier for the Optimizer depends on query complexity –easier for you to tune Dont go too far with granularity –connecting and disconnecting for all queries KISS –easier for the Optimizer depends on query complexity –easier for you to tune Dont go too far with granularity –connecting and disconnecting for all queries Watch Granularity

Is intelligent –better with simple queries Is usually correct Nothing is set in stone Verify SQL code efficiency EXPLAIN PLAN –use EXPLAIN PLAN –SET AUTOTRACE ON EXPLAIN –$ORACLE_HOME/rdbms/admin/utlxplan.sql Is intelligent –better with simple queries Is usually correct Nothing is set in stone Verify SQL code efficiency EXPLAIN PLAN –use EXPLAIN PLAN –SET AUTOTRACE ON EXPLAIN –$ORACLE_HOME/rdbms/admin/utlxplan.sql The Optimizer

Basic Query Components SELECTSELECT –get data WHERE –filtering ORDER BY –sorting GROUP BY –aggregating SELECTSELECT –get data WHERE –filtering ORDER BY –sorting GROUP BY –aggregating

Basic Query Components SELECT –get data WHEREWHERE –filtering ORDER BY –sorting GROUP BY –aggregating SELECT –get data WHEREWHERE –filtering ORDER BY –sorting GROUP BY –aggregating

Basic Query Components SELECT –get data WHERE –filtering ORDER BYORDER BY –sorting GROUP BY –aggregating SELECT –get data WHERE –filtering ORDER BYORDER BY –sorting GROUP BY –aggregating

Basic Query Components SELECT –get data WHERE –filtering ORDER BY –sorting GROUP BYGROUP BY –aggregating SELECT –get data WHERE –filtering ORDER BY –sorting GROUP BYGROUP BY –aggregating

SELECTSELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BY SELECTSELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BY Queries * SELECT * FROM division; SELECT division_id, name, city, state, country FROM division; SELECT division_id FROM division;

SELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BY SELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BY Queries Avoid unintentional full table scans SELECT * FROM division WHERE country LIKE '%a%'; Match indexes Exact hits (equality) SELECT * FROM division WHERE division_id = 1; Range scans / skip scans / full index scans EXISTS (correlate) faster than IN Biggest filters first Full table scans can sometimes be faster Avoid unintentional full table scans SELECT * FROM division WHERE country LIKE '%a%'; Match indexes Exact hits (equality) SELECT * FROM division WHERE division_id = 1; Range scans / skip scans / full index scans EXISTS (correlate) faster than IN Biggest filters first Full table scans can sometimes be faster

Queries Resorts on result after WHERE and GROUP BY Dont repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id FROM division ORDER BY division_id; by WHERE SELECT * FROM division WHERE division_id < 10 ORDER BY division_id; GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; by DISTINCT SELECT DISTINCT(state) FROM division ORDER BY state; by indexes SELECT division_id FROM division ORDER BY division_id; Resorts on result after WHERE and GROUP BY Dont repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id FROM division ORDER BY division_id; by WHERE SELECT * FROM division WHERE division_id < 10 ORDER BY division_id; GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; by DISTINCT SELECT DISTINCT(state) FROM division ORDER BY state; by indexes SELECT division_id FROM division ORDER BY division_id; SELECT –FOR UPDATE Filtering –WHERE ORDER BYORDER BY –often ignored –query complexity GROUP BY SELECT –FOR UPDATE Filtering –WHERE ORDER BYORDER BY –often ignored –query complexity GROUP BY

SELECT –FOR UPDATE Filtering –WHERE ORDER BYORDER BY –often ignored –query complexity GROUP BY SELECT –FOR UPDATE Filtering –WHERE ORDER BYORDER BY –often ignored –query complexity GROUP BY Queries Resorts on result after WHERE and GROUP BY Dont repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id FROM division ORDER BY division_id; by WHERE SELECT * FROM division WHERE division_id < 10 ORDER BY division_id; GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; by DISTINCT SELECT DISTINCT(state) FROM division ORDER BY state; by indexes SELECT division_id FROM division ORDER BY division_id; Resorts on result after WHERE and GROUP BY Dont repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id FROM division ORDER BY division_id; by WHERE SELECT * FROM division WHERE division_id < 10 ORDER BY division_id; GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; by DISTINCT SELECT DISTINCT(state) FROM division ORDER BY state; by indexes SELECT division_id FROM division ORDER BY division_id;

Queries GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; HAVING (filters aggregate) SELECT state, COUNT(state) FROM division GROUP BY state HAVING COUNT(state) > 1; use WHERE SELECT state, COUNT(state) FROM division WHERE state = 'NY' GROUP BY state; not HAVING SELECT state, COUNT(state) FROM division GROUP BY state HAVING state = 'NY'; GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; HAVING (filters aggregate) SELECT state, COUNT(state) FROM division GROUP BY state HAVING COUNT(state) > 1; use WHERE SELECT state, COUNT(state) FROM division WHERE state = 'NY' GROUP BY state; not HAVING SELECT state, COUNT(state) FROM division GROUP BY state HAVING state = 'NY'; SELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BYGROUP BY –Use WHERE not HAVING SELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BYGROUP BY –Use WHERE not HAVING

SELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BYGROUP BY –Use WHERE not HAVING SELECT –FOR UPDATE Filtering –WHERE ORDER BY –often ignored –query complexity GROUP BYGROUP BY –Use WHERE not HAVING Queries GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; HAVING (filters aggregate) SELECT state, COUNT(state) FROM division GROUP BY state HAVING COUNT(state) > 1; use WHERE SELECT state, COUNT(state) FROM division WHERE state = 'NY' GROUP BY state; not HAVING SELECT state, COUNT(state) FROM division GROUP BY state HAVING state = 'NY'; GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; HAVING (filters aggregate) SELECT state, COUNT(state) FROM division GROUP BY state HAVING COUNT(state) > 1; use WHERE SELECT state, COUNT(state) FROM division WHERE state = 'NY' GROUP BY state; not HAVING SELECT state, COUNT(state) FROM division GROUP BY state HAVING state = 'NY';

FunctionsFunctions –conversions –avoid indexes –function based indexing –DECODE –CASE expressions –set operators (UNION) Use sequencesUse sequences Use equality (=) or range scans (>)Use equality (=) or range scans (>) –Avoid negatives (!=, NOT) –Avoid LIKE FunctionsFunctions –conversions –avoid indexes –function based indexing –DECODE –CASE expressions –set operators (UNION) Use sequencesUse sequences Use equality (=) or range scans (>)Use equality (=) or range scans (>) –Avoid negatives (!=, NOT) –Avoid LIKE Other Stuff

JoinsJoins –avoid Cartesian Products –avoid anti joins –avoid outer joins –perhaps replace –multiple table complex joins –with subquery semi joins and inline views Be careful with viewsBe careful with views JoinsJoins –avoid Cartesian Products –avoid anti joins –avoid outer joins –perhaps replace –multiple table complex joins –with subquery semi joins and inline views Be careful with viewsBe careful with views