1
SQL Server Query execution.
Why my query is slow?
Konstantynov Valerii
Software engineer
2
Agenda
1. How to analyze query?
1. When a problem can be not in my query.
1. Parameter Sniffing
1. Convert Implicit.
33
How to analyze query?
• STATISTICS TIME
• STATISTICS IO
• Execution Plan
• WAITS
• SSMS + SQL Profiler
44
When a problem it not in my query?
Low CPU time - High Duration
5
LCK_M_…. WAITS
• Occurs when a task is waiting to acquire a lock.
• Example:
- Transaction 1:
- Transaction 2:
66
Demo
LCK_M_…. WAITS
7
ASYNC_NETWORK_IO WAIT
• Occurs on network writes when the task is blocked behind the
network. Verify that the client is processing data from the server.
• Example:
- Problem with network connection between the server and the client.
- Slow processing in the application.
88
Demo
ASYNC_NETWORK_IO WAIT
99
Parameter Sniffing
10
Query
Query
Plan
Cache
Execution
Optimizer
Parameter Sniffing
Query Execution
11
Parameter Sniffing
SELECT * FROM Customer c
WHERE c.EmailAddress = 'petr@gmail.com'
Execution Plan
SELECT * FROM Customer c
WHERE c.EmailAddress = 'ivan@gmail.com' Execution Plan
12
Parameter Sniffing
EXEC GetCustomers
@email='petr@gmail.com'
Execution Plan
EXEC GetCustomers
@email='ivan@gmail.com'
1313
Demo
Parameter Sniffing
1414
Slow in the Application, Fast in SSMS?
1515
The Default Session Settings
1616
Convert Implicit
Demo
1717
Data Type Precedence
1. user-defined data types (highest)
2. sql_varian t
3. xml
4. datetimeoffset
5. datetime2
6. datetime
7. smalldatetime
8. date
9. time
10. float
11. real
12. decimal
13. money
14. smallmoney
15. bigint
16. int
17. smallint
18. tinyint
19. bit
20. ntext
21. text
22. image
23. timestamp
24. uniqueidentifier
25. nvarchar (including nvarchar(max) )
26. nchar
27. varchar (including varchar(max) )
28. char
29. varbinary (including varbinary(max) )
30. binary (lowest)
18
Thank you

SQL Server Query execution. Why my query is slow?