SlideShare a Scribd company logo
Gianluca Sartori
Owner
sqlconsulting.it
My query is slow,
now what?
2
Gianluca Sartori
• Independent SQL Server consultant
• Working with SQL Server since version 7
• MCTS, MCITP, MCT
• DBA @ Formula 1 team
Blog: spaghettidba.com
Twitter: @spaghettidba
3
Agenda
My query is slow!
The performance tuning pyramid
Schema design
Code optimization
Indexing
The performance tuning
pyramid
5
Schema design
7
Schema Design
Normalization
1NF:
Must have a key, atomic attributes only
2NF:
Each attribute depends on the whole key
3NF:
Each attribute depends only on the key
«The key, the whole key and nothing but the key,
so help me Codd»
8
Schema Design
Denormalization clues
• Data repeated  redundancy
• Inconsistent data  anomalies
• Data separated by «,»
eg: john@gmail.com, john@business.com
• Structured data in «notes» columns
• Column names with a numeric suffix
eg: Area1, Area2 , Area3…
9
Schema Design Worst Practices
No Primary Key or surrogate keys only
«Id» is not the only possible key!
No Foreign Keys
They’re «difficult» to deal with
No CHECK constraint
The application will enforce consistency…
Wrong data types
VAT number, Telephone number
Dates stored as strings
Use of NULL where inappropriate
Use of «dummy» values (eg: ‘.’ , 0)
Query Optimization
11
Query optimization
RBAR
Code Re-Use
Sort / DISTINCT
A query to rule them all
Redundant subqueries
12
Query optimization
• Row By Agonizing Row
• Cursors
• WHILE loops
• App-side cursors
• Scalar and multi-statement functions
http://www.sqlservercentral.com/Authors/Articles/Jeff_Moden/80567/
Jeff Moden
13
RBAR - Cursors
• Procedural code
• Fixed execution strategy  doesn’t scale!
• Use lots of memory and CPU
• Use lots of tempdb space
• Execute a huge amount of statements
• Can (almost) always be replaced with set-based code
14
RBAR
15
Code Reuse
• Stored procedures, functions and views encapsulate
the complexity
• Code reuse works as far as it doesn’t hurt performance
• What to avoid:
• Stored procedure invoked inside cursor loops
• Scalar functions with data access
• Multi-statement table-valued functions
• Views on views on views…
16
Code Reuse
Stored procedures encapsulate complex logic
• Different from OO
• No inheritance, polymorfism etc…
Impossible to combine with other constructs
• Require cursors to be applied on a set
• App-side cursors are no better!
• The same applies to ORMs
If no data modification happens, better use a function (ITVF if
possible)
17
Code Reuse – Cursors and stored procedures
18
Functions
Scalar functions work well for complex calculations with
NO data access
• Invoked for each row in the input set
• «hidden» RBAR
Better use inline table-valued functions
• Multi-statement table-valued functions return table
variables  estimated cardinality = 1
• Merged in the outer execution plan
19
Code Reuse – Scalar functions
Might look like a brilliant idea at first
• You can end up losing control
• Unneeded multiple accesses to the same tables
• Unnecessary JOINs
Views on views on views…
21
Code Reuse – Views on views on views…
22
SORT / DISTINCT
If not necessary, don’t sort results
DISTINCT is the refugium peccatorum for missed JOIN
predicates
EXISTS often helps avoiding CROSS JOINs for filter
predicates
UNION is always DISTINCT
Use UNION ALL whenever possible
One query to rule them all
• Set-based is ok, everything in one query is too much
• The optimizer is good, not perfect
• Too complex: where do we start from?
• Divide et impera
• Break the code into pieces
• CTEs
• Temporary tables
• Table Variables
• Functions
• Identify redundant pieces
• Re-assemble
• http://spaghettidba.com/2012/03/15/how-to-eat-a-sql-elephant/
24
Divide et impera
Redundant Subqueries
Indexing
26
Indexing
SARGAbility
Deciding which indexes to create?
Common mistakes
Included columns
Execution plan analysis
How to read an execution plan
What to look for
27
SARGAbility
SARGABLE = Search ARGument ABLE
A predicate is «sargable» when it can be evaluated by
means of an index
• Non-Sargable in general is every predicate that requires
transforming the column before evaluation
eg: functions
WHERE YEAR(order_date) = YEAR(GETDATE())
28
SARGabilty - examples
WHERE YEAR(SellStartDate) = YEAR(GETDATE())
WHERE SellStartDate >= DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
AND SellStartDate < DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0)
WHERE ISNULL(ProductLine,'M') = 'M'
WHERE ProductLine = 'M' OR ProductLine IS NULL
WHERE LEFT(ProductNumber,2) = 'BK'
WHERE ProductNumber LIKE 'BK%'
29
Deciding which indexes to create
Create indexes to support the following predicates
• WHERE
• JOIN
• GROUP BY
• ORDER BY
Indexes are used effectively when the leading column appears
in the predicate
Execution plans suggest missing indexes
• Watch out from overly «aggressive» suggestions
30
Common mistakes
• One index for each column
• Missing indexes on Foreign Keys
• Accepting all suggestions from the DB Tuning Advisor
• Duplicate Indexes
• Sub-optimal clustered index
• Unique
• Small
• Invariable
• Ever-increasing
• Not testing on the whole workload
31
Included columns
Columns not present in filter predicates but only in the
SELECT list can be included
Included columns are not for free
• Present at leaf level in the B-tree
• Contribute to index size
Clustered index always includes all columns
Nonclustered indexes always include the clustering key
32
Execution plans
Output of the query optimizer
T-SQL
statement
Parser Algebrizer
Algebrizer
Tree
Optimizer
Statistics
Execution Plan
33
Execution plans
How to read an execution plan
34
Execution plans
What to look for:
Implicit conversions
Scans
Missing JOIN predicate alerts
Spools
Sorts
Parallelism
Wrong cardinality estimates
35
Recap
Simplify code
RBAR is the root of all evil
Remove redundant accesses
Divide et impera
Execution plans
Adding indexes may help
Unused indexes?
SARGability
Included columns
Schema design mistakes?
Q&A.
My Query is slow, now what?

More Related Content

PDF
Make an Entrance
PPS
Pictures of earth
PPTX
Learn Database Design with MySQL - Chapter 6 - Database design process
PDF
t4 e6 cardona
PPTX
Little and Often: Exploring the Potential of Information Literacy Mini-lessons
PPTX
Why do you want to study in UWE
PPTX
Learn Database Design with MySQL - Chapter 4 - Data types
PDF
Consolidation of diamond mines final draft
Make an Entrance
Pictures of earth
Learn Database Design with MySQL - Chapter 6 - Database design process
t4 e6 cardona
Little and Often: Exploring the Potential of Information Literacy Mini-lessons
Why do you want to study in UWE
Learn Database Design with MySQL - Chapter 4 - Data types
Consolidation of diamond mines final draft

Viewers also liked (9)

PPT
The Tipping Point - Final Presentation
PDF
презентация Costa alta copy (1)
PPTX
аз жаргалтай үлгэр
PDF
A review of Zimbabwe's draft minerals policy by ZELA
PPTX
How to make a relationship last
PPTX
Pregunta de investigacion
PDF
68 transaction scheduling in distributed real time systems
PPTX
Research, social media and getting published
PDF
2014 the boulder history museum
The Tipping Point - Final Presentation
презентация Costa alta copy (1)
аз жаргалтай үлгэр
A review of Zimbabwe's draft minerals policy by ZELA
How to make a relationship last
Pregunta de investigacion
68 transaction scheduling in distributed real time systems
Research, social media and getting published
2014 the boulder history museum
Ad

Similar to My Query is slow, now what? (20)

PPTX
Tips & Tricks SQL in the City Seattle 2014
PDF
SqlDay 2018 - Brief introduction into SQL Server Execution Plans
PPTX
The Plan Cache Whisperer - Performance Tuning SQL Server
PDF
Postgres for MySQL (and other database) people
PPTX
SQL Server Tips & Tricks
PDF
Geek Sync I Consolidating Indexes in SQL Server
PDF
Exploring T-SQL Anti-Patterns
PPTX
3 CityNetConf - sql+c#=u-sql
PPTX
MySQL 8 - UKOUG Techfest Brighton December 2nd, 2019
PPTX
Optimizing Application Performance - 2022.pptx
PPTX
SQL Server Worst Practices - EN
PDF
Presentation interpreting execution plans for sql statements
PPTX
PHP UK 2020 Tutorial: MySQL Indexes, Histograms And other ways To Speed Up Yo...
PDF
Database and application performance vivek sharma
PDF
Oracle SQL tuning with SQL Plan Management
PPTX
JSSUG: SQL Sever Index Tuning
PDF
Sql Performance Tuning For Developers
PPTX
Sql query performance analysis
PPTX
Saleforce For Domino Dogs
PDF
How MySQL can boost (or kill) your application v2
Tips & Tricks SQL in the City Seattle 2014
SqlDay 2018 - Brief introduction into SQL Server Execution Plans
The Plan Cache Whisperer - Performance Tuning SQL Server
Postgres for MySQL (and other database) people
SQL Server Tips & Tricks
Geek Sync I Consolidating Indexes in SQL Server
Exploring T-SQL Anti-Patterns
3 CityNetConf - sql+c#=u-sql
MySQL 8 - UKOUG Techfest Brighton December 2nd, 2019
Optimizing Application Performance - 2022.pptx
SQL Server Worst Practices - EN
Presentation interpreting execution plans for sql statements
PHP UK 2020 Tutorial: MySQL Indexes, Histograms And other ways To Speed Up Yo...
Database and application performance vivek sharma
Oracle SQL tuning with SQL Plan Management
JSSUG: SQL Sever Index Tuning
Sql Performance Tuning For Developers
Sql query performance analysis
Saleforce For Domino Dogs
How MySQL can boost (or kill) your application v2
Ad

More from Gianluca Sartori (9)

PPTX
Benchmarking like a pro
PPTX
Sql server infernals
PPTX
SQL Server 2016 New Security Features
PPTX
Responding to extended events in near real time
PPTX
Sql server security in an insecure world
PPTX
TSQL Advanced Query Techniques
PPTX
SQL Server Benchmarking, Baselining and Workload Analysis
PPTX
A performance tuning methodology
PPTX
SQL Server Worst Practices
Benchmarking like a pro
Sql server infernals
SQL Server 2016 New Security Features
Responding to extended events in near real time
Sql server security in an insecure world
TSQL Advanced Query Techniques
SQL Server Benchmarking, Baselining and Workload Analysis
A performance tuning methodology
SQL Server Worst Practices

Recently uploaded (20)

PPTX
1. Introduction to Computer Programming.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
MYSQL Presentation for SQL database connectivity
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Getting Started with Data Integration: FME Form 101
PDF
Electronic commerce courselecture one. Pdf
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PPTX
Tartificialntelligence_presentation.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
Machine Learning_overview_presentation.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
A Presentation on Artificial Intelligence
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
1. Introduction to Computer Programming.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Dropbox Q2 2025 Financial Results & Investor Presentation
MYSQL Presentation for SQL database connectivity
MIND Revenue Release Quarter 2 2025 Press Release
Getting Started with Data Integration: FME Form 101
Electronic commerce courselecture one. Pdf
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Tartificialntelligence_presentation.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Building Integrated photovoltaic BIPV_UPV.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Machine Learning_overview_presentation.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
A comparative analysis of optical character recognition models for extracting...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
A Presentation on Artificial Intelligence
Mobile App Security Testing_ A Comprehensive Guide.pdf

My Query is slow, now what?

  • 2. 2 Gianluca Sartori • Independent SQL Server consultant • Working with SQL Server since version 7 • MCTS, MCITP, MCT • DBA @ Formula 1 team Blog: spaghettidba.com Twitter: @spaghettidba
  • 3. 3 Agenda My query is slow! The performance tuning pyramid Schema design Code optimization Indexing
  • 5. 5
  • 7. 7 Schema Design Normalization 1NF: Must have a key, atomic attributes only 2NF: Each attribute depends on the whole key 3NF: Each attribute depends only on the key «The key, the whole key and nothing but the key, so help me Codd»
  • 8. 8 Schema Design Denormalization clues • Data repeated  redundancy • Inconsistent data  anomalies • Data separated by «,» eg: john@gmail.com, john@business.com • Structured data in «notes» columns • Column names with a numeric suffix eg: Area1, Area2 , Area3…
  • 9. 9 Schema Design Worst Practices No Primary Key or surrogate keys only «Id» is not the only possible key! No Foreign Keys They’re «difficult» to deal with No CHECK constraint The application will enforce consistency… Wrong data types VAT number, Telephone number Dates stored as strings Use of NULL where inappropriate Use of «dummy» values (eg: ‘.’ , 0)
  • 11. 11 Query optimization RBAR Code Re-Use Sort / DISTINCT A query to rule them all Redundant subqueries
  • 12. 12 Query optimization • Row By Agonizing Row • Cursors • WHILE loops • App-side cursors • Scalar and multi-statement functions http://www.sqlservercentral.com/Authors/Articles/Jeff_Moden/80567/ Jeff Moden
  • 13. 13 RBAR - Cursors • Procedural code • Fixed execution strategy  doesn’t scale! • Use lots of memory and CPU • Use lots of tempdb space • Execute a huge amount of statements • Can (almost) always be replaced with set-based code
  • 15. 15 Code Reuse • Stored procedures, functions and views encapsulate the complexity • Code reuse works as far as it doesn’t hurt performance • What to avoid: • Stored procedure invoked inside cursor loops • Scalar functions with data access • Multi-statement table-valued functions • Views on views on views…
  • 16. 16 Code Reuse Stored procedures encapsulate complex logic • Different from OO • No inheritance, polymorfism etc… Impossible to combine with other constructs • Require cursors to be applied on a set • App-side cursors are no better! • The same applies to ORMs If no data modification happens, better use a function (ITVF if possible)
  • 17. 17 Code Reuse – Cursors and stored procedures
  • 18. 18 Functions Scalar functions work well for complex calculations with NO data access • Invoked for each row in the input set • «hidden» RBAR Better use inline table-valued functions • Multi-statement table-valued functions return table variables  estimated cardinality = 1 • Merged in the outer execution plan
  • 19. 19 Code Reuse – Scalar functions
  • 20. Might look like a brilliant idea at first • You can end up losing control • Unneeded multiple accesses to the same tables • Unnecessary JOINs Views on views on views…
  • 21. 21 Code Reuse – Views on views on views…
  • 22. 22 SORT / DISTINCT If not necessary, don’t sort results DISTINCT is the refugium peccatorum for missed JOIN predicates EXISTS often helps avoiding CROSS JOINs for filter predicates UNION is always DISTINCT Use UNION ALL whenever possible
  • 23. One query to rule them all • Set-based is ok, everything in one query is too much • The optimizer is good, not perfect • Too complex: where do we start from? • Divide et impera • Break the code into pieces • CTEs • Temporary tables • Table Variables • Functions • Identify redundant pieces • Re-assemble • http://spaghettidba.com/2012/03/15/how-to-eat-a-sql-elephant/
  • 26. 26 Indexing SARGAbility Deciding which indexes to create? Common mistakes Included columns Execution plan analysis How to read an execution plan What to look for
  • 27. 27 SARGAbility SARGABLE = Search ARGument ABLE A predicate is «sargable» when it can be evaluated by means of an index • Non-Sargable in general is every predicate that requires transforming the column before evaluation eg: functions WHERE YEAR(order_date) = YEAR(GETDATE())
  • 28. 28 SARGabilty - examples WHERE YEAR(SellStartDate) = YEAR(GETDATE()) WHERE SellStartDate >= DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) AND SellStartDate < DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0) WHERE ISNULL(ProductLine,'M') = 'M' WHERE ProductLine = 'M' OR ProductLine IS NULL WHERE LEFT(ProductNumber,2) = 'BK' WHERE ProductNumber LIKE 'BK%'
  • 29. 29 Deciding which indexes to create Create indexes to support the following predicates • WHERE • JOIN • GROUP BY • ORDER BY Indexes are used effectively when the leading column appears in the predicate Execution plans suggest missing indexes • Watch out from overly «aggressive» suggestions
  • 30. 30 Common mistakes • One index for each column • Missing indexes on Foreign Keys • Accepting all suggestions from the DB Tuning Advisor • Duplicate Indexes • Sub-optimal clustered index • Unique • Small • Invariable • Ever-increasing • Not testing on the whole workload
  • 31. 31 Included columns Columns not present in filter predicates but only in the SELECT list can be included Included columns are not for free • Present at leaf level in the B-tree • Contribute to index size Clustered index always includes all columns Nonclustered indexes always include the clustering key
  • 32. 32 Execution plans Output of the query optimizer T-SQL statement Parser Algebrizer Algebrizer Tree Optimizer Statistics Execution Plan
  • 33. 33 Execution plans How to read an execution plan
  • 34. 34 Execution plans What to look for: Implicit conversions Scans Missing JOIN predicate alerts Spools Sorts Parallelism Wrong cardinality estimates
  • 35. 35 Recap Simplify code RBAR is the root of all evil Remove redundant accesses Divide et impera Execution plans Adding indexes may help Unused indexes? SARGability Included columns Schema design mistakes?
  • 36. Q&A.