Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

Module 5:

Monitoring SQL Server


Overview

Viewing Current Activity


Using System Monitor
Using SQL Server Profiler
Using DDL Triggers
Using Event Notifications
Lesson 1: Viewing Current Activity

What Is Activity Monitor?


What Are Dynamic Management Views and Functions?
Practice: Viewing Current Activity
What Is Activity Monitor?

Graphical views of current user connections and locks


 Process Info
 Locks by Process
 Locks by Object
What Are Dynamic Management Views?

Provide information about the current state of a server, for


example:
 Locks held, threads, memory usage
Listed in <Database name>\Views\System Views folder

sys.dm_*
Practice: Viewing Current Activity

In this practice, you will:


View current activity by using
Activity Monitor
View current activity by using dynamic
management views
Lesson 2: Using System Monitor

Introduction to System Monitor


SQL Server Performance Objects
Considerations for Monitoring SQL Server
Demonstration: Using System Monitor
Introduction to System Monitor

Use System Monitor to view system metrics


 Objects
 Counters
 Instances
SQL Server Performance Objects

SQL Server-specific objects enable you to monitor each


instance of SQL Server
SQL Server-specific objects include:
Object Description
SQLServer:Buffer Provides information about the memory
Manager buffers used by SQL Server
Provides information about a SQL
SQLServer:Databases
Server database
Provides information about individual
SQLServer:Locks
lock requests
SQLServer:Memory Provides information about SQL Server
Manager memory usage
Considerations for Monitoring SQL Server

Key areas to monitor


 Disk system
 Memory
 CPU
Demonstration: Using System Monitor

In this demonstration, you will see how to:


View live activity in System Monitor
Log performance data
Lesson 3: Using SQL Server Profiler

What Is SQL Server Profiler?


SQL Server Profiler Trace Options
Trace Categories, Events, and Columns
Demonstration: Using SQL Server Profiler
What Is SQL Server Profiler?

Graphical tool for tracing server and database activity

 Create a trace that is based on a reusable template


 Watch the trace results as the trace runs
 Store the trace results in a table or file for
further analysis
 Start, stop, pause, and modify the trace results as necessary
 Replay the trace results
SQL Server Profiler Trace Options

Specify trace template


 Predefined
 User defined
Save trace data
 Save to table
 Save to file
Specify trace stop time
Trace Categories, Events, and Columns

Categories
 Groups of related events
Events
 The occurrence of an action within SQL Server
Columns
 The attributes of events
 Managed by using column filters
Demonstration: Using SQL Server Profiler

In this demonstration, you will see how to:


Create a new trace by using SQL Server
Profiler
Use the trace to capture a record of SQL
Server activity
Lesson 4: Using DDL Triggers

What Are DDL Triggers?


How to Create DDL Triggers
How to Manage DDL Triggers
Demonstration: Creating a DDL Trigger
What Are DDL Triggers?

Triggers to trap DDL statement execution


Database or server scope
Process:
1 DDL statement executed CREATE TABLE SomeTable(…)

2 DDL action performed

3 Trigger fires EventData


How to Create DDL Triggers

Define the trigger name, scope, and event

Retrieve event information using eventdata()

Extract event data by using XQuery


CREATE TRIGGER CreateTable
ON DATABASE
FOR CREATE_TABLE
AS
...
DECLARE @data XML
. . .
DECLARE @database NVARCHAR (100)
SET @data = eventdata()
.
SET. .
@database =
@data.value('(/EVENT_INSTANCE/DatabaseName)[1]',
'nvarchar(100)')
. . .
How to Manage DDL Triggers

Viewing triggers
SELECT name FROM sys.triggers
SELECT definition FROM sys.sql_modules . . .

Modifying triggers
ALTER TRIGGER CreateTable ON DATABASE FOR
CREATE_TABLE
AS . . .

Deleting triggers

DROP TRIGGER CreateTable ON DATABASE


Demonstration: Creating a DDL Trigger

In this demonstration, you will see how to:


Create a DDL trigger that fires whenever any
DDL operation is performed in a database
Test the DDL trigger and display the
eventdata information
Lesson 5: Using Event Notifications

What Are Event Notifications?


How to Create Event Notifications
How to Process Event Notifications
How to Manage Event Notifications
Demonstration: Implementing Event Notifications 
What Are Event Notifications?

Messages containing event data


 DDL events
 DML events
 Trace events
Sent to an event processing service by using
Service Broker
 A message type and contract are predefined
 You must create a queue, a service, and a route
How to Create Event Notifications

Define the event notification

Specify the scope

Specify the event

Specify the service

CREATE EVENT NOTIFICATION UpdateStats


ON SERVER
FOR UPDATE_STATISTICS
TO SERVICE 'NotifyService', 'current database'
How to Process Event Notifications

Receive the message

Extract event data by using XQuery

DECLARE @messageTypeName
@cmd nvarchar(1000)
NVARCHAR(256),
DECLARE
@messageBody
@posttimeXML
nvarchar(24)
;RECEIVE@spid
DECLARE TOP(1)nvarchar(6)
@messageTypeName = message_type_name,
SET @messageBody
@cmd = @messagebody.value
= message_body
FROM dbo.NotifyQueue;
('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]',
'nvarchar(100)')
SET @messagebody.value
IF @@ROWCOUNT = 0
RETURN
('(/EVENT_INSTANCE/PostTime)[1]','nvarchar(24)')
SET @spid = @messagebody.value
('(/EVENT_INSTANCE/SPID)[1]','nvarchar(6)')
How to Manage Event Notifications

Viewing event notifications and queues


SELECT name FROM sys.event_notifications
SELECT definition FROM sys.service_queues

Deleting event notifications

DROP EVENT NOTIFICATION UpdateStats ON SERVER


Demonstration: Implementing Event Notifications

In this demonstration, you will see how to:


Create Service Broker objects to support
event notifications
Create event notifications that capture
CREATE_TABLE and ALTER_TABLE events
Test the event notifications
Lab: Monitoring SQL Server

Exercise 1: Monitoring SQL


Server Performance
Exercise 2: Tracing SQL Server Activity
Exercise 3: Implementing DDL Triggers

You might also like