Transact SQL Performance Tips

Slides:



Advertisements
Similar presentations
T-SQL: Simple Changes That Go a Long Way DAVE ingeniousSQL.com linkedin.com/in/ingenioussql.
Advertisements

Indexes and Views Unit 7.
Stored Procedure Optimization Preventing SP Time Out Delay Deadlocking More DiskReads By: Nix.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
Pinal Dave Mentor | Solid Quality India |
Thinking in Sets and SQL Query Logical Processing.
How to kill SQL Server Performance Håkan Winther.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP SQL SERVER Database Administration.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Session Name Pelin ATICI SQL Premier Field Engineer.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Memory-Optimized Tables Querying at the speed of light.
Just Enough Database Theory for Power Pivot / Power BI
Creating Database Objects
SQL Server Statistics and its relationship with Query Optimizer
Standard/Express edition
Parameter Sniffing in SQL Server Stored Procedures
Tuning Transact-SQL Queries
Query Optimization Techniques
Dynamic SQL Writing Efficient Queries on the Fly
Indexes By Adrienne Watt.
Thank You! #sqlsatdnipro Denis
UFC #1433 In-Memory tables 2014 vs 2016
T-SQL Coding Techniques Are you playing with fire?
T-SQL: Simple Changes That Go a Long Way
T-SQL: Simple Changes That Go a Long Way
Finding more space for your tight environment
Parameter Sniffing in SQL Server Stored Procedures
Reading execution plans successfully
Mission-critical performance with Microsoft SQL Server 2016
Dynamic SQL Writing Efficient Queries on the Fly
Reading Execution Plans Successfully
The Ins and Outs of Indexes
Marcos Freccia Stop everything! Top T-SQL tricks to a developer
The Key to the Database Engine
SQL Server 2017 has more cool features than just running on Linux
Please support our sponsors
Cardinality Estimator 2014/2016
TSQL Coding Techniques
Statistics What are the chances
Query Optimization Techniques
SQL Server Mythconceptions And Mythteries
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Execution Plans Demystified
Statistics: What are they and How do I use them
Hidden gems of SQL Server 2016
Transactions, Locking and Query Optimisation
Entity Framework from a database perspective
The 5 Hidden Performance Gems
Microsoft SQL Server 2014 for Oracle DBAs Module 7
Please thank our sponsors!
Ascending Key Problem in SQL Server Large Tables
Hidden Gems of SQL Server 2014
Query Tuning Fundamentals
Fewer cursors since SQL Server 2012 Came Along
Diving into Query Execution Plans
Are you following SQL Server Development Best Practices?
SQL Server Query Design and Optimization Recommendations
From adaptive to intelligent: query processing in SQL Server 2019
Sourav Mukherjee Are you following SQL Server Development Best Practices? March 30, 2019 Cincinnati.
Query Optimization Techniques
The Five Mistakes You are Probably Making with SQL Server
Creating Database Objects
T-SQL Basics: Coding for performance
Developing Microsoft SQL Server Databases
T-SQL: Simple Changes That Go a Long Way
From adaptive to intelligent:
Presentation transcript:

Transact SQL Performance Tips Miloš Radivojević Milos.Radivojevic@bwinparty.com @MilosSQL

Gold sponsors

Platinum sponsor 12/31/201812/31/2018 | Footer Goes Here

About Me Miloš Radivojević SQL Server MVP Principal Database Consultant, bwin.party Vienna, Austria MCT, MCITP, MCTS, MCP Co-Founder: SQL Pass Austria Conference Speaker: SQLBits, SQL Saturday, SQL Pass Austria Contact: E Milos.Radivojevic@bwinparty.com W www.bwinparty.com @MilosSQL http://milossql.wordpress.com

SQL Server 2016 CTP2 First Impressions PASS Austria and bwin SQL Server Community SPECIAL: SQL Server 2016 Vienna, Thursday 25th June 14-18 Bwin, Marxergasse 1B, 1030 Vienna 13.07.2013 |

SQL Server 2016 CTP2 First Impressions 14:30 First Session by Milos Radivojevic: SQL Server 2016 – What’s New for Developers? Temporal Tables in SQL Server 2016 JSON Support in SQL Server 2016 In-Memory OLTP from Developer Perspective Dynamic Data Masking Query Store and Live Query Statistics 16:00 Break 16:30 Second Session by Rick Kutschera: SQL Server 2016 – What’s New for Database Administrators? Columnstore Indexes in SQL Server 2016 In-Memory OLTP from DBAs Perspective Polybase Row-Level Security and Security Enhancements High Availability Enhancements 18:00 End of the program and get together

Agenda Query Execution Functions in the WHERE Clause Data Type Conversion Local Variables APPLY vs. JOIN Database Constraints and Performance Other Tips

Query Execution Transact SQL is a declarative language SQL Server generates an execution plan (cost-based optimization) What can affect the query execution? Which tables are involved in the query Are there indexes available on the tables Cardinality estimation by reading table statistics How the query is written?

Functions in the WHERE Clause Tip: Avoid functions in the WHERE clause with table columns as arguments! An index will not be used at all or it will be used inefficiently

Data Type Conversion Tip: Choose the right data type! Otherwise: Conversion overhead String Conversion costs can be significant (a “non-Unicode” value will be converted to a “Unicode” value) Estimation problem (for instance with the LIKE Operator)

Local Variables Tip: Understand how local variables affect query execution When local variables are used the SQL Server optimizer cannot generate the optimal execution plan because the variable value is not known at the compile time Tip: Use OPTION (RECOMPILE) to get a good execution plan

Local Variables Tip: Do not underestimate discrepancy in the cardinality estimation! Otherwise you could see something like this:

SQL Server 2012 Tips Tip: Use Window Aggregate Functions! Great SQL Server 2012 Improvements( Running Totals) Tip: Use Window Offset Functions! LEAD, LAG, FIRST_VALUE, LAST_VALUE (Current vs. Previous) Tip: Use Window Functions Whenever You Can! In SQL Server 2005, 2008/R2 too!

Database Constraints Tip: Database Constrains help SQL Server to make a better execution plan. The main purpose of constraints is data integrity, but Unique Constraints, Check Constraints and Foreign Keys help SQL Server optimizer to make the right decision about query execution

Other Tips Tip: SELECT Only Required Columns! Only required information should be requested. It sounds trivial, but there are lot of examples with unnecessary SELECT * or ORDER BY statements Tip: Use ORDER BY only if it was explicitly required! An overhead can be significant when Sort Operator is involved

Other Tips Tip: Use UNION ALL when you know that sets are not overlapped and when duplicates are allowed! Tip: IN vs. EXISTS! perform the same. Use what you are more comfortable with! The same tip for EXISTS vs. COUNT(*)

Thank You!