T-SQL performance improvement - session 2 - Owned copy

29
T-SQL PERFORMANCE IMPROVEMENT NGUYEN PHAN DZUNG APRIL 2016

Transcript of T-SQL performance improvement - session 2 - Owned copy

Page 1: T-SQL performance improvement - session 2 - Owned copy

T-SQL PERFORMANCE IMPROVEMENTNGUYEN PHAN DZUNG

APRIL 2016

Page 2: T-SQL performance improvement - session 2 - Owned copy

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

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

Execution plan

Page 5: T-SQL performance improvement - session 2 - Owned copy

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

6Security Classification: Internal

Execution plan – When it happens?

Page 7: T-SQL performance improvement - session 2 - Owned copy

7Security Classification: Internal

Execution plan – Components

Page 8: T-SQL performance improvement - session 2 - Owned copy

DEMO

Page 9: T-SQL performance improvement - session 2 - Owned copy

Database management views, a.k.a DMV

Page 10: T-SQL performance improvement - session 2 - Owned copy

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

DEMO

Page 12: T-SQL performance improvement - session 2 - Owned copy

Using SQL Profiler

Page 13: T-SQL performance improvement - session 2 - Owned copy

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

DEMO

Page 15: T-SQL performance improvement - session 2 - Owned copy

Parameter sniffing

Page 16: T-SQL performance improvement - session 2 - Owned copy

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

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

DEMO

Page 19: T-SQL performance improvement - session 2 - Owned copy

Table partitioning

Page 20: T-SQL performance improvement - session 2 - Owned copy

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

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

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

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

24Security Classification: Internal

Table partitioning– Split & merge

Page 25: T-SQL performance improvement - session 2 - Owned copy

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

DEMO

Page 27: T-SQL performance improvement - session 2 - Owned copy

Q & A

Page 28: T-SQL performance improvement - session 2 - Owned copy

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

Thank you for your attention!