You can watch the replay for this Geek Sync webcast, Performance Tune Like an MVP, in the IDERA Resource Center, http://ow.ly/aDE250A4qdF.
The life of a DBA is evolving and your tuning skills should always be sharp. Tuning is one of the key components of a great DBA and developer. In this demo rich session we'll deep dive into performance tuning for on-premises, PaaS (platform as a service), and IaaS (infrastructure as a service). We'll discuss tips and techniques for troubleshooting bottlenecks and how to remediate them for hardware, OS, and the database.
Speaker: Daniel Janik has been supporting SQL Server for over 18 years. Six of those years were at Microsoft Corporation supporting SQL Server as a Senior Premier Field Engineer (PFE) where he supported over 287 different clients with both reactive and proactive database needs. Daniel has presented at many community events and SQL Saturdays.
2. • Microsoft Field Engineer (PFE) for 6 years
• Working with SQL since 6.5 in ~1998
• @SQLSME, Daniel Janik on LinkedIn, Daniel@SQLDataPros.com
www.SQLTechBlog.com
About Me
3. About this Webinar
• We’ll discuss performance tuning on a mix of
On-Prem SQL Server and Azure data platform
solutions.
5. SQL Server’s #1 Bottleneck: Disk Latency
• Caused by a bottleneck in the disk or fabric
– Disk is the physical spindle where the data is stored.
– Fabric is the communication path from the application to
the physical disk (HBA, switch, Fiber Adapter ports, …)
• Can be observed from
– DMVs (Dynamic Management Views)
– Windows tools (Perfmon, Resmon, Storport, …)
– SAN tools
– Azure Portal
9. PaaS DMVs
sys.dm_db_resource_stats (per db)
– Collects every 15 seconds
– Stored for 1 hour
– Shows resource utilization
sys.resource_stats (in master)
– Collects every 5 minutes
10. Storage Limitations in Azure
Small databases
may require a lot of
storage depending
on IOP and
throughput needs
https://docs.microsoft.com/en-us/azure/sql-database/sql-database-managed-
instance-resource-limits#file-io-characteristics-in-general-purpose-tier
12. RAM Considerations in Azure
RAM depends on
VCORE count
(adding more VCORES
increases RAM).
4 VCORES may be
enough compute but
20 GB RAM for a 500
GB db may be too little
https://azure.microsoft.com/en-us/pricing/details/sql-database/managed/
13. Troubleshooting Flow
O/S Issue?
Taskman
Resmon
SQL Issue?
Research Error
Message
sys.messages
Storage Processor
Memory Network
Blocking?
Exec_requests
ISSUE
?
Specific
Error?
General
Slowness?
Single
Query?
14. Viewing Blocking by Using DMVs
• sys.dm_tran_locks
– Replaces syslockinfo and sp_lock
– Each row has information about both the resource and the request
– request_status = WAIT implies blocking
• sys.dm_os_waiting_tasks
– blocking_session_id > 0
• sys.dm_exec_requests
– status = suspended
– blocking_session_id > 0
• sys.dm_exec_sessions
– Join with each of the above for session details (join on session_id)
18. Troubleshooting Flow
O/S Issue?
Taskman
Resmon
SQL Issue?
Research Error
Message
sys.messages
Storage Processor
Memory Network
Blocking?
Exec_requests
ISSUE
?
Specific
Error?
General
Slowness?
Single
Query?
Query Stats
19. Query Statistics
• SQL 2005+
– sys.dm_exec_query_stats
– sys.dm_exec_trigger_stats
– sys.dm_exec_procedure_stats
• SQL 2014+
– sys.dm_exec_query_profiles
• Requires use of SET STATISTICS PROFILE ON;
• Monitors real time query progress while the query is in execution. For example, use
this DMV to determine which part of the query is running slow.
• SQL 2016+
– sys.dm_exec_function_stats
19
20. Query Optimizer
• The query optimizer
– attempts to find the fastest plan
– validates the query
– parses a query into tree representation
– evaluates possible query plans until a ‘good enough’ plan is found
– can abort optimization due to timeout
– sometimes makes a poor choice
25. Query Plan Myths
• Estimated plans give me all the facts
• Estimated cost is in “seconds”
• Estimated cost changes if the data is in cache
or if I have faster storage
• Costs are always accurate
26. Query Optimizer IO cost
• Query Plan IO costs
– Random IO = 0.003125
– Sequential IO = 0.000740741
28. Where to start
• Estimated plan
• Graphical plan
• Seeks vs Scans
• Key or RID lookups
• High cost operators
29. Noteworthy investigation
Look for
• Missing Indexes
• Estimated row count vs actual row count
• Required Memory vs Desired Memory
• Warnings
• Predicate Pushes (Probe Residual, Filter, …)
• Spools, Sorts, and other ‘blocking’ operators
30. ‘Bad’ Plans & Query Performance
• Stats
• Be careful using trace flag 2371
• Ascending Stats
• “@param = is null OR column = @param”
31. Memory – Runtime data
Example of large grant
<MemoryGrantInfo SerialRequiredMemory="46720"
SerialDesiredMemory="9198680"
RequiredMemory="104880"
DesiredMemory="9256848"
RequestedMemory="5133928" GrantWaitTime="0"
GrantedMemory="5133928"
MaxUsedMemory="322736" />
Example of large grant
<MemoryGrantInfo SerialRequiredMemory="46720"
SerialDesiredMemory="9198680"
RequiredMemory="104880"
DesiredMemory="9,256,848 KB" (9.2 GB!!!)
RequestedMemory="5133928" GrantWaitTime="0"
GrantedMemory="5,133,928 KB" (5.1 GB!!!)
MaxUsedMemory="322,736 KB" /> (322 MB used)
32. Adaptive Query Plans and IQP
• Microsoft has done a lot of work to help with
these memory grant issues. For more info
refer to the following:
• https://docs.microsoft.com/en-us/sql/relational-
databases/performance/intelligent-query-
processing?view=sql-server-ver15
33. SARGability
• SARG = Search Argument (aka Predicate)
• SARGable expressions can utilize and index
• Non-SARGable expressions force scans and
can kill performance
34. Query Operators
• Describe how SQL Server executes the query
• Blocking operators such as Sort, Hash, Spool, & Filter reduce
concurrency
– Data must be fully retrieved before an output is produced
37. Spool
• Blocking operator
• Query Optimizer usage
• Halloween protection
• Dumps data from another operator into a
worktable in TempDB.
38. Data type conversion
• Often serious bottleneck
• Can be caused by joining two columns
that do not match data types in a join
or union
• Demo
39. Performance via Constraints
• Trusted constraints can help eliminate joins
• UNIQUE constraint can allow DISTINCT to be
ignored
40. Troubleshooting Flow
O/S Issue?
Taskman
Resmon
SQL Issue?
Research Error
Message
sys.messages
Storage Processor
Memory Network
Blocking?
Exec_requests
Query Stats
ISSUE
?
Specific
Error?
General
Slowness?
Single
Query?
Wait Stats
41. Wait Statistics
• SQL 2005+
– sys.dm_os_wait_stats
• SQL 2016+
– sys.dm_exec_session_wait_stats
• Returns information about all the waits encountered
by threads that executed for each session.
41
42. SQL Server Wait Types
• Typical types seen using the SYS.DM_OS_WAIT_STATS DMV
• Waiting for I/Os to
complete
• Generally not data
pages
• Often associated with
file growth or other
non-read/write
operations
• Waiting for a log flush
operation to complete
• Generally due to disk
latency, although high
activity can contribute
• Waiting for a buffer
(data page) that is in
an I/O operation
• Generally due to disk
latency, although high
I/O queries and lack
of buffer space can
contribute
WRITELOG PAGEIOLATCH_xxASYNC_IO_COMPLETION
IO_COMPLETION