Chapter 9: Monitoring and Performance Course 157: SQL Server 2008 Database Administration Presented by Scott Whigham... • Chapter 9 is about performance and exception monitoring your se
Trang 2Chapter 9: Monitoring and
Performance
Course 157: SQL Server 2008 Database Administration
Presented by Scott Whigham
Trang 3• PerfMon
• Data Collector
What We’re Going to Cover
Trang 4• Chapter 9 is about performance and
exception monitoring your server
• We’ll start with the basics (using Profiler) and end with new features in SQL Server
2008 (Data Collector)
Chapter Introduction
Trang 5• PerfMon
• Data Collector
What We’re Going to Cover
Trang 6• There are many built-in tools to monitor
performance and health:
– Activity Monitor and sp_who
• Covered in Chapter 5!
– SQL Server Profiler
• Detailed view of statements issued against instance
– Dynamic Management Views (DMVs)
• “Live” view of server health and activity
– Performance counters and objects (“PerfMon”)
• “Live” view of server health and activity
– Data Collector
Tools of Performance Monitoring
Trang 7Profiler DMVs Perf
Mon
Data Coll.
Provides “live”, real-time reporting?
Can do trend analysis and historical reporting? Requires
manual load
Requires manual load
Requires manual load
Can view SQL issued?
Provides detail-level view or summary? Detail Depends… Summary Summary
Can be used to diagnose performance problems?
Tools of Performance Monitoring
“But Mr Scott, how do I know when to use each one?”
Trang 8• Let’s play with Profiler!
In the next video…
Trang 9Chapter 9: Monitoring and
Performance
Course 157: SQL Server 2008 Database Administration
Presented by Scott Whigham
Trang 10• PerfMon
• Data Collector
What We’re Going to Cover
Trang 11• SQL Server Profiler is a tool for
monitoring real-time queries issued
against your server
– “Old Faithful” - been around for years
– Requires sysadmin or ALTER TRACE server
permission
SQL Server Profiler
Trang 12SQL Server Profiler
Trang 13• DBAs use traces to monitor statements
Trang 14• SQL Server Profiler is the GUI for
defining, viewing, and replaying traces
– It does not have to be running to create a trace
– Many DBAs create their traces via Profiler and
then script out to jobs
SQL Server Profiler
Trang 15• Running Profiler on a “live” system adds
to an already stressful situation
– Particularly if run interactively on the server
• If you must run Profiler against a “live”
server, run it remotely
• Adds network overhead but this is almost never an issue today
SQL Server Profiler
Trang 16• Trace data can be stored:
– In a trc file (fastest)
– In a SQL Server table
• trc files can be configured to rollover to
a new file after a certain threshold
• Can load trc file contents into SQL Server table after trace for maximum flexibility!
SQL Server Profiler
Trang 17• After trace is complete, you can export
results
SQL Server Profiler
Trang 18• Tips on working with Profiler
– Traces can have filters to help minimize the noise
• The better you filter, the less of an impact you put on the server
– Being able to write queries against the trace
results can help you identify many common trends and problems
• This requires you load the trace data into a table!
SQL Server Profiler
Trang 19• Let’s play with Profiler!
In the next video…
Trang 20Chapter 9: Monitoring and
Performance
Course 157: SQL Server 2008 Database Administration
Presented by Scott Whigham
Trang 21• PerfMon
• Data Collector
What We’re Going to Cover
Trang 22• DBAs use Dynamic Management Views
(“DMVs”) and Dynamic Management
Functions (“DMFs”) to peek under the
covers
– In the sys schema and begin with dm_
– Used to get up-to-the-second performance
information during critical time
– Often need to JOIN many together to come up
Dynamic Management Views
Trang 23Profiler DMVs Perf
Mon
Data Coll.
Provides “live”, real-time reporting?
Can do trend analysis and historical reporting? Requires
manual load
Requires manual load
Requires manual load
Can view SQL issued?
Provides detail-level view or summary? Detail Depends… Summary Summary
Can be used to diagnose performance problems?
Tools of Performance Monitoring
Reminder…
Trang 24• There are too many DMxs to cover each
one so we’ll cover concepts and core
DMxs
– Over 130 DMVs and DMFs in SQL Server 2008!
• It is important for DBAs to maintain a
script library
Dynamic Management Views
Trang 25• The easiest way to get to know the
Trang 26• Intellisense can help you greatly!
Dynamic Management Views
Trang 27• Object Explorer can also help
Dynamic Management Views
Trang 28• You can also query system views for full
Trang 29DMV and DMF Query Results
Trang 31• We’ll take a look at a few categories and examples
In the next video…
Trang 32Chapter 9: Monitoring and
Performance
Course 157: SQL Server 2008 Database Administration
Presented by Scott Whigham
Trang 33• PerfMon
• Data Collector
What We’re Going to Cover
Trang 34• Reminder: the more popular categories
for performance monitoring
Dynamic Management Views
Trang 36• From a performance standpoint, you will spend a lot of time in the Database
Trang 37• Common index-related DMVs in the
Database Category
– sys.dm_db_missing_index_details
returns information about indexes that could
potentially help speed up the system
you whether an index is being used and, if so, in
what way
Database Category
Trang 38• Common index-related DMFs in the
Trang 39• We’ll take a look at writing queries using the DMVs and DMFs in the Database
category
In the next video…
Trang 40Chapter 9: Monitoring and
Performance
Course 157: SQL Server 2008 Database Administration
Presented by Scott Whigham
Trang 41• PerfMon
• Data Collector
What We’re Going to Cover
Trang 42• Let’s look at the other categories now…
Dynamic Management Views
Trang 44• The Execution/Session category will help you spy on people
– That’s what you really want to do, isn’t it?
– Can also view specific query information
• Plans, text, stats, caching information
Execution/Session Category
Trang 45• There is at least one DMF in this category that you definitely want to know
– sys.dm_exec_sql_text allows you to
see the query text a user submits
• By itself, it is not that useful but combined with other Execution/Session DMVs it is very powerful
• Accepts one parameter - the memory location of the query plan -
Execution/Session Category
Trang 46• A common DMV in this category is:
metrics about queries submitted to server
• Reads, writes, waits, execution count, etc
• plan_handle column is the column to pass to
sys.dm_exec_sql_text to return SQL
Execution/Session Category
Trang 47SELECT TOP(50) sql.text
Trang 48Return 50 most-run queries
Trang 49• Other common DMVs in this category are
info
the cached query plans
• Is it compiled or adhoc?
• How many times has it been used?
Execution/Session Category
Trang 50• Spying on users is easy!
authenticated session
Returns all non-internal sessions
SELECT * FROM sys.dm_exec_sessions
WHERE session_id>50
Execution/Session Category
Trang 51• We’ll write several queries and see the
Execution/Session category in action
In the next video…
Trang 52Chapter 9: Monitoring and
Performance
Course 157: SQL Server 2008 Database Administration
Presented by Scott Whigham
Trang 53• PerfMon
• Data Collector
What We’re Going to Cover
Trang 54• Let’s look at the IO and OS categories
now to help diagnose disk pressure
Dynamic Management Views
Trang 56• The most common item in this category
is
– sys.dm_io_virtual_file_stats()
returns information about usage of the database
files
• Reads, writes, IO waits, size
• Accepts two parameters:
– database_id - use NULL for all databases – file_id - the file_id from sys.database_files; use NULL for
IO Category
Trang 57• sys.dm_io_virtual_file_stats
returns several columns:
IO Category
Column Description
sample_ms Milliseconds since server was restarted
num_of_reads Physical reads of objects in database file
num_of_bytes_read Large textual-based columns will affect this greatly
io_stall_read_ms Total time a user process waited for IO to this file
num_of_writes,
num_of_bytes_written,
Database write activity
Trang 58• Here are some things to monitor with
dm_io_virtual_file_stats()
– Look at total reads+writes to find “most active”
– Look at io_stall to identify files that might
benefit from new disks
• All databases have stalls; the important metric is “What percentage of the uptime of the server am I having
stalls?”
• Compare read/write stalls on log and data file to
IO Category
Trang 59View IO stats for all DBs on instance:
Trang 60IO DMF
Trang 61Aggregate total IO since last restart:
GROUP BY vfs.database_id
ORDER BY IO_MB DESC
IO DMF
Trang 62• The OS category is one of the largest
Trang 63• You can use this category to find waiting tasks:
dozens and dozens of wait types
waits for a specific resource
• Hint: use dm_os_wait_stats to identify the areas of contention and then dm_os_waiting_tasks to view
OS Category
Trang 64• These two DMVs are accumulative
– Their statistics are the totals since SQL Server
service has been started
• To track effectively over time, it is
necessary to reset periodically
– Common to store in a table for historical
perspective
– DBCC SQLPERF(‘<dmv_name>’, CLEAR)
OS Category
Trang 65Current wait info:
SELECT wait_type, wait_time_ms / 1000 as wait_time_s
FROM sys.dm_os_wait_stats
ORDER BY wait_time_ms DESC
Clear out stats:
DBCC SQLPERF ('sys.dm_os_wait_stats', CLEAR)
Run this at a later time to view waits since CLEAR
SELECT wait_type, wait_time_ms / 1000 as wait_time_s
FROM sys.dm_os_wait_stats
OS Category
Trang 66OS Category
Trang 67• To identify disk pressure, you can look in
sys.dm_os_wait_stats for specific wait
types
– Order by wait_time_ms DESC to identify most often waits
– If anyone of these are in the top two positions, you
have disk I/O pressure:
Trang 68• Let’s look further at the OS category and others
In the next video…
Trang 69Chapter 9: Monitoring and
Performance
Course 157: SQL Server 2008 Database Administration
Presented by Scott Whigham
Trang 70• PerfMon
• Data Collector
What We’re Going to Cover
Trang 71• The OS category has lots of info
of CPUs, start time of service
loaded DLL that SQL Server is using
• Cryptography Manager
• Kerberos Security Package
• Microsoft SQL Server Native Client 10.0
OS Category
Trang 72• Many OS category DMVs can help
diagnose memory pressure
– Performance Counters can help too
– We’ll cover Perf Counters in an upcoming section
of this chapter
OS Category
Trang 73• The main areas for memory pressure are:
– Database page cache (used to cache table/index)
– Query Workspace memory (for Hash and Sort ops)– Plan cache
– There are others…
OS Category
Trang 74Find out what has the most entries
Trang 75OS Category
Trang 76Top 10 consumers of Buffer Pool
SELECT TOP 10 [type]
Trang 77OS Category
Trang 78• One area that many servers have trouble
with is the TokenAndPermUserStore cache
– An internal security system that generates security
tokens for sessions
– Particularly troublesome area for DBs with lots of
ad-hoc SQL
– The problem can be that the
TokenAndPermUserStore cache takes up so
much memory and is so frequently accessed that
when there are a high number of items in the cache,
access takes a long time
OS Category
Trang 791 Queries for diagnosing
issues:
What is the size? The larger, the slower!
SELECT SUM(single_pages_kb + multi_pages_kb)
AS ‘CurrentSizeOfTokenCache(kb)’
FROM sys.dm_os_memory_clerks
OS Category
Trang 802 Queries for diagnosing
Trang 81Scary!
Trang 82• There are multiple ways to prevent
• access check bucket count
• access check cache quota
•
OS Category
Trang 83• There is one way to fix a “live”
– Free the system cache
WARNING: Do not use on production without
a full understanding!
After running, there will not be an entry
in sys.dm_os_memory_clerks for TAPUS!
DBCC FREESYSTEMCACHE('TokenAndPermUserStore')
OS Category
Trang 84• Let’s look at blocking and locking…
In the next video…
Trang 85Chapter 9: Monitoring and
Performance
Course 157: SQL Server 2008 Database Administration
Presented by Scott Whigham
Trang 86• PerfMon
• Data Collector
What We’re Going to Cover
Trang 87• To maximize coverage, you want to use
both DMVs/DMFs along with
Performance Monitor
– a.k.a PerfMon and System Monitor
• PerfMon gathers stats about SQL Server
as well as the rest of the environment
– Disk subsystems, non-SQL resource usage
Performance Monitor
Trang 88• PerfMon uses objects and counters
– Objects are for categorization
– Objects contain one or more counters
• When you install SQL Server, it installs a
set of instance-specific objects
Performance Monitor
Trang 89• You can view the SQL Server objects and counters using the DMV
sys.dm_os_performance_counters
– Careful - more than 1,100 rows
Performance Monitor
Trang 90• A few things to know about
sys.dm_os_performance_counters
– Only displays SQL Server instance’s counters
– Any counter that has “/sec” in the name is cumulative since time SQL Server service was started
• Transactions/sec
• Bulk copy rows/sec
– To accurately read these values, compare
intervals
• Compare 0800 reading to 0900 reading, for
Performance Monitor
Trang 91• Example query to return all
deprecated features usage
– Many feature usages are actually SQL Server internals and tools!
Trang 92Performance Monitor
Trang 93• Querying for specific counters allows
ad-hoc views of point-in-time information
– But no graphs, charts, pretty colors
• For trend analysis, you have a few
choices:
– Store the query results in a table for later review
– Use PerfMon to store logs for later analysis
Performance Monitor
Trang 94• Some DBAs will store certain values in a
table for later querying or for historical
perspective
– Watch the rest of the chapter prior to attempting
this!
Create a new table (”PerfCounters”)
and store current counters
SELECT *
INTO PerfCounters
FROM sys.dm_os_performance_counters
Performance Monitor
Trang 95• All DBAs will use PerfMon
Performance Monitor
Trang 96• Let’s take a look at specific objects and
counters
In the next video…
Trang 97Chapter 9: Monitoring and
Performance
Course 157: SQL Server 2008 Database Administration
Presented by Scott Whigham
Trang 98• PerfMon
• Data Collector
What We’re Going to Cover
Trang 99• There are a few specific areas you will
want to monitor
– Sometimes you can find that “Aha!” moment
using ad-hoc monitoring
– Sometimes you can only find that “Aha!” moment using trend analysis
Performance Monitor
Trang 100• A few topics before we go further:
– PerfMon, counters with “/sec” in the name are
not cumulative
• Makes it easy to do ad-hoc monitoring
– PerfMon’s GUI defaults to taking measurements
every one second
• Too often for trend analysis
• 30-seconds is generally “good enough” for ad-hoc troubleshooting
Performance Monitor
Trang 101• A few areas DBAs want to monitor:
Trang 102• A few areas DBAs want to monitor:
– Disk usage
– CPU usage
– Memory usage
Performance Monitor
Trang 103• Disk usage monitoring
– Disk I/O is commonly a performance stealer
– While SQL Server manages the “when” and “how
often” of disk I/O, it is the operating system that
performs I/O
– When monitoring disk activity, you need to
monitor both SQL Server’s disk activity and the
disk activity of the server as a whole
Disk Usage
Trang 104• Disk I/O cannot be isolated as a metric
– Example: You are monitoring disk I/O and notice
that you have waits for disk reads/writes Does
this always mean that you need faster/more
disks?
• No! Could be that you are paging constantly and memory is the real bottleneck
Disk Usage
Trang 105• Important system objects and counters:
Disk Usage
Object Counter When to worry
PhysicalDisk % Disk Time Consistently > 90%
PhysicalDisk Current Disk Queue
PhysicalDisk Avg Disk Queue Length In many configs, a disk has 1 spindle Start
worrying when this value is more than 1.5x the number of spindles
Trang 106• PhysicalDisk: % Disk Time
– Tells us how “busy” your array is
– With this counter, you want to worry about
sustained activity, not bursts
– A % Disk Time of more than 75% for several
continuous minutes occurring several times a day
can likely be resolved:
• By adding more disks to the array
• By using faster drives
Disk Usage