T-SQL PERFORMANCE IMPROVEMENTNGUYEN PHAN DZUNG
APRIL 2016
AGENDA- Objectives
- Contents:• Execution plan• DMV• SQL Profiler• Parameter sniffing• Table partitioning
- Q&A- References
Security Classification: Internal
Objectives
3
• Get the idea of using execution plan • Get the idea of using DMVs• Be able to use SQL profilers• Understand parameter sniffing and how to
avoid• Understand table partitioning in SQL Server
Execution plan
5Security Classification: Internal
Execution plan – What is execution plan?
An execution plan is the result of the query optimizer's attempt to calculate the most efficient way to imple ment the request represented by the T-SQL query you sub mitted
6Security Classification: Internal
Execution plan – When it happens?
7Security Classification: Internal
Execution plan – Components
DEMO
Database management views, a.k.a DMV
10Security Classification: Internal
DMV
• Views built on top of internal structures• Ideal for tracking performance
Server level Component leveldm_exec_*
Execution of user code and associated connections
dm_os_* Memory, locking & scheduling
dm_tran_* Transactions & isolation
dm_io_* I/O on network and disks
dm_db_* Databases and database object
dm_repl_* Replication
dm_broker_* SQL Service Broker
dm_fts_* Full Text Search
dm_qn_* Query Notifications
dm_clr_* Common Language Runtime
DEMO
Using SQL Profiler
13Security Classification: Internal
SQL Profiler
• Profiler is a GUI based tool that runs a SQL Server trace to capture the metrics (duration, number of reads, number of writes, the machine that ran the query, etc...)
• A Profiler trace can consume a significant amount of network bandwidth
DEMO
Parameter sniffing
16Security Classification: Internal
Parameter sniffing
Parameter sniffing is the process whereby SQL Server creates an optimal plan for a stored procedure by using the calling parameters that are passed the first time a stored procedure is executed.
17Security Classification: Internal
Parameter sniffing - Solutions
• Create SQL Server Stored Procedures using the WITH RECOMPILE Option
• Use the SQL Server Hint OPTION (RECOMPILE)• Use the SQL Server Hint OPTION (OPTIMIZE FOR)• Use local variables on SQL Server Stored Procedures• Disable SQL Server Parameter Sniffing at the Instance
Level• Disable Parameter Sniffing for a Specific SQL Server
Query (adding the QUERYTRACEON hint to the OPTION clause)
DEMO
Table partitioning
20Security Classification: Internal
Table partitioning– What?
Table partitioning is a way to divide a large table into smaller, more manageable parts without having to create separate tables for each part
21Security Classification: Internal
Table partitioning– Partition column
Data in a partitioned table is partitioned based on a single column, the partition column, often called the partition key. Only one column can be used as the partition column, but it is possible to use a computed column.
22Security Classification: Internal
Table partitioning– Partition function
The partition function defines how to partition data based on the partition column. The partition function does not explicitly define the partitions and which rows are placed in each partition. Instead, the partition function specifies boundary values, the points between partitions.
Partition functions are created as either range left or range right to specify whether the boundary values belong to their left or right partitions:• Range left means that the actual boundary value
belongs to its left partition, it is the last value in the left partition.
• Range right means that the actual boundary value belongs to its right partition, it is the first value in the right partition.
23Security Classification: Internal
Table partitioning– Partition scheme
The partition scheme maps the logical partitions to physical filegroups. It is possible to map each partition to its own filegroup or all partitions to one filegroup.
24Security Classification: Internal
Table partitioning– Split & merge
25Security Classification: Internal
Table partitioning– Partition switching
Inserts, updates and deletes on large tables can be very slow and expensive, cause locking and blocking, and even fill up the transaction log. One of the main benefits of table partitioning is that you can speed up loading and archiving of data by using partition switching.
DEMO
Q & A
Security Classification: Internal
References
Big data and Hadoop introduction 28
1.SQL Server 2008 Query Performance Tuning Distilled2.SQL Server 2012 Query Performance Tuning, 3rd Edition3.SQL Server 2012 T-SQL Recipes, 3rd Edition4.Expert Performance Indexing for SQL Server 20125.https://simple-talk.com6.https://sqlservercentral.com7.https://www.brentozar.com8.http://www.cathrinewilhelmsen.net/9.http://www.sqlshack.com/
Thank you for your attention!
Top Related