SQL Server 2000 (& 7.0) Execution Plan Cost Formulas Joe Chang

Slides:



Advertisements
Similar presentations
Chapter 13: Query Processing
Advertisements

Monitoring and Testing I/O
Understanding SQL Server Query Execution Plans
Introduction to SQL Tuning Brown Bag Three essential concepts.
Database Performance Tuning and Query Optimization
Copyright © 2011 by the Commonwealth of Pennsylvania. All Rights Reserved. Load Test Report.
1 Overview Assignment 4: hints Memory management Assignment 3: solution.
Overview of Query Evaluation (contd.) Chapter 12 Ramakrishnan and Gehrke (Sections )
Introduction to Indexes Rui Zhang The University of Melbourne Aug 2006.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Evaluation of Relational Operations Chapter 12, Part A.
Equality Join R X R.A=S.B S : : Relation R M PagesN Pages Relation S Pr records per page Ps records per page.
Query Processing and Optimizing on SSDs Flash Group Qingling Cao
SQL Performance 2011/12 Joe Chang, SolidQ
EXECUTION PLANS By Nimesh Shah, Amit Bhawnani. Outline  What is execution plan  How are execution plans created  How to get an execution plan  Graphical.
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
SQL Server Query Optimizer Cost Formulas Joe Chang
Parallel Execution Plans Joe Chang
SQL Server 2005 Performance Enhancements for Large Queries Joe Chang
Quantitative Performance Analysis Joe Chang
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 14 – Join Processing.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Denny Cherry Manager of Information Systems MVP, MCSA, MCDBA, MCTS, MCITP.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
Insert, Update & Delete Performance Joe Chang
1 Chapter 7 Optimizing the Optimizer. 2 The Oracle Optimizer is… About query optimization Is a sophisticated set of algorithms Choosing the fastest approach.
© Dennis Shasha, Alberto Lerner, Philippe Bonnet 2004 DBMS Performance Monitoring.
Data Partitioning in VLDB Tal Olier
Primary Key, Cluster Key & Identity Loop, Hash & Merge Joins Joe Chang
Parallel Execution Plans Joe Chang
Large Data Operations Joe Chang
Parallel Execution Plans Joe Chang
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
TPC-H Studies Joe Chang
Query Optimizer Execution Plan Cost Model Joe Chang
1 Chapter 13 Parallel SQL. 2 Understanding Parallel SQL Enables a SQL statement to be: – Split into multiple threads – Each thread processed simultaneously.
Infrastructure for Data Warehouses. Basics Of Data Access Data Store Machine Memory Buffer Memory Cache Data Store Buffer Bus Structure.
CPSC 404, Laks V.S. Lakshmanan1 Evaluation of Relational Operations – Join Chapter 14 Ramakrishnan and Gehrke (Section 14.4)
Computing & Information Sciences Kansas State University Monday, 03 Nov 2008CIS 560: Database System Concepts Lecture 27 of 42 Monday, 03 November 2008.
1 Chapter 9 Tuning Table Access. 2 Overview Improve performance of access to single table Explain access methods – Full Table Scan – Index – Partition-level.
Computing & Information Sciences Kansas State University Wednesday, 08 Nov 2006CIS 560: Database System Concepts Lecture 32 of 42 Monday, 06 November 2006.
Eugene Meidinger Execution Plans
How to kill SQL Server Performance Håkan Winther.
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.
SAP Tuning 실무 SK㈜ ERP TFT.
Indexing strategies and good physical designs for performance tuning Kenneth Ureña /SpanishPASSVC.
SQL Server Performance Tuning Starter Kit Randolph West | Born SQL.
CS 540 Database Management Systems
Query Tuning without Production Data
Scaling SQL with different approaches
Database Applications (15-415) DBMS Internals- Part VII Lecture 16, October 25, 2016 Mohammad Hammoud.
Query Tuning without Production Data
Introduction to Execution Plans
The Key to the Database Engine
Physical Join Operators
Query Optimization CS 157B Ch. 14 Mien Siao.
SQL Server Query Optimizer Cost Formulas
Four Rules For Columnstore Query Performance
Introduction to Execution Plans
CS222P: Principles of Data Management Notes #13 Set operations, Aggregation, Query Plans Instructor: Chen Li.
EXECUTION PLANS Quick Dive.
Execution plans Eugene
Diving into Query Execution Plans
Introduction to Execution Plans
Introduction to Execution Plans
Welcome!.
All about Indexes Gail Shaw.
Presentation transcript:

SQL Server 2000 (& 7.0) Execution Plan Cost Formulas Joe Chang

Database Performance Code Tables, Indexes, Constraints, SQL statements Data Total rows, Rows involved, Statistics Execution Plan Formulas used by the CBO Server Architecture Processor, SMP, etc

Topics Component Operation Cost Model Cost formulas for basic operations Dependencies Rows & Pages involved - yes Index depth – no, Locks level – no WHERE conditions – no cost for logic only if row count affected

Index Seek - 1 row SELECT xx FROM N1C WHERE ID I/OCPUTotal 1GB > 1GB

Index Seek Cost Formula Multiple rows, 1GB I/O: per additional page CPU: per additional row Total: /add. Page / add. row

Plan Cost Unit of Measure Time? CPU-usage? time, in seconds sec -> 160/sec >1350/sec (8KB) ->169/sec(64K)-> 10.8MB/sec Too fast for 7200RPM disk random I/Os. About right for 1997 sequential disk transfer rate? S2K BOL: Administering SQL Server, Managing Servers, Setting Configuration Options: cost threshold for parallelism Option Query cost refers to the estimated elapsed time, in seconds, required to execute a query on a specific hardware configuration. S2K5 BOL: Administering SQL Server, Managing Servers, Setting Configuration Options: cost threshold for parallelism Option The cost refers to an estimated elapsed time in seconds required to run the serial plan on a specific hardware configuration.

Disk Drive Performance Access time = rotational latency + seek time 7200RPM = 4.17ms Rotational Latency 10000RPM = 3ms, 15000RPM = 2ms Rot.Avg.Sequential YearModelRPMSeek Transfer 1994ST K MB/sec 1996ST K ST K ST K * 1999ST K * 2000 ST K * 2002ST K

Bookmark Lookup – Single Row Same cost for bookmark lookup on Heap and Clustered Index SQL Server 2000 up to SP3 (build 760) SQL Server 2000 after MS (build 818)

Detail up to SP3 (build 760) after MS (build 818)

BL Plan Cost – Single Row I/O CPU 1GB > 1GB I/O CPU Total 1GB > 1GB up to SP3 (build 760) after MS (build 818)

Bookmark Lookup Multiple Rows A lower I/O cost per page ( ) for up to 64 rows, original I/O cost applies at 65 rows and above For table size up to approx. 100 pages, I/O cost is bounded by the number of pages, representing an estimate of # of I/O required, at cost / page For larger tables, I/O cost is not bounded by the number of pages I/O Cost: up to SP3 (build 760) after MS (build 818) CPU cost per applies in all cases

Bookmark Lookup I/O Cost – SP3 By Table Size (pages) versus rows, steps

Bookmark Lookup I/O Cost By Table Size (pages) versus rows, steps

Bookmark Lookup I/O Cost – SP3 Plan Cost by Table Size (pages) versus rows, SP3 and earlier

Bookmark Lookup I/O Cost Plan Cost by Table Size (pages) versus rows, build 818+

Bookmark Lookup IO Count For a bookmark lookup for n rows to a table with p pages 1 st row: 1 I/O required 2 nd row: 1 I/O already done, 1-1/p chance additional I/O F(n,p) = F(n-1,p) + 1 – F(n-1,p)/p = F(n-1,p)(1-1/p) + 1 R = 1-1/p F(n,p) = 1 + R + R 2 + … + R n-1 = (1-R n )/(1-R) = p – p (1-1/p) n

Table Scan SELECT xx FROM N1H WHERE ID I/O: /page CPU: /row

Bookmark versus Scan Table scan cost for 50,000 row, 506 pages Index Seek and Bookmark Lookup cost for 1GB, SP3

Aggregates SELECT MIN(x) FROM M2C WHERE GroupID=1 I/O: NoneCPU: /row Aggregate: MIN & MAX Aggregate & Compute Scalar AVG & SUM For single row result

Loop, Hash and Merge Joins SQL Server supports 3 types of joins Loop, Hash, Merge Hash join subtypes In memory, Grace, Recursive Different settings for SQL Batch & RPC Merge join one-to-many many-to-many

Loop Join Outer Source (Top Input) Inner Source (Bottom Input) Join

Loop Join Cost Loop Join Cost = Outer Source Cost + Inner Source Cost + Join Cost

Loop Join, cont I/O Cost: 0 CPU Cost per row

Loop Join, Inner Source I/O and CPU cost is for 1 execute Cost is for all executes Number of executes is row count from outer source row count is expected matches per row for each row from outer source (rounded down)

Loop Join IS Cost Assumptions: CPU Cost: per row applies for 1:1 join IO Cost: Single row base cost same as Index Seek Multiple rows: estimate of number pages required For small IS table (<100 pages) Bounded by per page For large IS table (>200 pages) Bounded by per page for up to 132 rows Approx (1GB) per row for 133 rows and above

Loop Join IS I/O Costs (1) Inner Source I/O Cost by table size versus rows Excluding base I/O cost

Loop Join IS I/O Costs (2) Inner Source I/O Cost by table size versus rows Excluding base I/O cost

Loop Join Costs Small & Big IS Source table size

Hash Join Hash Join Cost = Outer Source + Inner Source + Hash Match SELECT * FROM M2C m INNER HASH JOIN M2D n ON n.ID = m.ID WHERE m.GroupID AND n.GroupID

Hash Join Cost Outer Source & Inner Source are index seeks or scans 1 execute, 1 or more rows

Hash Join - IS Hash join cost independent of IS column count or size

Hash Join - OS Hash join cost dependent on OS size Q1 Q2 Q3

Hash Join Cost Q1 Q2 Q3

Hash Join Cost Formula Base CPU Cost = base Fudge factors (2-30 rows) (100 rows) Cost per row byte, 1 st 100 (1:1 join) >1K (parallel) per row per 4 bytes in OS OS / IS I/O Cost = per row over >64-102MB? per row per 4 byte Hash join spills to tempdb at MB in 32-bit 1-2GB memory 700MB+ in 64-bit with 32GB memory

Merge Join Merge Join Cost = Outer Source + Inner Source + Merge cost SELECT xx FROM M2C m INNER MERGE JOIN M2D n ON n.ID = m.ID WHERE m.GroupID AND n.GroupID

Merge Join Cost Cost CPU: /row Discrepancy: /row IS /row OS

Merge + Sort

Many-to-Many Merge I/O: per row CPU: per row

Loop, Hash & Merge Join

Loop, Hash & Merge Join (2)

Loop, Hash & Merge Join (3)

1 to Many Joins Each row from OS joins to n rows in IS Join Cost per additional IS row Loop Hash~ Merge ~ IS Index Seek cost: /row + IO costs

Sort Cost I/O: CPU: *(rows-1)^1.26 weak dependency on size per row

Index Intersection SELECT xx FROM M2C WHERE GroupID AND CodeID SELECT xx FROM M2C a INNER JOIN M2C b ON b.ID = a.ID WHERE a.GroupID AND b.CodeID Table M2x, Index on GroupID Index on CodeID Merge Join cost formula different than previously discussed

Execution Plan Costs Recap Index Seek I/OCPUTotal 1GB > 1GB Additional page /p Additional rows /r Bookmark LookupI/OCPUTotal 1GB > 1GB Table ScanI/OCPUTotal Base Additional page /p Additional row /r

Logical IO count Example: Index Depth 2, rows per page: 100 I/O per additional row Bookmark Lookup (Heap)1 Bookmark Lookup (Clustered)2 Loop Join (IS)2 I/O per addition 100 rows Index Seek1 Hash & Merge join2 Very little relation between IO count and plan cost for different component operations IO count comparisons more relevant for similar operations

Accurate Performance Testing Execution Plan - match Raw size of DB not as important: 1M customers actual, 10K test Cardinality more important 1 Customer – 10 orders – 10 order items per order Statistics & actual data queried Statistics could be accurate but actual queries favors different distribution

Aggregates, multiple result rows CPU Cost: per source row per result row CPU Cost per result row:

Execution Plan Cost Summary Plan costs do not include RPC cost Plan costs are a model Index seek independent of index depth Bookmark L/U independent of table organization Logic by itself does not influence cost Costs are not influenced by lock hints Populate test DB with accurate cardinality

Additional Information SQL Server Quantitative Performance Analysis Server System Architecture Processor Performance Direct Connect Gigabit Networking Parallel Execution Plans Large Data Operations Transferring Statistics SQL Server Backup Performance with Imceda LiteSpeed