Tuning Transact-SQL Queries

Slides:



Advertisements
Similar presentations
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.
Advertisements

Advanced SQL (part 1) CS263 Lecture 7.
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.
Query Optimization Reserves Sailors sid=sid bid=100 rating > 5 sname (Simple Nested Loops) Imperative query execution plan: SELECT S.sname FROM Reserves.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
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.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
Introduction to Structured Query Language (SQL)
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
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.
Database Systems More SQL Database Design -- More SQL1.
Access Path Selection in a Relation Database Management System (summarized in section 2)
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.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Access Path Selection in a Relational Database Management System Selinger et al.
Database Management 9. course. Execution of queries.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
Chapter 5 Index and Clustering
Query Execution. Where are we? File organizations: sorted, hashed, heaps. Indexes: hash index, B+-tree Indexes can be clustered or not. Data can be stored.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to.
Database Systems, 8 th Edition SQL Performance Tuning Evaluated from client perspective –Most current relational DBMSs perform automatic query optimization.
Execution Plans Detail From Zero to Hero İsmail Adar.
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.
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
Database Performance Tuning &
Choosing Access Path The basic methods.
Database Application Development
Using the Set Operators
Chapter 12: Query Processing
Database Performance Tuning and Query Optimization
Relational Algebra Chapter 4, Part A
Writing Correlated Subqueries
Introduction to Execution Plans
Chapter 15 QUERY EXECUTION.
Access Path Selection in a Relational Database Management System
Evaluation of Relational Operations: Other Operations
Using the Set Operators
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Tuning Queries from (E&N)
Advance Database Systems
Reporting Aggregated Data Using the Group Functions
Introduction to Execution Plans
Implementation of Relational Operations
Chapter 8 Advanced SQL.
Chapter 11 Database Performance Tuning and Query Optimization
CS222P: Principles of Data Management Notes #13 Set operations, Aggregation, Query Plans Instructor: Chen Li.
Evaluation of Relational Operations: Other Techniques
IST 318 Database Administration
Reporting Aggregated Data Using the Group Functions
Database Administration
Using the Set Operators
Introduction to Execution Plans
Reporting Aggregated Data Using the Group Functions
Query Optimization Techniques
Introduction to Execution Plans
Lecture 20: Query Execution
Query Tuning.
Database Application Development
Presentation transcript:

Tuning Transact-SQL Queries Learn the Strengths and Weaknesses of the Optimizer One of the largest factors determining performance is T-SQL! Test not only for efficient plans but also semantic correctness ! Optimizer “Plans” Join Order 4 Tables at a Time Every combination of “4-table permutations” is costed (e.g. {t1,t2,t3,t4}, {t1,t2,t3,t5}, {t1,t2,t3,t6}, {t1,t2,t4,t5}, {t1,t2,t4,t6}, {t2,t1,t3,t4}, etc.). Best “outer” table is saved and the remaining combinations are “costed” to determine “next outer-most” table until done. Scrutinize these types of queries for best possible plan. Adding redundant predicates (eg. where a = b and b = c and a = c) gives the optimizer more choices! .

Tuning Transact-SQL Queries Learn the Strengths and Weaknesses of the Optimizer Avoid the following, if possible : Mathematical Manipulation of SARGs SELECT name FROM employee WHERE salary * 12 > 100000 Incompatible Datatypes (Columns, SARGs, or SPROC Parameters) Float & Int, Char & Varchar, Binary & Varbinary are Incompatible Int & Intn (allow nulls) OK Multiple “OR” Clauses (especially on different columns in same table) If any portion of the OR clause requires a table scan, it will ! OR strategy requires cost of creating and sorting a work table. Evaluate UNIONs as an alternative ! Not Using the Leading Index Keys (unless query is covered) Without leading key, B-tree index can’t be searched ! Not Equal Expressions (!=)

Tuning Transact-SQL Queries Subquery Processing in SQL Server Subquery Flattening (Transform to Normal or Existence Join) Normal joins execute more quickly because best join order can be chosen Existence joins stop after first match Queries that CAN be flattened include: Many IN, ANY, and EXISTS subqueries Expression subqueries (ie., column {<, <=, >, >=, !=, =} subquery) with unique joins or returning unique columns Queries that CAN NOT be flattened include: Most NOT IN, NOT EXISTS, ALL subqueries IN, ANY, EXISTS subqueries in an OR clause IN, ANY, EXISTS subqueries in a correlated subquery with aggregates Expression subqueries without unique joins or not returning unique columns Subquery Materialization Evaluate subquery once before outer query and store results for later use Makes sense only when subquery will evaluate to same result for every outer row! Non-correlated expression subqueries select title_id from titles where total_sales = (select max(total_sales) from sales) Quantified subqueries (ie., IN, ALL, ANY, EXISTS) containing aggregates select name from employees where salary IN (select max(salary) from employee group by office)

Tuning Transact-SQL Queries Simple Tricks to Use for Good Performance Use “>=“ rather than “>“ Whenever Possible ý “select * from foo where x > 3” must scan all index pages where x=3 just to find the first qualified row! þ “select * from foo where x >= 4” can go directly to first qualified row. n Use “EXISTS” and “IN”rather than “NOT EXISTS” and “NOT IN” (or COUNT) þ Faster in both subqueries and IF statements þ Easy to re-write sprocs using EXISTS or IN. For example, if not exists (select * from ...) begin ... /* statement group */ end could be re-written as : if exists (select * from ...) goto exists_label exists_label: ... þ EXISTS stop after 1st match as opposed to COUNT which does all the I/O to count!

Tuning Transact-SQL Queries Creating Tables in Stored Procedures n When tables are created in the same stored procedure in which they are used, the optimizer can’t know their size. n In these cases, the optimizer assumes the table has 10 data pages & 100 rows ý This assumption can lead to poor plans when the table is large. ý Previously recommended creating the table outside the stored procedure in which it is used. This allows the optimizer to see its real size. For example, create proc p as select * into #huge_result from ... select * from foo, #huge_result where ... /* may result in #huge_result as outer table */ can be re-written as : exec s create proc s as

Tuning Transact-SQL Queries Use Parameters, Not Local Variables, in WHERE Clauses n The optimizer can’t predict the value of a declared variable at compile-time. n The optimizer does know the value of a parameter to the sproc at compile-time perhaps leading the “right” plan. n To avoid using local variables in a WHERE clause, split up the sproc whenever possible (see warning on previous page). For example, create proc p as declare @x int select @x = col1 from foo1 where ... select * from foo2 where col2 = @x can be re-written as : exec s @x create proc s @x int as

Tuning Transact-SQL Queries Enhanced SHOWPLAN in SQL Server Shows the plan chosen as “most efficient” by the optimizer. Run all queries through during development & testing to ensure accurate access model and performance SELECT title_id FROM titles WHERE total_sales > ALL (SELECT total_sales FROM titles WHERE type = “business”) QUERY PlAN FOR STATEMENT 1 (at line 1). STEP 1 The type of query is SELECT FROM TABLE titles Nested iteration Table scan, Ascending scan, Positioned at start of table Run Subquery 1 (at nesting level 1) Using I/O Size 16kbytes, With LRU Buffer Replacement strategy NESTING LEVEL 1 SUBQUERIES FOR STATEMENT 1 QUERY PLAN FOR SUBQUERY 1 (at nesting level 1 and at line 2) Correlated Subquery, Subquery under an ALL predicate STEP 1 The type of query is SELECT Evaluate ungrouped ANY AGGREGATE FROM TABLE titles EXISTS TABLE: nested iteration Table scan, Ascending scan, Positioned at start of table Using I/O Size 16kbytes, With LRU Buffer Replacement strategy END OF QUERY PLAN FOR SUBQUERY 1 n Verify Proper Index Selections. Watch out for table scans. n Verify Proper Join Order. Watch out for large tables. n Verify Proper I/O Block Size and Cache Strategy. n Verify Proper Subquery Optimizations n Look at TEMPDB usage. May require optimizations for multi-user issues.