SlideShare a Scribd company logo
Query tuning techniques
Rajnikant Tandel, DBA Manager, Datavail
Anup Gopinathan, Sr. SQL Server DBA, Datavail
Optimize SQL Server
Please silence
cell phones
Free online webinar
events
Free 1-day local
training events
Local user groups
around the world
Online special
interest user groups
Business analytics
training
Get involved
Explore
everything
PASS has
to offer
Free Online Resources
Newsletters
PASS.org
Download the GuideBook App
and search: PASS Summit 2018
Follow the QR code link displayed on session
signage throughout the conference venue and
in the program guide
Session
evaluations
Your feedback is
important and valuable.
Go to passSummit.com
3 Ways to Access:
Submit by 5pm Friday, November 16th to win prizes.
Anup Gopinathan
Sr. SQL Server DBA, Datavail
Sr. DBA with over 11 years of
experience in database design,
development & administration and
cloud computing. Extensive customer
engagement experience, supported
and worked with various customers.
Database performance tuning
Query tuning, execution plan analysis, index
and statistics optimization.
Database Scalability
Table partitioning, analyze isolation and
locking mechanisms, load balancing
/anupgopinathannair
@GopinathanAnup
Powershell scripting and
automations
Cloud Data Architecture/Cloud
Migration
Design highly available and scalable database
systems in cloud, migrate database from On-
premises to cloud
Rajnikant Tandel
DBA Manager
I’m a Microsoft Certified Solution
Associate, SQL Server 2012/2014 and
have over 15 years of SQL Server DBA
experience on relational databases
and application development and
specialize in performance tuning,
scalability and maintainability of large
scale SQL Servers. I’ve worked
extensively on performance tuning
and query optimization projects.
/rajnikant-tandel
@tandelrajni
Background
• Programmer
• Database Developer/DBA
• Database Administrator
• DBA Manager
Datavail Overview
Databases, Analytics, and
Application Data Integration
13+ years delivering
data services
300+ customers with
average client
retention of 7 years
Managed services,
projects, and staffing
• All major platforms: SQL, Oracle,
DB2, MySQL, Mongo DB
• Comprehensive development &
operational services
• US & Global models
• Reactive and proactive services
Database
Operations
• Microsoft SSRS, SSIS, SSAS
• PowerBI
• Oracle OBIEE, BICS, Golden
Gate
• EBS
• Hyperion/Essbase
• Microsoft Dynamics
• SharePoint
• Custom development
• Microsoft .NET
• Oracle APEX
Analytics, Visualization & Integration
Application
Management
We are data specialists
• Informatica
• Oracle Data Integrator
• ETL development and
operational run
Win Shoes
Fill out the session survey
for a chance to win an
exclusive pair of Datavail’s
running shoes!
• T-SQL new functions and performance – How to use SQL Server 2017 new
string functions to improve performance?
• Lock Escalation – How? When? Why?
• Table Variable V/S Temporary table V/S Correlated Subquery – compare
performance.
• Parameter sniffing – a look into how performance is affected.
Agenda
T-SQL new functions and performance
Example:
select category_code,
STRING_AGG(category_desc, ', ') within group
(order by category_desc) categories
from category
group by category_code
category_code categories
1 Groceries, Lodging, Meals, Travel
2
Clothing, Communication, Electronics,
Entertainment, Home Supplies, Misc
STRING_AGG()
Concatenates the values of
string expressions and places
separator values between
them. The separator is not
added at the end of string.
STRING_AGG()
T-SQL new functions and performance
TRANSLATE()
Returns the string provided as a
first argument after some
characters specified in the
second argument are translated
into a destination set of
characters.
TRANSLATE (inputstring,
characters,
translations)
Example:
SELECT TRANSLATE('2*[3+4]/{7-2}',
'[]{}', '()()’);
Output:
2*(3+4)/(7-2)
TRANSLATE()
T-SQL new functions and performance
Summary
• STRING_AGG() or TRANSLATE() perform better than user defined function or
other T-SQL methods
• Use CAST() when get an error “STRING_AGG aggregation result exceeded the
limit of 8000 bytes.”
• Consider replacing user defined function with new T-SQL functions
Questions &
Answers
Lock Escalation
The process of acquiring
higher level lock instead of
many smaller locks is called
lock escalation.
How does escalation happen?
1250
5000
Row locks Page Lock Table lock
Can we disable the lock escalation?
Table hints
• Row lock
• Page lock
ALTER TABLE CHARGE SET (LOCK_ESCALATION
= TABLE | DISABLE | AUTO)
• TABLE
• DISABLE
• AUTO
There are some options available to control it.
Lock Escalation
Summary
• Lock escalation can be controlled by
using table hints and by disabling
escalation at table level.
• Understand the pros and cons of
each method.
• SQL Engine can still escalate
• “TEST”
Questions &
Answers
Table Variable vs
Temp Table vs Correlated Subquery
Table Variable
• Can only be accessed within the batch and scope in which
they are declared
• Stored in TempDB
• Because of the well-defined scope, a table variable
generally uses fewer resources than a temporary table.
• No statistics-based recompiles
• Dropped automatically
DECLARE @Totalcharge AS TABLE
(Charge_no INT,
Amount money,
Date_Inserted datetime)
There’s
comfort in
knowing that
everything is
temporary.
Table Variable vs
Temp Table vs Correlated Subquery
Temporary Table
• Stored in TempDB
• Can create Indexes
• Deleting a Temporary Table
• Automatically when connection is
closed
• Manually using DROP Table
statement
• Performs better with large
number of rows
create table #Totalcharge
(Charge_no INT,
Amount money,
Date_Inserted datetime)
Table Variable vs
Temp Table vs Correlated Subquery
Correlated Subquery
• A correlated subquery is a
SELECT statement nested
inside another T-SQL
statement, which contains
a reference to one or more
columns in the outer
query.
• The correlated subquery
will be run once for each
candidate row selected by
the outer query.
SELECT column1, column2, ....
FROM table1 outer
WHERE column1 operator
(SELECT column1, column2
FROM table2
WHERE expr1 = outer.expr2);
Table Variable vs
Temp Table vs
Correlated Subquery
Table Variable vs Temp Table vs Correlated
Subquery
Summary
• Table variable performs well for
smaller set of rows
• Low maintenance cost
• No statistics-based recompiles
• Temp Table performs better for larger result set
• Can Create Indexes and Statistics
• Correlated subquery can be inefficient because it evaluated once for each row processed
by the outer query
Questions &
Answers
Parameter Sniffing
The execution plan for
stored procedures does
not work well for some
arguments.
Is this a bug?
Parameter sniffing
is not a bug
Parameter
Sniffing
Summary
• Recompile
• OPTION (OPTIMIZE FOR (@VARIABLE=VALUE | UNKNOWN))
• Disable parameter sniffing using Trace Flag 4136 / Database Scoped
Configuration
• local variableIt is not a bug.
• Analyze your workloads
• Testing
Questions &
Answers
References
• www.mssqltips.com
• sqlmag.com
• sqlservercentral
• stackoverflow.com
• Sqlserverperformance
• Statisticsparser.com
Thank You
Rajnikant Tandel
@tandelrajni
Rajnikant.Tandel@datavail.com
Anup Gopinathan
@GopinathanAnup
Anup.Gopinathan@datavail.com
Learn more from Learn more from
Win Shoes
Fill out the session survey
for a chance to win an
exclusive pair of Datavail’s
running shoes!
Ad

Recommended

U-SQL Does SQL (SQLBits 2016)
U-SQL Does SQL (SQLBits 2016)
Michael Rys
 
Stored procedure tuning and optimization t sql
Stored procedure tuning and optimization t sql
nishantdavid9
 
SQL Server Workshop for Developers - Visual Studio Live! NY 2012
SQL Server Workshop for Developers - Visual Studio Live! NY 2012
Andrew Brust
 
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressions
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressions
dubon07
 
Unit 1 - SAP System Architecture
Unit 1 - SAP System Architecture
dubon07
 
Unit 2 - Object Navigator, Repository and ABAP Programs
Unit 2 - Object Navigator, Repository and ABAP Programs
dubon07
 
Unit 3 - Transparent tables in the ABAP Dictionary
Unit 3 - Transparent tables in the ABAP Dictionary
dubon07
 
Express Yourself: Building Expressions with Microsoft Flow - SPSCLT 2018
Express Yourself: Building Expressions with Microsoft Flow - SPSCLT 2018
Fausto Capellan Jr
 
Sql performance tuning
Sql performance tuning
Leo Mark Villar
 
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09
guest9d79e073
 
Tunning overview
Tunning overview
Hitesh Kumar Markam
 
SQL Windowing
SQL Windowing
Sandun Perera
 
Before you optimize: Understanding Execution Plans
Before you optimize: Understanding Execution Plans
Timothy Corey
 
Uncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony Davis
Red Gate Software
 
IBM SPSS Statistics Subscription (월 구독) 제품 구성
IBM SPSS Statistics Subscription (월 구독) 제품 구성
Jin Sol Kim 김진솔
 
Exciting Features for SQL Devs in SQL 2012
Exciting Features for SQL Devs in SQL 2012
Brij Mishra
 
Back to the future - Temporal Table in SQL Server 2016
Back to the future - Temporal Table in SQL Server 2016
Stéphane Fréchette
 
MS SQL SERVER: Programming sql server data mining
MS SQL SERVER: Programming sql server data mining
DataminingTools Inc
 
Efficient SPARQL to SQL translation with user defined mapping
Efficient SPARQL to SQL translation with user defined mapping
Miloš Chaloupka
 
Pass 2018 introduction to dax
Pass 2018 introduction to dax
Ike Ellis
 
Taming the Data Science Monster with A New ‘Sword’ – U-SQL
Taming the Data Science Monster with A New ‘Sword’ – U-SQL
Michael Rys
 
Window functions with SQL Server 2016
Window functions with SQL Server 2016
Mark Tabladillo
 
SAS Online Training
SAS Online Training
Nagendra Kumar
 
SAS Online Training
SAS Online Training
revanthonlineenquiry
 
Project A Data Modelling Best Practices Part II: How to Build a Data Warehouse?
Project A Data Modelling Best Practices Part II: How to Build a Data Warehouse?
Martin Loetzsch
 
Hundreds of queries in the time of one - Gianmario Spacagna
Hundreds of queries in the time of one - Gianmario Spacagna
Spark Summit
 
02 database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
Amazon Redshift For Data Analysts
Amazon Redshift For Data Analysts
Can Abacıgil
 
Tuning and Optimizing U-SQL Queries (SQLPASS 2016)
Tuning and Optimizing U-SQL Queries (SQLPASS 2016)
Michael Rys
 
3 CityNetConf - sql+c#=u-sql
3 CityNetConf - sql+c#=u-sql
Łukasz Grala
 

More Related Content

What's hot (20)

Sql performance tuning
Sql performance tuning
Leo Mark Villar
 
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09
guest9d79e073
 
Tunning overview
Tunning overview
Hitesh Kumar Markam
 
SQL Windowing
SQL Windowing
Sandun Perera
 
Before you optimize: Understanding Execution Plans
Before you optimize: Understanding Execution Plans
Timothy Corey
 
Uncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony Davis
Red Gate Software
 
IBM SPSS Statistics Subscription (월 구독) 제품 구성
IBM SPSS Statistics Subscription (월 구독) 제품 구성
Jin Sol Kim 김진솔
 
Exciting Features for SQL Devs in SQL 2012
Exciting Features for SQL Devs in SQL 2012
Brij Mishra
 
Back to the future - Temporal Table in SQL Server 2016
Back to the future - Temporal Table in SQL Server 2016
Stéphane Fréchette
 
MS SQL SERVER: Programming sql server data mining
MS SQL SERVER: Programming sql server data mining
DataminingTools Inc
 
Efficient SPARQL to SQL translation with user defined mapping
Efficient SPARQL to SQL translation with user defined mapping
Miloš Chaloupka
 
Pass 2018 introduction to dax
Pass 2018 introduction to dax
Ike Ellis
 
Taming the Data Science Monster with A New ‘Sword’ – U-SQL
Taming the Data Science Monster with A New ‘Sword’ – U-SQL
Michael Rys
 
Window functions with SQL Server 2016
Window functions with SQL Server 2016
Mark Tabladillo
 
SAS Online Training
SAS Online Training
Nagendra Kumar
 
SAS Online Training
SAS Online Training
revanthonlineenquiry
 
Project A Data Modelling Best Practices Part II: How to Build a Data Warehouse?
Project A Data Modelling Best Practices Part II: How to Build a Data Warehouse?
Martin Loetzsch
 
Hundreds of queries in the time of one - Gianmario Spacagna
Hundreds of queries in the time of one - Gianmario Spacagna
Spark Summit
 
02 database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
Amazon Redshift For Data Analysts
Amazon Redshift For Data Analysts
Can Abacıgil
 
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09
guest9d79e073
 
Before you optimize: Understanding Execution Plans
Before you optimize: Understanding Execution Plans
Timothy Corey
 
Uncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony Davis
Red Gate Software
 
IBM SPSS Statistics Subscription (월 구독) 제품 구성
IBM SPSS Statistics Subscription (월 구독) 제품 구성
Jin Sol Kim 김진솔
 
Exciting Features for SQL Devs in SQL 2012
Exciting Features for SQL Devs in SQL 2012
Brij Mishra
 
Back to the future - Temporal Table in SQL Server 2016
Back to the future - Temporal Table in SQL Server 2016
Stéphane Fréchette
 
MS SQL SERVER: Programming sql server data mining
MS SQL SERVER: Programming sql server data mining
DataminingTools Inc
 
Efficient SPARQL to SQL translation with user defined mapping
Efficient SPARQL to SQL translation with user defined mapping
Miloš Chaloupka
 
Pass 2018 introduction to dax
Pass 2018 introduction to dax
Ike Ellis
 
Taming the Data Science Monster with A New ‘Sword’ – U-SQL
Taming the Data Science Monster with A New ‘Sword’ – U-SQL
Michael Rys
 
Window functions with SQL Server 2016
Window functions with SQL Server 2016
Mark Tabladillo
 
Project A Data Modelling Best Practices Part II: How to Build a Data Warehouse?
Project A Data Modelling Best Practices Part II: How to Build a Data Warehouse?
Martin Loetzsch
 
Hundreds of queries in the time of one - Gianmario Spacagna
Hundreds of queries in the time of one - Gianmario Spacagna
Spark Summit
 
02 database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
Amazon Redshift For Data Analysts
Amazon Redshift For Data Analysts
Can Abacıgil
 

Similar to SQL Pass Summit Presentations from Datavail - Optimize SQL Server: Query Tuning Techniques (20)

Tuning and Optimizing U-SQL Queries (SQLPASS 2016)
Tuning and Optimizing U-SQL Queries (SQLPASS 2016)
Michael Rys
 
3 CityNetConf - sql+c#=u-sql
3 CityNetConf - sql+c#=u-sql
Łukasz Grala
 
Getting Strated with Amazon Dynamo DB (Jim Scharf) - AWS DB Day
Getting Strated with Amazon Dynamo DB (Jim Scharf) - AWS DB Day
Amazon Web Services Korea
 
Db2 sql tuning and bmc catalog manager
Db2 sql tuning and bmc catalog manager
Krishan Singh
 
Exploring T-SQL Anti-Patterns
Exploring T-SQL Anti-Patterns
Antonios Chatzipavlis
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Ronald Francisco Vargas Quesada
 
SQL Server 2016 novelties
SQL Server 2016 novelties
MSDEVMTL
 
Best Practices for Supercharging Cloud Analytics on Amazon Redshift
Best Practices for Supercharging Cloud Analytics on Amazon Redshift
SnapLogic
 
Overview of query evaluation
Overview of query evaluation
avniS
 
Taming the shrew Power BI
Taming the shrew Power BI
Kellyn Pot'Vin-Gorman
 
U-SQL - Azure Data Lake Analytics for Developers
U-SQL - Azure Data Lake Analytics for Developers
Michael Rys
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for Programmers
Adam Hutson
 
Columnstore improvements in SQL Server 2016
Columnstore improvements in SQL Server 2016
Niko Neugebauer
 
Sql 2016 - What's New
Sql 2016 - What's New
dpcobb
 
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09
Mark Ginnebaugh
 
A Metadata-Driven Approach to Computing Financial Analytics in a Relational D...
A Metadata-Driven Approach to Computing Financial Analytics in a Relational D...
inscit2006
 
Self-serve analytics journey at Celtra: Snowflake, Spark, and Databricks
Self-serve analytics journey at Celtra: Snowflake, Spark, and Databricks
Grega Kespret
 
Database testing
Database testing
Pesara Swamy
 
Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008
paulguerin
 
Understanding the SQL aspects of Spark - Spark SQL.pptx
Understanding the SQL aspects of Spark - Spark SQL.pptx
Knoldus Inc.
 
Tuning and Optimizing U-SQL Queries (SQLPASS 2016)
Tuning and Optimizing U-SQL Queries (SQLPASS 2016)
Michael Rys
 
3 CityNetConf - sql+c#=u-sql
3 CityNetConf - sql+c#=u-sql
Łukasz Grala
 
Getting Strated with Amazon Dynamo DB (Jim Scharf) - AWS DB Day
Getting Strated with Amazon Dynamo DB (Jim Scharf) - AWS DB Day
Amazon Web Services Korea
 
Db2 sql tuning and bmc catalog manager
Db2 sql tuning and bmc catalog manager
Krishan Singh
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Ronald Francisco Vargas Quesada
 
SQL Server 2016 novelties
SQL Server 2016 novelties
MSDEVMTL
 
Best Practices for Supercharging Cloud Analytics on Amazon Redshift
Best Practices for Supercharging Cloud Analytics on Amazon Redshift
SnapLogic
 
Overview of query evaluation
Overview of query evaluation
avniS
 
U-SQL - Azure Data Lake Analytics for Developers
U-SQL - Azure Data Lake Analytics for Developers
Michael Rys
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for Programmers
Adam Hutson
 
Columnstore improvements in SQL Server 2016
Columnstore improvements in SQL Server 2016
Niko Neugebauer
 
Sql 2016 - What's New
Sql 2016 - What's New
dpcobb
 
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09
Mark Ginnebaugh
 
A Metadata-Driven Approach to Computing Financial Analytics in a Relational D...
A Metadata-Driven Approach to Computing Financial Analytics in a Relational D...
inscit2006
 
Self-serve analytics journey at Celtra: Snowflake, Spark, and Databricks
Self-serve analytics journey at Celtra: Snowflake, Spark, and Databricks
Grega Kespret
 
Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008
paulguerin
 
Understanding the SQL aspects of Spark - Spark SQL.pptx
Understanding the SQL aspects of Spark - Spark SQL.pptx
Knoldus Inc.
 
Ad

More from Datavail (20)

Journey to Cloud Analytics
Journey to Cloud Analytics
Datavail
 
Accelerate SQL Server Migration to the AWS Cloud
Accelerate SQL Server Migration to the AWS Cloud
Datavail
 
MOUS 2020 - Hyperion 11.2 vs. Cloud: Should I Stay or Should I Go?
MOUS 2020 - Hyperion 11.2 vs. Cloud: Should I Stay or Should I Go?
Datavail
 
Oracle Enterprise Manager Seven Robust Features to Put in Action final
Oracle Enterprise Manager Seven Robust Features to Put in Action final
Datavail
 
Lessons from Migrating Oracle Databases to Amazon RDS or Amazon Aurora
Lessons from Migrating Oracle Databases to Amazon RDS or Amazon Aurora
Datavail
 
EPM 11.2: Lessons Learned and 2021 Preparedness
EPM 11.2: Lessons Learned and 2021 Preparedness
Datavail
 
Optimizing Oracle Databases & Applications Gives Fast Food Giant Major Gains
Optimizing Oracle Databases & Applications Gives Fast Food Giant Major Gains
Datavail
 
RMOUG 2020: Keeping Pace with Change
RMOUG 2020: Keeping Pace with Change
Datavail
 
Upcoming Extended Support Deadlines & What They Mean for You
Upcoming Extended Support Deadlines & What They Mean for You
Datavail
 
SQL on Linux
SQL on Linux
Datavail
 
Reduce Cost by Tuning Queries on Azure DBaaS
Reduce Cost by Tuning Queries on Azure DBaaS
Datavail
 
MOUS 2019 - Keeping Pace with Change: Prepare for Tomorrow & Advance Your Car...
MOUS 2019 - Keeping Pace with Change: Prepare for Tomorrow & Advance Your Car...
Datavail
 
Essbase On-Prem to Oracle Analytics Cloud - How, When, and Why
Essbase On-Prem to Oracle Analytics Cloud - How, When, and Why
Datavail
 
Is "Free" Good Enough for Your MySQL Environment?
Is "Free" Good Enough for Your MySQL Environment?
Datavail
 
Critical Preflight Checks for Your EPM Applications
Critical Preflight Checks for Your EPM Applications
Datavail
 
SQL to Azure Migrations
SQL to Azure Migrations
Datavail
 
Essbase On-Prem to Oracle Analytics Cloud - How, When, and Why
Essbase On-Prem to Oracle Analytics Cloud - How, When, and Why
Datavail
 
3 Ways to Lead an Accidental DBA
3 Ways to Lead an Accidental DBA
Datavail
 
Creating a Solid EPM Punch List
Creating a Solid EPM Punch List
Datavail
 
Why NBC Universal Migrated to MongoDB Atlas
Why NBC Universal Migrated to MongoDB Atlas
Datavail
 
Journey to Cloud Analytics
Journey to Cloud Analytics
Datavail
 
Accelerate SQL Server Migration to the AWS Cloud
Accelerate SQL Server Migration to the AWS Cloud
Datavail
 
MOUS 2020 - Hyperion 11.2 vs. Cloud: Should I Stay or Should I Go?
MOUS 2020 - Hyperion 11.2 vs. Cloud: Should I Stay or Should I Go?
Datavail
 
Oracle Enterprise Manager Seven Robust Features to Put in Action final
Oracle Enterprise Manager Seven Robust Features to Put in Action final
Datavail
 
Lessons from Migrating Oracle Databases to Amazon RDS or Amazon Aurora
Lessons from Migrating Oracle Databases to Amazon RDS or Amazon Aurora
Datavail
 
EPM 11.2: Lessons Learned and 2021 Preparedness
EPM 11.2: Lessons Learned and 2021 Preparedness
Datavail
 
Optimizing Oracle Databases & Applications Gives Fast Food Giant Major Gains
Optimizing Oracle Databases & Applications Gives Fast Food Giant Major Gains
Datavail
 
RMOUG 2020: Keeping Pace with Change
RMOUG 2020: Keeping Pace with Change
Datavail
 
Upcoming Extended Support Deadlines & What They Mean for You
Upcoming Extended Support Deadlines & What They Mean for You
Datavail
 
SQL on Linux
SQL on Linux
Datavail
 
Reduce Cost by Tuning Queries on Azure DBaaS
Reduce Cost by Tuning Queries on Azure DBaaS
Datavail
 
MOUS 2019 - Keeping Pace with Change: Prepare for Tomorrow & Advance Your Car...
MOUS 2019 - Keeping Pace with Change: Prepare for Tomorrow & Advance Your Car...
Datavail
 
Essbase On-Prem to Oracle Analytics Cloud - How, When, and Why
Essbase On-Prem to Oracle Analytics Cloud - How, When, and Why
Datavail
 
Is "Free" Good Enough for Your MySQL Environment?
Is "Free" Good Enough for Your MySQL Environment?
Datavail
 
Critical Preflight Checks for Your EPM Applications
Critical Preflight Checks for Your EPM Applications
Datavail
 
SQL to Azure Migrations
SQL to Azure Migrations
Datavail
 
Essbase On-Prem to Oracle Analytics Cloud - How, When, and Why
Essbase On-Prem to Oracle Analytics Cloud - How, When, and Why
Datavail
 
3 Ways to Lead an Accidental DBA
3 Ways to Lead an Accidental DBA
Datavail
 
Creating a Solid EPM Punch List
Creating a Solid EPM Punch List
Datavail
 
Why NBC Universal Migrated to MongoDB Atlas
Why NBC Universal Migrated to MongoDB Atlas
Datavail
 
Ad

Recently uploaded (20)

MCB Internship report for the year of 2025
MCB Internship report for the year of 2025
PakistanPrinting
 
NASA ESE Study Results v4 05.29.2020.pptx
NASA ESE Study Results v4 05.29.2020.pptx
CiroAlejandroCamacho
 
定制OCAD学生卡加拿大安大略艺术与设计大学成绩单范本,OCAD成绩单复刻
定制OCAD学生卡加拿大安大略艺术与设计大学成绩单范本,OCAD成绩单复刻
taqyed
 
QUALITATIVE EXPLANATORY VARIABLES REGRESSION MODELS
QUALITATIVE EXPLANATORY VARIABLES REGRESSION MODELS
Ameya Patekar
 
Attendance Presentation Project Excel.pptx
Attendance Presentation Project Excel.pptx
s2025266191
 
Module 1Integrity_and_Ethics_PPT-2025.pptx
Module 1Integrity_and_Ethics_PPT-2025.pptx
Karikalcholan Mayavan
 
reporting monthly for genset & Air Compressor.pptx
reporting monthly for genset & Air Compressor.pptx
dacripapanjaitan
 
Team_Mercury.pdf hai kya hai kya hai kya hai kya hai kya
Team_Mercury.pdf hai kya hai kya hai kya hai kya hai kya
genadit49
 
llm_presentation and deep learning methods
llm_presentation and deep learning methods
sayedabdussalam11
 
Veilig en vlot fietsen in Oost-Vlaanderen: Fietssnelwegen geoptimaliseerd met...
Veilig en vlot fietsen in Oost-Vlaanderen: Fietssnelwegen geoptimaliseerd met...
jacoba18
 
presentation4.pdf Intro to mcmc methodss
presentation4.pdf Intro to mcmc methodss
SergeyTsygankov6
 
BCG-Executive-Perspectives-CEOs-Guide-to-Maximizing-Value-from-AI-EP0-3July20...
BCG-Executive-Perspectives-CEOs-Guide-to-Maximizing-Value-from-AI-EP0-3July20...
benediktnetzer1
 
Pause Travail 22 Hostiou Girard 12 juin 2025.pdf
Pause Travail 22 Hostiou Girard 12 juin 2025.pdf
Institut de l'Elevage - Idele
 
Measurecamp Copenhagen - Consent Context
Measurecamp Copenhagen - Consent Context
Human37
 
LECTURE_skakkakwowowkwkkwkskwkqowoowoaoaoa.cooos
LECTURE_skakkakwowowkwkkwkskwkqowoowoaoaoa.cooos
ssuseraf13da
 
UPS and Big Data intro to Business Analytics.pptx
UPS and Big Data intro to Business Analytics.pptx
sanjum5582
 
REGRESSION DIAGNOSTIC II: HETEROSCEDASTICITY
REGRESSION DIAGNOSTIC II: HETEROSCEDASTICITY
Ameya Patekar
 
Data-Driven-Operational--Excellence.pptx
Data-Driven-Operational--Excellence.pptx
NiwanthaThilanjanaGa
 
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
taqyea
 
Power BI API Connectors - Best Practices for Scalable Data Connections
Power BI API Connectors - Best Practices for Scalable Data Connections
Vidicorp Ltd
 
MCB Internship report for the year of 2025
MCB Internship report for the year of 2025
PakistanPrinting
 
NASA ESE Study Results v4 05.29.2020.pptx
NASA ESE Study Results v4 05.29.2020.pptx
CiroAlejandroCamacho
 
定制OCAD学生卡加拿大安大略艺术与设计大学成绩单范本,OCAD成绩单复刻
定制OCAD学生卡加拿大安大略艺术与设计大学成绩单范本,OCAD成绩单复刻
taqyed
 
QUALITATIVE EXPLANATORY VARIABLES REGRESSION MODELS
QUALITATIVE EXPLANATORY VARIABLES REGRESSION MODELS
Ameya Patekar
 
Attendance Presentation Project Excel.pptx
Attendance Presentation Project Excel.pptx
s2025266191
 
Module 1Integrity_and_Ethics_PPT-2025.pptx
Module 1Integrity_and_Ethics_PPT-2025.pptx
Karikalcholan Mayavan
 
reporting monthly for genset & Air Compressor.pptx
reporting monthly for genset & Air Compressor.pptx
dacripapanjaitan
 
Team_Mercury.pdf hai kya hai kya hai kya hai kya hai kya
Team_Mercury.pdf hai kya hai kya hai kya hai kya hai kya
genadit49
 
llm_presentation and deep learning methods
llm_presentation and deep learning methods
sayedabdussalam11
 
Veilig en vlot fietsen in Oost-Vlaanderen: Fietssnelwegen geoptimaliseerd met...
Veilig en vlot fietsen in Oost-Vlaanderen: Fietssnelwegen geoptimaliseerd met...
jacoba18
 
presentation4.pdf Intro to mcmc methodss
presentation4.pdf Intro to mcmc methodss
SergeyTsygankov6
 
BCG-Executive-Perspectives-CEOs-Guide-to-Maximizing-Value-from-AI-EP0-3July20...
BCG-Executive-Perspectives-CEOs-Guide-to-Maximizing-Value-from-AI-EP0-3July20...
benediktnetzer1
 
Measurecamp Copenhagen - Consent Context
Measurecamp Copenhagen - Consent Context
Human37
 
LECTURE_skakkakwowowkwkkwkskwkqowoowoaoaoa.cooos
LECTURE_skakkakwowowkwkkwkskwkqowoowoaoaoa.cooos
ssuseraf13da
 
UPS and Big Data intro to Business Analytics.pptx
UPS and Big Data intro to Business Analytics.pptx
sanjum5582
 
REGRESSION DIAGNOSTIC II: HETEROSCEDASTICITY
REGRESSION DIAGNOSTIC II: HETEROSCEDASTICITY
Ameya Patekar
 
Data-Driven-Operational--Excellence.pptx
Data-Driven-Operational--Excellence.pptx
NiwanthaThilanjanaGa
 
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
taqyea
 
Power BI API Connectors - Best Practices for Scalable Data Connections
Power BI API Connectors - Best Practices for Scalable Data Connections
Vidicorp Ltd
 

SQL Pass Summit Presentations from Datavail - Optimize SQL Server: Query Tuning Techniques

  • 1. Query tuning techniques Rajnikant Tandel, DBA Manager, Datavail Anup Gopinathan, Sr. SQL Server DBA, Datavail Optimize SQL Server
  • 3. Free online webinar events Free 1-day local training events Local user groups around the world Online special interest user groups Business analytics training Get involved Explore everything PASS has to offer Free Online Resources Newsletters PASS.org
  • 4. Download the GuideBook App and search: PASS Summit 2018 Follow the QR code link displayed on session signage throughout the conference venue and in the program guide Session evaluations Your feedback is important and valuable. Go to passSummit.com 3 Ways to Access: Submit by 5pm Friday, November 16th to win prizes.
  • 5. Anup Gopinathan Sr. SQL Server DBA, Datavail Sr. DBA with over 11 years of experience in database design, development & administration and cloud computing. Extensive customer engagement experience, supported and worked with various customers. Database performance tuning Query tuning, execution plan analysis, index and statistics optimization. Database Scalability Table partitioning, analyze isolation and locking mechanisms, load balancing /anupgopinathannair @GopinathanAnup Powershell scripting and automations Cloud Data Architecture/Cloud Migration Design highly available and scalable database systems in cloud, migrate database from On- premises to cloud
  • 6. Rajnikant Tandel DBA Manager I’m a Microsoft Certified Solution Associate, SQL Server 2012/2014 and have over 15 years of SQL Server DBA experience on relational databases and application development and specialize in performance tuning, scalability and maintainability of large scale SQL Servers. I’ve worked extensively on performance tuning and query optimization projects. /rajnikant-tandel @tandelrajni Background • Programmer • Database Developer/DBA • Database Administrator • DBA Manager
  • 7. Datavail Overview Databases, Analytics, and Application Data Integration 13+ years delivering data services 300+ customers with average client retention of 7 years Managed services, projects, and staffing • All major platforms: SQL, Oracle, DB2, MySQL, Mongo DB • Comprehensive development & operational services • US & Global models • Reactive and proactive services Database Operations • Microsoft SSRS, SSIS, SSAS • PowerBI • Oracle OBIEE, BICS, Golden Gate • EBS • Hyperion/Essbase • Microsoft Dynamics • SharePoint • Custom development • Microsoft .NET • Oracle APEX Analytics, Visualization & Integration Application Management We are data specialists • Informatica • Oracle Data Integrator • ETL development and operational run
  • 8. Win Shoes Fill out the session survey for a chance to win an exclusive pair of Datavail’s running shoes!
  • 9. • T-SQL new functions and performance – How to use SQL Server 2017 new string functions to improve performance? • Lock Escalation – How? When? Why? • Table Variable V/S Temporary table V/S Correlated Subquery – compare performance. • Parameter sniffing – a look into how performance is affected. Agenda
  • 10. T-SQL new functions and performance Example: select category_code, STRING_AGG(category_desc, ', ') within group (order by category_desc) categories from category group by category_code category_code categories 1 Groceries, Lodging, Meals, Travel 2 Clothing, Communication, Electronics, Entertainment, Home Supplies, Misc STRING_AGG() Concatenates the values of string expressions and places separator values between them. The separator is not added at the end of string.
  • 12. T-SQL new functions and performance TRANSLATE() Returns the string provided as a first argument after some characters specified in the second argument are translated into a destination set of characters. TRANSLATE (inputstring, characters, translations) Example: SELECT TRANSLATE('2*[3+4]/{7-2}', '[]{}', '()()’); Output: 2*(3+4)/(7-2)
  • 14. T-SQL new functions and performance Summary • STRING_AGG() or TRANSLATE() perform better than user defined function or other T-SQL methods • Use CAST() when get an error “STRING_AGG aggregation result exceeded the limit of 8000 bytes.” • Consider replacing user defined function with new T-SQL functions
  • 16. Lock Escalation The process of acquiring higher level lock instead of many smaller locks is called lock escalation.
  • 17. How does escalation happen? 1250 5000 Row locks Page Lock Table lock
  • 18. Can we disable the lock escalation? Table hints • Row lock • Page lock ALTER TABLE CHARGE SET (LOCK_ESCALATION = TABLE | DISABLE | AUTO) • TABLE • DISABLE • AUTO There are some options available to control it.
  • 20. Summary • Lock escalation can be controlled by using table hints and by disabling escalation at table level. • Understand the pros and cons of each method. • SQL Engine can still escalate • “TEST”
  • 22. Table Variable vs Temp Table vs Correlated Subquery Table Variable • Can only be accessed within the batch and scope in which they are declared • Stored in TempDB • Because of the well-defined scope, a table variable generally uses fewer resources than a temporary table. • No statistics-based recompiles • Dropped automatically DECLARE @Totalcharge AS TABLE (Charge_no INT, Amount money, Date_Inserted datetime) There’s comfort in knowing that everything is temporary.
  • 23. Table Variable vs Temp Table vs Correlated Subquery Temporary Table • Stored in TempDB • Can create Indexes • Deleting a Temporary Table • Automatically when connection is closed • Manually using DROP Table statement • Performs better with large number of rows create table #Totalcharge (Charge_no INT, Amount money, Date_Inserted datetime)
  • 24. Table Variable vs Temp Table vs Correlated Subquery Correlated Subquery • A correlated subquery is a SELECT statement nested inside another T-SQL statement, which contains a reference to one or more columns in the outer query. • The correlated subquery will be run once for each candidate row selected by the outer query. SELECT column1, column2, .... FROM table1 outer WHERE column1 operator (SELECT column1, column2 FROM table2 WHERE expr1 = outer.expr2);
  • 25. Table Variable vs Temp Table vs Correlated Subquery
  • 26. Table Variable vs Temp Table vs Correlated Subquery Summary • Table variable performs well for smaller set of rows • Low maintenance cost • No statistics-based recompiles • Temp Table performs better for larger result set • Can Create Indexes and Statistics • Correlated subquery can be inefficient because it evaluated once for each row processed by the outer query
  • 28. Parameter Sniffing The execution plan for stored procedures does not work well for some arguments.
  • 29. Is this a bug? Parameter sniffing is not a bug
  • 31. Summary • Recompile • OPTION (OPTIMIZE FOR (@VARIABLE=VALUE | UNKNOWN)) • Disable parameter sniffing using Trace Flag 4136 / Database Scoped Configuration • local variableIt is not a bug. • Analyze your workloads • Testing
  • 33. References • www.mssqltips.com • sqlmag.com • sqlservercentral • stackoverflow.com • Sqlserverperformance • Statisticsparser.com
  • 34. Thank You Rajnikant Tandel @tandelrajni [email protected] Anup Gopinathan @GopinathanAnup [email protected] Learn more from Learn more from
  • 35. Win Shoes Fill out the session survey for a chance to win an exclusive pair of Datavail’s running shoes!