EXECUTION PLANS By Nimesh Shah, Amit Bhawnani. Outline  What is execution plan  How are execution plans created  How to get an execution plan  Graphical.

Slides:



Advertisements
Similar presentations
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
Advertisements

Understanding SQL Server Query Execution Plans
Equality Join R X R.A=S.B S : : Relation R M PagesN Pages Relation S Pr records per page Ps records per page.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Algorithms for SELECT and JOIN Operations (8) Implementing the JOIN Operation: Join (EQUIJOIN, NATURAL.
Database Management Systems 3ed, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 14, Part B.
Database Management Systems, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
Database Management Systems, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
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.
Query Evaluation. SQL to ERA SQL queries are translated into extended relational algebra. Query evaluation plans are represented as trees of relational.
Module 13: Optimizing Query Performance. Overview Introduction to the Query Optimizer Obtaining Execution Plan Information Using an Index to Cover a Query.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Query Processing (overview)
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
CSCI 5708: Query Processing I Pusheng Zhang University of Minnesota Feb 3, 2004.
1 Query Processing: The Basics Chapter Topics How does DBMS compute the result of a SQL queries? The most often executed operations: –Sort –Projection,
The query processor does what the query plan tells it to do A “good” query plan is essential for a well- performing.
Query Optimization, part 2 CS634 Lecture 13, Mar Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 14 – Join Processing.
Module 12: Optimizing Query Performance. Overview Introducing the Query Optimizer Tuning Performance Using SQL Utilities Using an Index to Cover a Query.
Denny Cherry Manager of Information Systems MVP, MCSA, MCDBA, MCTS, MCITP.
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 13 Query Processing Melissa Jamili CS 157B November 11, 2004.
Query Processing. Steps in Query Processing Validate and translate the query –Good syntax. –All referenced relations exist. –Translate the SQL to relational.
CS 338Query Evaluation7-1 Query Evaluation Lecture Topics Query interpretation Basic operations Costs of basic operations Examples Textbook Chapter 12.
12.1Database System Concepts - 6 th Edition Chapter 12: Query Processing Overview Measures of Query Cost Selection Operation Join Operation Sorting 、 Other.
Computing & Information Sciences Kansas State University Tuesday, 03 Apr 2007CIS 560: Database System Concepts Lecture 29 of 42 Tuesday, 03 April 2007.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Lecture 1- Query Processing Advanced Databases Masood Niazi Torshiz Islamic Azad university- Mashhad Branch
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
Session 1 Module 1: Introduction to Data Integrity
Query Optimization CMPE 226 Database Systems By, Arjun Gangisetty
CPSC 404, Laks V.S. Lakshmanan1 Evaluation of Relational Operations – Join Chapter 14 Ramakrishnan and Gehrke (Section 14.4)
Query Processing CS 405G Introduction to Database Systems.
File Processing : Query Processing 2008, Spring Pusan National University Ki-Joune Li.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Eugene Meidinger Execution Plans
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.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Microsoft SQL Server 2005 Advanced SQL Programming and Optimization
Database Management System
Chapter 12: Query Processing
Introduction to Execution Plans
Chapter 15 QUERY EXECUTION.
Database Management Systems (CS 564)
Evaluation of Relational Operations: Other Operations
The Key to the Database Engine
Execution Plans Demystified
SQL Server Query Plans Journeyman and Beyond
Introduction to reading execution plans
Lecture 2- Query Processing (continued)
Dave Bland LinkedIn SQL Server Execution Plans: How to use them to find performance bottlenecks Dave Bland LinkedIn
Introduction to Execution Plans
Implementation of Relational Operations
EXECUTION PLANS Quick Dive.
Execution plans Eugene
Evaluation of Relational Operations: Other Techniques
Diving into Query Execution Plans
Introduction to Execution Plans
Introduction to Execution Plans
Evaluation of Relational Operations: Other Techniques
All about Indexes Gail Shaw.
Presentation transcript:

EXECUTION PLANS By Nimesh Shah, Amit Bhawnani

Outline  What is execution plan  How are execution plans created  How to get an execution plan  Graphical  Text

What is execution plan  The execution plan is created by the optimizer and used for the execution of a statement. Once the execution of a statement has started, the execution plan is followed in a step-by-step manner to retrieve the required result. It is an explanation of the steps to perform during statement execution.  An execution plan is composed of primitive operations. Examples of primitive operations are: reading a table completely, using an index, performing a nested loop or a hash join.

How are execution plans created

 Parse: - The first phase is to parse the SQL query for syntaxes and create a query processor tree which defines logical steps to execute the SQL. This process is also called as ‘algebrizer’.  Optimize: - The next step is to find a optimized way of executing the query processor tree defined by the ‘algebrizer’. This task is done by using ‘Optimizer’.’Optimizer’ takes data statistics like how many rows, how many unique data exist in the rows, do the table span over more than one page etc. In other words it takes information about data’s data. These all statistics are taken, the query processor tree is taken and a cost based plan is prepared using resources, CPU and I/O. The optimizer generates and evaluates many plan using the data statistics, query processor tree, CPU cost, I/O cost etc to choose the best plan. The optimizer arrives to an estimated plan, for this estimated plan it tries to find an actual execution plan in the cache. Estimated plan is basically which comes out from the optimizer and actual plan is the one which is generated once the query is actually executed.  Execute: - The final step is to execute the plan which is sent by the optimizer.

How are execution plans created  The creation of an execution plan takes time.  Not every execution option is always explored, a “good enough” execution plan is often generated, then sent to the database engine for execution.  The execution plan is estimated, and may change when the T-SQL is actually executed by the database engine.  Execution plans are usually cached (in the plan cache) for later use so that it can be reused if an identical (or paramerized) query is submitted for execution again.  Reusing a cached execution plan can save time because a new execution plan does not have to be recreated each time the same query is re-executed.

How to get an execution plan  SQL Server has multiple ways to get execution plans. The two most important methods are:  Graphical The graphical representation of SQL Server execution plans is easily accessible in the Management Studio but is hard to share. Especially because detailed information for the individual operations is only visible when the mouse is over the particular operation ("hover").  Text The table wise execution plan is hard to read but easy to copy. The table includes all the information in show shot.

Graphical Execution plan

Interpreting Graphical Execution Plans  You read a graphical execution plan from right to left and top to bottom.  Icons (operators) - The icons you see in the above execution plan are 2 of the several operators that represent various actions and decisions that potentially make up an execution plan.  Arrows - The arrow pointing between two operators represent data being passed between them. The thickness of the arrow reflects the amount of data being passed, thicker meaning more rows.  Costs (per operator) - Below each icon is displayed a number as a percentage. This number represents the relative cost to the query for that operator

Tooltips  Each of the icons and the arrows has, associated with it, a pop-up window called a ToolTip, which you can access by hovering your mouse pointer over the icon.  Physical Operation - Lists the physical operation being performed for the node,such as a Clustered Index Scan, Index Seek, Aggregate, Hash or Nested Loop Join,and so on  Logical Operation—Lists the logical operation that corresponds with the physical operation, such as the logical operation of a union being physically performed as a merge join.  Estimated I/O Cost—Indicates the estimated relative I/O cost for the operation. Preferably, this value should be as low as possible.  Estimated CPU Cost—Lists the estimated relative CPU cost for the operation.  Estimated Number of Executions—Lists the estimated number of times this operation will be executed.  Estimated Operator Cost—Indicates the estimated cost to execute the physical operation. For best performance, you want this value as low as possible.

Tooltips (contd.)  Estimated Number of Rows—Lists the estimated number of rows to be output by the operation and passed on to the parent operation.  Estimated Row Size—Indicates the estimated average row size of the rows being passed through the operator.  Estimated Subtree Cost—Lists the estimated cumulative total cost of this operation and all child operations preceding it in the same subtree.  Object—Indicates which database object is being accessed by the operation being performed by the current node.  Predicate—Indicates the search predicate specified for the object in the original query.  Seek Predicates—Indicates the search predicate being used in the seek against the index when an index seek is being performed.  Output List—Indicates which columns of data are being returned by the operation.  Ordered—Indicates whether the rows are being retrieved via an index in sorted order.

Logical and Physical Operators  Each operator implements a single basic operation, such as:  Scanning data from a table  Seeking data in a table  Aggregating data  Sorting  Joining two data sets  Etc.  In total, there are 79 different operator that can be included in an execution plan.

Table Scan  Seeing a table scan often indicates a problem that needs to be addressed.  A table scan indicates that every row in the table had to be examined to see if it met the query criteria, which can mean slow performance if there are a large numbers of rows.  A table scan indicates there is no clustered index on the table, and the table is a heap.  In most cases, you will want to add a clustered index to every table, as it has the potential of boosting the performance of the query.

Clustered Index Scan  A clustered index scan is a scan of all the rows of a table that has a clustered index.  Like a table scan, clustered index scans can be slow and use up lots of server resources.  Generally, clustered index scans should generally be avoided (but better than table scans).  On the other hand, when tables are small or many rows are returned, then a clustered index scan might be the fastest way to return data.

Clustered Index Seek  If there is an available and useful index, and there is a sargeable WHERE clause, the query optimizer can usually, very quickly, identify the rows to be returned and return them without having to scan each row of the table.  Ideally, for best query performance, clustered index seeks should be as used often as feasible. Consider them the “golden standard” for returning data.

Nonclustered Index Scan  All records in the table are scanned, and all rows that match the WHERE clause are returned.  As with all scans, it can be slow and require extra I/O resources.  Generally, non-clustered index scans should be avoided.

Nonclustered Index Seek  A non-clustered index is used to identify the row(s) to be returned, so every row does not need to be scanned (assumes sargeable WHERE clause).  This is generally much faster than a non-clustered index scan.  Like clustered index seeks, non-clustered index seeks are generally a good thing.  One exception is if bookmark (RID or Key) lookups occur as part of the non-clustered index seek, then performance may lag if many rows are returned.

RID Lookup/ Key Lookup  A RID/Key Lookup is generally an indicator of a performance issue.  A RID Lookup is a form of a bookmark index lookup on a table without a clustered index (a heap).  A Key Lookup is a form of a bookmark index lookup on a table with a clustered index.  While Lookups are often faster than most “scans,” this is often not the case if many rows have to be returned.  Generally, RID Lookups should be eliminated with the addition of an appropriate clustered index, and if necessary, a covering or included index.

Joins (Loop, Merge, Hash)  The nested loop join compares each row from one table (the “outer table”) to each row from the other table (the “inner table”), looking for rows that satisfy the join predicate.  The merge join works by simultaneously reading and comparing the two sorted inputs one row at a time. For each step, it compares the next row from each input. If the rows are equal, it outputs a joined row and continues. If the rows are not equal, it discards the lesser of the two inputs and continues.  The hash join algorithm executes in two phases known as the “build” and “probe” phases. During the build phase, it reads all rows from the first input, hashes the rows on the equijoin keys, and creates or builds an in-memory hash table. During the probe phase, it reads all rows from the second input (often called the right or probe input), hashes these rows on the same equijoin keys, and looks or probes for matching rows in the hash table.

Text execution plan

References  us/library/ms aspx us/library/ms aspx 