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!
SPONSORS
¡Gracias!
@SQLCrespi

3 query tuning techniques every sql server programmer should know

  • 1.
  • 2.
    Query Tuning TechniquesEvery 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 SQLMgr. 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 ProtocolLayer 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 ProtocolLayer 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.
  • 11.
    The best pattern isthe one that every team follows! Pattern
  • 12.
    Formatting T-SQL forReadability ● 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 Testa Query ● Clear caches; ● Try, Test and Compare; ● Set measurements; ● DMVs for check; ● Clone Database.
  • 14.
    T-SQL Performance • SetStatistics IO / Time; • Compare the execution plans;
  • 15.
    Predicates and SetTheory ● 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 ● Theoptimizer 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 Lockis required in SQL Databases. This is a true! HOWEVER, if you use hint nolock pay attention if you reading a dirty page!
  • 19.
  • 20.