SlideShare a Scribd company logo
uy.vopen.tech
Query Tuning Techniques Every
SQL Server Programmer Should
Know
Rodrigo Crespi
Rodrigo Crespi
MCSE, MCSA, etc.
PostDegree in Data Base – UGF
PASS Chapter Leader – SQL Server RS
University Professor of Database Management
CrespiDB | Soluções em Plataforma de Dados
@SQLCrespi
rodrigocrespi
S Q L C r e s p i
sqlcrespi.com
SQLCrespi
First of All!
● Understand how a query works;
● Understand set theory;
● Most important try another thought.
SQL Server Architecture
P
r
o
t
o
c
o
l
o
s
Parser
Optimizer
SQL Mgr.
DB. Mgr.
Query
Executor
R
e
l
a
t
i
o
n
a
l
E
n
g
i
n
e
S
t
o
r
a
g
e
E
n
g
i
n
e
T-Services
Lock Mgr.
File Mgr.
Utilities
Access
Methods
SQLOS
SQLOS.dll
SQLLang.dll
PageLock Monitor
Resource Monitor
Lazy Writer
Scheduler Monitor
SQLmin.dll
Buffer Pool
Memory Mgr.
Scheduling
Sinc. Services
Lock Mgr.
I/O
SQLDK.dll
It’s a simple
representation
Relational Engine
Optimizer
Query
Executor
Cmd Parser
SNI
Protocol Layer
Storage Engine
Transactio
n Manager
Access
Methods
Buffer
Manager
Lock
Manager
Data Cache
Plan Cache
Data File
T-SQL
Select * from cliente
T
D
S
IF
Query Tree
O
L
E
D
B
IF
Buffer Pool
Query
Workspace
Query
Compile
Token Perm
XEvents,Metadados,
outros...
Relational Engine
Optimizer
Query
Executor
Cmd Parser
SNI
Protocol Layer
Storage Engine
Transactio
n Manager
Access
Methods
Buffer
Manager
Data File
T-SQL
Update cliente set nome = ‘Rodrigo’
T
D
S
IF
Query Tree
O
L
E
D
B
IF
Lazy Writer
CheckPoint
Lock Manager
Data Cache
Plan Cache
Buffer Pool
Query
Workspace
Query
Compile
Token Perm
Xevents,Metadados,
outros...
Dirty
Page
Physical Query Tree
• It is the way the optimizer calculates the most efficient path for executing the
command;
• More efficient or more optimized?
Zoom of Optimizer
Parsing / binding
Simplification
Start a joins set
Trivial Plan
Optimization phase
Search 0
Search 1
Search 2
Phase 0-Traditional: nested
Loops, with no parallelism
options.
Phase 1 – Fast: Uses a fast
subset of rules that
contains the most common
patterns.
Phase 2 – Complete: For
complex queries with
parallelism and indexed
views.
Before Optimization
Optimization
Tips
The best
pattern is the
one that every
team follows!
Pattern
Formatting T-SQL for Readability
● Comments, author, and brief audit trail;
● Be Consistent;
● The best formatting pattern is the one that is followed by the entire team.
How To Test a Query
● Clear caches;
● Try, Test and Compare;
● Set measurements;
● DMVs for check;
● Clone Database.
T-SQL Performance
• Set Statistics IO / Time;
• Compare the execution plans;
Predicates and Set Theory
● Say no to cursor;
● The Optimizer can’t pick the best execution plan if the
Stored Procedure is broken up into many parts;
● Built-In functions that can affect performance;
● Like;
● And | Or | In;
● Union and union all;
● IsNull();
Implicit Conversions
● The optimizer chooses the data type based on a predefined list;
○ https://docs.microsoft.com/en-us/sql/t-sql/data-types/data-
type-precedence-transact-
sql?redirectedfrom=MSDN&view=sql-server-ver15
● Data Types matter;
● You may not get what you expect;
● Implicit conversions can be costly.
Lock and NoLock
Lock is required in SQL
Databases. This is a true!
HOWEVER, if you use
hint nolock pay attention
if you reading a dirty page!
3 query tuning techniques every sql server programmer should know
SPONSORS
¡Gracias!
@SQLCrespi
Ad

Recommended

Islamabad PUG - 7th meetup - performance tuning
Islamabad PUG - 7th meetup - performance tuning
Umair Shahid
 
Growing up new PostgreSQL developers (pgcon.org 2018)
Growing up new PostgreSQL developers (pgcon.org 2018)
Aleksander Alekseev
 
JChem Microservices
JChem Microservices
ChemAxon
 
Spark Structured Streaming
Spark Structured Streaming
Knoldus Inc.
 
Moving advanced analytics to your sql server databases
Moving advanced analytics to your sql server databases
Enrico van de Laar
 
Islamabad PUG - 7th Meetup - performance tuning
Islamabad PUG - 7th Meetup - performance tuning
Umair Shahid
 
kranonit S06E01 Игорь Цинько: High load
kranonit S06E01 Игорь Цинько: High load
Krivoy Rog IT Community
 
Oracle Sql Tuning
Oracle Sql Tuning
Chris Adkin
 
Introduction to Web Development with Ruby on Rails
Introduction to Web Development with Ruby on Rails
pmatsinopoulos
 
Key to a successful Exadata POC
Key to a successful Exadata POC
Umair Mansoob
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
Mydbops
 
OutSystems Tips and Tricks
OutSystems Tips and Tricks
OutSystems
 
Evolution of DBA in the Cloud Era
Evolution of DBA in the Cloud Era
Mydbops
 
Cqrs and Event Sourcing Intro For Developers
Cqrs and Event Sourcing Intro For Developers
wojtek_s
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Aaron Shilo
 
Database Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance Analysis
DAGEOP LTD
 
Before you optimize: Understanding Execution Plans
Before you optimize: Understanding Execution Plans
Timothy Corey
 
Improving The Quality of Existing Software
Improving The Quality of Existing Software
Steven Smith
 
Migrating Oracle database to PostgreSQL
Migrating Oracle database to PostgreSQL
Umair Mansoob
 
Optimising Queries - Series 1 Query Optimiser Architecture
Optimising Queries - Series 1 Query Optimiser Architecture
DAGEOP LTD
 
31063115_1679409488310Developer_Tuning_Tips_-_UTOUG_Mar_2023.pdf
31063115_1679409488310Developer_Tuning_Tips_-_UTOUG_Mar_2023.pdf
TricantinoLopezPerez
 
MongoDB Online Training.pdf
MongoDB Online Training.pdf
SpiritsoftsTraining
 
Plpgsql russia-pgconf
Plpgsql russia-pgconf
Pavel Stěhule
 
The Plan Cache Whisperer - Performance Tuning SQL Server
The Plan Cache Whisperer - Performance Tuning SQL Server
Jason Strate
 
שבוע אורקל 2016
שבוע אורקל 2016
Aaron Shilo
 
PostgreSQL for Oracle Developers and DBA's
PostgreSQL for Oracle Developers and DBA's
Gerger
 
Selena Deckelmann - Sane Schema Management with Alembic and SQLAlchemy @ Pos...
Selena Deckelmann - Sane Schema Management with Alembic and SQLAlchemy @ Pos...
PostgresOpen
 
The Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance Tuning
jClarity
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 

More Related Content

Similar to 3 query tuning techniques every sql server programmer should know (20)

Introduction to Web Development with Ruby on Rails
Introduction to Web Development with Ruby on Rails
pmatsinopoulos
 
Key to a successful Exadata POC
Key to a successful Exadata POC
Umair Mansoob
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
Mydbops
 
OutSystems Tips and Tricks
OutSystems Tips and Tricks
OutSystems
 
Evolution of DBA in the Cloud Era
Evolution of DBA in the Cloud Era
Mydbops
 
Cqrs and Event Sourcing Intro For Developers
Cqrs and Event Sourcing Intro For Developers
wojtek_s
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Aaron Shilo
 
Database Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance Analysis
DAGEOP LTD
 
Before you optimize: Understanding Execution Plans
Before you optimize: Understanding Execution Plans
Timothy Corey
 
Improving The Quality of Existing Software
Improving The Quality of Existing Software
Steven Smith
 
Migrating Oracle database to PostgreSQL
Migrating Oracle database to PostgreSQL
Umair Mansoob
 
Optimising Queries - Series 1 Query Optimiser Architecture
Optimising Queries - Series 1 Query Optimiser Architecture
DAGEOP LTD
 
31063115_1679409488310Developer_Tuning_Tips_-_UTOUG_Mar_2023.pdf
31063115_1679409488310Developer_Tuning_Tips_-_UTOUG_Mar_2023.pdf
TricantinoLopezPerez
 
MongoDB Online Training.pdf
MongoDB Online Training.pdf
SpiritsoftsTraining
 
Plpgsql russia-pgconf
Plpgsql russia-pgconf
Pavel Stěhule
 
The Plan Cache Whisperer - Performance Tuning SQL Server
The Plan Cache Whisperer - Performance Tuning SQL Server
Jason Strate
 
שבוע אורקל 2016
שבוע אורקל 2016
Aaron Shilo
 
PostgreSQL for Oracle Developers and DBA's
PostgreSQL for Oracle Developers and DBA's
Gerger
 
Selena Deckelmann - Sane Schema Management with Alembic and SQLAlchemy @ Pos...
Selena Deckelmann - Sane Schema Management with Alembic and SQLAlchemy @ Pos...
PostgresOpen
 
The Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance Tuning
jClarity
 
Introduction to Web Development with Ruby on Rails
Introduction to Web Development with Ruby on Rails
pmatsinopoulos
 
Key to a successful Exadata POC
Key to a successful Exadata POC
Umair Mansoob
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
Mydbops
 
OutSystems Tips and Tricks
OutSystems Tips and Tricks
OutSystems
 
Evolution of DBA in the Cloud Era
Evolution of DBA in the Cloud Era
Mydbops
 
Cqrs and Event Sourcing Intro For Developers
Cqrs and Event Sourcing Intro For Developers
wojtek_s
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Aaron Shilo
 
Database Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance Analysis
DAGEOP LTD
 
Before you optimize: Understanding Execution Plans
Before you optimize: Understanding Execution Plans
Timothy Corey
 
Improving The Quality of Existing Software
Improving The Quality of Existing Software
Steven Smith
 
Migrating Oracle database to PostgreSQL
Migrating Oracle database to PostgreSQL
Umair Mansoob
 
Optimising Queries - Series 1 Query Optimiser Architecture
Optimising Queries - Series 1 Query Optimiser Architecture
DAGEOP LTD
 
31063115_1679409488310Developer_Tuning_Tips_-_UTOUG_Mar_2023.pdf
31063115_1679409488310Developer_Tuning_Tips_-_UTOUG_Mar_2023.pdf
TricantinoLopezPerez
 
The Plan Cache Whisperer - Performance Tuning SQL Server
The Plan Cache Whisperer - Performance Tuning SQL Server
Jason Strate
 
שבוע אורקל 2016
שבוע אורקל 2016
Aaron Shilo
 
PostgreSQL for Oracle Developers and DBA's
PostgreSQL for Oracle Developers and DBA's
Gerger
 
Selena Deckelmann - Sane Schema Management with Alembic and SQLAlchemy @ Pos...
Selena Deckelmann - Sane Schema Management with Alembic and SQLAlchemy @ Pos...
PostgresOpen
 
The Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance Tuning
jClarity
 

Recently uploaded (20)

Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
janeliewang985
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
janeliewang985
 
Ad

3 query tuning techniques every sql server programmer should know

  • 2. Query Tuning Techniques Every SQL Server Programmer Should Know Rodrigo Crespi
  • 3. Rodrigo Crespi MCSE, MCSA, etc. PostDegree in Data Base – UGF PASS Chapter Leader – SQL Server RS University Professor of Database Management CrespiDB | Soluções em Plataforma de Dados @SQLCrespi rodrigocrespi S Q L C r e s p i sqlcrespi.com SQLCrespi
  • 4. First of All! ● Understand how a query works; ● Understand set theory; ● Most important try another thought.
  • 5. SQL Server Architecture P r o t o c o l o s Parser Optimizer SQL Mgr. DB. Mgr. Query Executor R e l a t i o n a l E n g i n e S t o r a g e E n g i n e T-Services Lock Mgr. File Mgr. Utilities Access Methods SQLOS SQLOS.dll SQLLang.dll PageLock Monitor Resource Monitor Lazy Writer Scheduler Monitor SQLmin.dll Buffer Pool Memory Mgr. Scheduling Sinc. Services Lock Mgr. I/O SQLDK.dll It’s a simple representation
  • 6. Relational Engine Optimizer Query Executor Cmd Parser SNI Protocol Layer Storage Engine Transactio n Manager Access Methods Buffer Manager Lock Manager Data Cache Plan Cache Data File T-SQL Select * from cliente T D S IF Query Tree O L E D B IF Buffer Pool Query Workspace Query Compile Token Perm XEvents,Metadados, outros...
  • 7. Relational Engine Optimizer Query Executor Cmd Parser SNI Protocol Layer Storage Engine Transactio n Manager Access Methods Buffer Manager Data File T-SQL Update cliente set nome = ‘Rodrigo’ T D S IF Query Tree O L E D B IF Lazy Writer CheckPoint Lock Manager Data Cache Plan Cache Buffer Pool Query Workspace Query Compile Token Perm Xevents,Metadados, outros... Dirty Page
  • 8. Physical Query Tree • It is the way the optimizer calculates the most efficient path for executing the command; • More efficient or more optimized?
  • 9. Zoom of Optimizer Parsing / binding Simplification Start a joins set Trivial Plan Optimization phase Search 0 Search 1 Search 2 Phase 0-Traditional: nested Loops, with no parallelism options. Phase 1 – Fast: Uses a fast subset of rules that contains the most common patterns. Phase 2 – Complete: For complex queries with parallelism and indexed views. Before Optimization Optimization
  • 10. Tips
  • 11. The best pattern is the one that every team follows! Pattern
  • 12. Formatting T-SQL for Readability ● Comments, author, and brief audit trail; ● Be Consistent; ● The best formatting pattern is the one that is followed by the entire team.
  • 13. How To Test a Query ● Clear caches; ● Try, Test and Compare; ● Set measurements; ● DMVs for check; ● Clone Database.
  • 14. T-SQL Performance • Set Statistics IO / Time; • Compare the execution plans;
  • 15. Predicates and Set Theory ● Say no to cursor; ● The Optimizer can’t pick the best execution plan if the Stored Procedure is broken up into many parts; ● Built-In functions that can affect performance; ● Like; ● And | Or | In; ● Union and union all; ● IsNull();
  • 16. Implicit Conversions ● The optimizer chooses the data type based on a predefined list; ○ https://docs.microsoft.com/en-us/sql/t-sql/data-types/data- type-precedence-transact- sql?redirectedfrom=MSDN&view=sql-server-ver15 ● Data Types matter; ● You may not get what you expect; ● Implicit conversions can be costly.
  • 17. Lock and NoLock Lock is required in SQL Databases. This is a true! HOWEVER, if you use hint nolock pay attention if you reading a dirty page!