T-SQL performance improvement - session 2 - Owned copy
-
Upload
dzung-nguyen -
Category
Documents
-
view
154 -
download
0
Transcript of T-SQL performance improvement - session 2 - Owned copy
![Page 1: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/1.jpg)
T-SQL PERFORMANCE IMPROVEMENTNGUYEN PHAN DZUNG
APRIL 2016
![Page 2: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/2.jpg)
AGENDA- Objectives
- Contents:• Execution plan• DMV• SQL Profiler• Parameter sniffing• Table partitioning
- Q&A- References
![Page 3: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/3.jpg)
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
![Page 4: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/4.jpg)
Execution plan
![Page 5: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/5.jpg)
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
![Page 6: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/6.jpg)
6Security Classification: Internal
Execution plan – When it happens?
![Page 7: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/7.jpg)
7Security Classification: Internal
Execution plan – Components
![Page 8: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/8.jpg)
DEMO
![Page 9: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/9.jpg)
Database management views, a.k.a DMV
![Page 10: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/10.jpg)
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
![Page 11: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/11.jpg)
DEMO
![Page 12: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/12.jpg)
Using SQL Profiler
![Page 13: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/13.jpg)
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
![Page 14: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/14.jpg)
DEMO
![Page 15: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/15.jpg)
Parameter sniffing
![Page 16: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/16.jpg)
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.
![Page 17: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/17.jpg)
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)
![Page 18: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/18.jpg)
DEMO
![Page 19: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/19.jpg)
Table partitioning
![Page 20: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/20.jpg)
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
![Page 21: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/21.jpg)
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.
![Page 22: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/22.jpg)
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.
![Page 23: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/23.jpg)
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.
![Page 24: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/24.jpg)
24Security Classification: Internal
Table partitioning– Split & merge
![Page 25: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/25.jpg)
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.
![Page 26: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/26.jpg)
DEMO
![Page 27: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/27.jpg)
Q & A
![Page 28: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/28.jpg)
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/
![Page 29: T-SQL performance improvement - session 2 - Owned copy](https://reader030.fdocuments.in/reader030/viewer/2022033106/587600641a28ab4a508b56f9/html5/thumbnails/29.jpg)
Thank you for your attention!