Download presentation
Presentation is loading. Please wait.
Published byJoanna Joseph Modified over 9 years ago
1
SQL Server Query Optimizer Cost Formulas Joe Chang jchang6@yahoo.comjchang6@yahoo.com www.qdpma.comwww.qdpma.com, www.solidq.comwww.solidq.com
2
Scope - Query Optimizer Parse SQL Execution Plans Cost Model Rows and pages in each operation Data Distribution Statistics Estimate rows and pages Sources – David Dewitt, Conor Cunningham
3
Query Optimizer References Conor Cunningham Chapter in Inside SQL Server Conor vs. SQL http://blogs.msdn.com/b/conor_cunningham_msft/ David Dewitt PASS 2010 Summit Keynote Search: Microsoft Jim Gray Systems Lab http://www.slideshare.net/GraySystemsLab/pass-summit-2010-keynote-david-dewitt http://www.slideshare.net/GraySystemsLab/pass-summit-2010-keynote-david- dewitt/download
4
My material http://www.qdpma.com/CBO/SQLServerCo stBasedOptimizer.html http://www.qdpma.com/zPresentations.ht ml
5
Paul White – Page Free Space http://sqlblog.com/blogs/paul_white/default.aspx Inside the Optimiser: Constructing a Plan – Part 4 http://sqlblog.com/blogs/paul_white/archive/2010/07/31/insid e-the-optimiser-constructing-a-plan-part-4.aspx DBCC RULEON/RULEOFF Inside the Optimizer: Plan Costing http://sqlblog.com/blogs/paul_white/archive/2010/0 9/01/inside-the-optimizer-plan-costing.aspx DBCC TRACEON (3604); -- Show DBCC output DBCC SETCPUWEIGHT(1E0); -- Default CPU weight DBCC SETIOWEIGHT(0.6E0); -- I/O multiplier = 0.6 DBCC SHOWWEIGHTS; -- Show the settings
6
Execution Plan Cost Model Index Seek + Key Lookup – Table Scan Joins – Loop, Hash, Merge Updates (Includes Insert & Delete) Really complicated, not covered here Parallel Execution Plans
7
Why this is Useful? When does the QO use: an index versus table scan Loop Join or Hash/Merge with Scan? Is there a difference between the Cost Model and True Cost Structure? Should I use query hints Parallel Execution Strategy Modern servers – 64+ cores
8
SQL Server Books Online Query Governor Cost Limit Cost Threshold for Parallelism The cost refers to an estimated elapsed time in seconds required to run the serial plan on a specific hardware configuration. … SQL Server creates and runs a parallel plan for a query only when the estimated cost to run a serial plan for the same query is higher than the value set in cost threshold for parallelism. Query cost refers to the estimated elapsed time, in seconds, required to complete a query on a specific hardware configuration.
9
Adventure Works Example
11
Estimated Execution Plan
12
Clustered Index Scan
13
Index Seek
14
Index Seek + Key Lookup
15
Heap Table
16
Heap Operations
17
The Formula – Seek, Scan (Clustered) Index Scan, Table Scan, Index Seek IO Cost0.003125 + 0.00074074 per page CPU Cost0.0001581 + 0.0000011 per row
18
Key Lookup (& Loop Join) Key/RID Lookup, Nested Loops Join IO Cost0.003125 x % that require Lookup CPU Cost0.0001581 per Lookup 0.0000011 per additional rows
19
IO Cost Model Sequential - Random Cost is elapsed time in seconds Random 0.003125 = 1/320 Sequential 0.00074074… = 1/1350 Random: 320 IOPS Sequential 1350 pages/sec, or 10.8MB/s
20
Key Lookup – Scan Cross over Key Lookup rows to pages scanned ratio 1 Key Lookup cost approximately 4 pages in scan operation Non-parallel plan, with other costs Cross-over approx 3.5 pages per KL row Parallel Plan Closer to 4 pages per Key Lookup row
22
Loop, Hash and Merge Joins
23
L H M
24
Sort
25
Loop Hash and Merge CostFixed Incremental Loop~0.00.00000418 + Seek + seek cost: 0.003125 IO, 0.0001581 CPU Hash~0.0177800.00001526* Merge~0.0056070.00000238 † Many-to-Many Merge0.00004738 Sort~0.011261 * Hash incremental cost depends on inner/outer source size † Merge join incremental is per IS & OS row? Merge + Sort fixed cost approx same as Hash fixed cost
26
Loop, Hash, Merge CostFixed Incremental LoopZeroHigh HashHighMedium MergeMediumLow Merge Join requires both source rows in index sorted order. Regular Merge only for 1-1 or 1-many Many-to-many merge join is more expensive
30
Plan Cross-over Theory Cost Rows Index Seek + Key Lookup Table Scan
31
Theory & Actual? Cost Rows KL Theory Table Scan KL alternate reality? KL Actual!
32
Plan and Actual IO RandomSequentialRatio Plan320 1,350 (10.8M/s)4.2187 Current HD200* 12,800 (100MB/s)64* SAN200 1,280 (10MB/s)6.4 SSD 20,000 25,000~1 *Note: original slide incorrectly listed 640:1
34
Loop, Hash & Merge
36
Loop Join
37
Merge Join
38
Hash Join
39
Insert, Update & Delete Really complicated See material from Conor For large number of rows (25%?) Consider dropping indexes
40
Delete Rows Index foreign keys when: Deletes from primary table are frequent
42
Parallel Execution Plans Parallel Execution Parallelism Gather, Repartition, Distribute Streams, Partitions
43
Parallel Execution Plan
44
Parallel Operations Distribute Streams Non-parallel source, parallel destination Repartition Streams Parallel source and destination Gather Streams Destination is non-parallel Bitmap
45
Scan
46
DOP 1 DOP 2 DOP 4 DOP 8 IO Cost same CPU reduce by degree of parallelism, except no reduction for DOP 16 2X 4X 8X IO contributes most of cost!
47
DOP 16 DOP 8
48
IO Cost is the same CPU cost reduced in proportion to degree of parallelism, last 2X excluded? On a weak storage system, a single thread can saturate the IO channel, Additional threads will not increase IO (reduce IO duration). A very powerful storage system can provide IO proportional to the number of threads. It might be nice if this was optimizer option? The IO component can be a very large portion of the overall plan cost Not reducing IO cost in parallel plan may inhibit generating favorable plan, i.e., not sufficient to offset the contribution from the Parallelism operations. A parallel execution plan is more likely on larger systems (-P to fake it?)
49
Partitioned Tables Regular Table Partitioned Tables No Repartition Streams operations!
51
Parallel Execution: Super Scaling Suppose at DOP 1, a query runs for 100 seconds, with one CPU fully pegged CPU time = 100 sec, elapse time = 100 sec What is best case for DOP 2? Assuming nearly zero Repartition Threads cost CPU time = 100 sec, elapsed time = 50? Super Scaling: CPU time decreases going from Non-Parallel to Parallel plan! No, I have not been drinking, today, yet
52
Super Scaling CPU-sec goes down from DOP 1 to 2 and higher (typically 8) CPU normalized to DOP 1 Speed up relative to DOP 1 3.5X speedup from DOP 1 to 2 (Normalized to DOP 1)
53
Most probable cause Bitmap Operator in Parallel Plan Bitmap Filters are great, Question for Microsoft: Can I use Bitmap Filters in OLTP systems with non-parallel plans?
54
Negative Scaling Query time “Speedup”
55
CPU
56
Small Queries – Plan Cost vs. Act Query 3 and 16 have lower plan cost than Q17, but not included Q4,6,17 great scaling to DOP 4, then weak Negative scaling also occurs Query time Plan Cost
57
What did I get for all that extra CPU?, Interpretation: sharp jump in CPU means poor scaling, disproportionate means negative scaling Query 2 negative at DOP 2, Q4 is good, Q6 get speedup, but at CPU premium, Q17 and 20 negative after DOP 8 CPU time Speed up
58
Parallel Exec – Small Queries Why? Almost No value OLTP with 32, 64+ cores Parallelism good if super-scaling Default max degree of parallelism 0 Seriously bad news, especially for small Q Increase cost threshold for parallelism?
59
Parallel Settings - Strategy Mostly for OLTP Cost Threshold for Parallelism Default: Plan Cost > 5: Proposed: 20-100 In 1997, Pentium Pro 200MHz, ~5 sec for 50MB table (index range) scan Today, Xeon 5680, 3.3GHz, ~ 30X faster Parallel plan could run 10-100 milli-sec
60
Parallel Settings - Strategy Mostly for OLTP Cost Threshold for Parallelism Default: Plan Cost > 5: Proposed: 20-100 In 1997, Pentium Pro 200MHz, ~5 sec for 50MB table (index range) scan Today, Xeon 5680, 3.3GHz, ~ 30X faster Parallel plan could run 10-100 milli-sec
61
Parallel Settings - Strategy Cost Threshold for Parallelism Default: Plan Cost > 5: Proposed: 20-100 In 1997, Pentium Pro 200MHz, ~5 sec for 50MB table (index range) scan Today, Xeon 5680, 3.3GHz, ~ 30X faster Parallel plan could run 10-100 milli-sec Max Degree of Parallelism (OLTP) Default: 0, unrestricted, Proposed: 2-4 Use OPTION (MAXDOP n)
63
Too Many Indexes Complicates Query Optimization Too many possible execution plan Large Updates – Maintenance Consider dropping indexes
64
Parameters and Variables Unknown, remote source Remote Scan: 10,000 rows Remote Seek xxx rows Unknown >, <, BETWEEN > or <: 30% of rows BETWEEN: 1/10 of rows
65
Temp tables and Table Variables
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.