Score a (row) goal and beat a query optimizer

Slides:



Advertisements
Similar presentations
Query Optimization Reserves Sailors sid=sid bid=100 rating > 5 sname (Simple Nested Loops) Imperative query execution plan: SELECT S.sname FROM Reserves.
Advertisements

Dos and don’ts of Columnstore indexes The basis of xVelocity in-memory technology What’s it all about The compression methods (RLE / Dictionary encoding)
David Konopnicki Choosing Access Path ä The basic methods. ä The access paths and when they are available. ä How the optimizer chooses among the.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 11 Database Performance Tuning and Query Optimization.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
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.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Pinal Dave Mentor | Solid Quality India |
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
Thinking in Sets and SQL Query Logical Processing.
Eugene Meidinger Execution Plans
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Execution Plans Detail From Zero to Hero İsmail Adar.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Session Name Pelin ATICI SQL Premier Field Engineer.
Tuning Oracle SQL The Basics of Efficient SQL Common Sense Indexing
CHAPTER 19 Query Optimization. CHAPTER 19 Query Optimization.
SQL Server Statistics and its relationship with Query Optimizer
Parameter Sniffing in SQL Server Stored Procedures
Query Optimization Techniques
Thank You! #sqlsatdnipro Denis
Query Tuning without Production Data
T-SQL: Simple Changes That Go a Long Way
Parameter Sniffing in SQL Server Stored Procedures
Query Tuning without Production Data
Query Tuning without Production Data
Choosing Access Path The basic methods.
Reading execution plans successfully
Reading Execution Plans Successfully
Overview of Query Optimization
Introduction to Execution Plans
Query Execution Expectation-Reality Denis Reznik
Decoding the Cardinality Estimator to Speed Up Queries
Marcos Freccia Stop everything! Top T-SQL tricks to a developer
Now where does THAT estimate come from?
Cardinality Estimator 2014/2016
Physical Join Operators
T-SQL Power! The OVER Clause: Your Key to No-Sweat Problem Solving
Statistics What are the chances
Query Optimization Techniques
Execution Plans Demystified
Statistics: What are they and How do I use them
Hidden gems of SQL Server 2016
Reading Execution Plans Successfully
Hugo Kornelis Now where does THAT estimate come from? The nuts and bolts of cardinality estimation.
SQL Server Query Plans Journeyman and Beyond
Transact SQL Performance Tips
Ascending Key Problem in SQL Server Large Tables
Hidden Gems of SQL Server 2016
Parameter Sniffing: the Good, the Bad, and the Ugly
Introduction to Execution Plans
Parameter Sniffing: the Good,the Bad, and the Ugly
Parameter Sniffing: the Good, the Bad, and the Ugly
Section 4 - Sorting/Functions
Power Query & Database Tuning
Execution plans Eugene
Diving into Query Execution Plans
Query Optimization.
SQL Server Query Design and Optimization Recommendations
Introduction to Execution Plans
SQL Performance for DBAs
Reading execution plans successfully
Introduction to Execution Plans
Azure SQL Database Lessons Learned From the Trenches Best practices, Tips and Tricks on monitoring and improving performance. Fernando Cobo.
All about Indexes Gail Shaw.
T-SQL: Simple Changes That Go a Long Way
XML? What’s this doing in my database? Adam Koehler
Presentation transcript:

Score a (row) goal and beat a query optimizer Pavel Málek Score a (row) goal and beat a query optimizer

Sponsors

Bio 10+ years of experience with SQL Server Czech PASS leader SQL Saturday Prague co-organizer Lead SQL Server DBA in largest Czech bank for 6+ years Currently newbie at Joyful Craftsmen @malekpav LinkedIn malekpav@gmail.com

Before we begin…

Challenge for you! Try to write a query which will show you 10 last trades (trd.TradesArch) for each market trader (trd.Parties)? It will be used on primary site for traders => it has to be faaaast!

Solution

Nature of SQL SQL is a declarative language!

But how much “declarative”? Cross apply RowNb But how much “declarative”? For every subject trading on market find the most recent related trade Go through all the trades and show only those who are the most recent joined with trading subjects In other words CROSS APPLY = nested loop The exception to this rule is if the APPLY is logically equivalent to a JOIN RowNb = always scan

Query Optimizer is in charge, but… Sometimes QO simply cannot have correct inputs OR logic in the WHERE clause Aggregations in intermediate results sets A large number of very complex joins A CASE clause in the WHERE or JOIN clause Other most common issues are MultiStatement TVF Table Variable TVP Parameter sniffing

Query optimizer having troubles SELECT TOP (80) * FROM trd.TradesView ORDER BY [Id] DESC, [Version] DESC SELECT TOP (80) * FROM trd.TradesArchView ORDER BY [Id] DESC, [Version] DESC

Demo

Histogram aligning Nicolas’s Bruno (Software Developer, Microsoft) work “Statistics on Query Expressions in Relational Database Management Systems” COLUMBIA UNIVERSITY, 2003.  “Mere mortals” reading: Hugo Kornelis – SQL Sat presentation Dimo – queryprocessor.com

Other possibilities?

Demo Takeaways FKs are good Indexes on FKs is almost always good But don’t expect them to be “always” used for row estimation! Updates of stats via full scan matters!!! TF 2301 sometimes helps Carefully with TF 2371

Demo case continues What if I don’t want to use any traceflags TOP operator introduced an interesting behavior What the hell “a row goal” is?

Case Trading System#2: Key Takeaways Via TOP + ORDER BY you are able to create your own “blocks” Via TOP (@x) OPTIMIZE FOR you are able to decrease estimated nb of rows Via MANY() you are able to enlarge estimated nb of rows CREDITS: Adam Machanic

Another example of fixed QO behavior

Summary How much statistics matter How joins are calculated Now you know: How much statistics matter How joins are calculated How to force QO to do what you want to (TOP blocks, MANY(), TOP (@x) + OPTIMIZE FOR ) CROSS APPLY vs ROWNB() What are the possibilities for MAX() and MIN()

Thank you!

Evaluations Event Evaluation Session Evaluation http://www.sqlsaturday.com/764/EventEval.aspx Session Evaluation http://www.sqlsaturday.com/764/Sessions/SessionEvaluation.aspx