Download as pps, pdf, or txt
Download as pps, pdf, or txt
You are on page 1of 26

Querying

QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Objectives

In this session, you will learn to:


Monitor performance
Optimize performance
Track data changes

Ver.
Ver. 1.0
1.0 Slide
Slide11of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Monitoring Performance

The performance of a database server is measured by the


time and resources consumed in responding to queries of
the users.
An administrator should regularly monitor the performance
of a database server and implement appropriate solutions to
optimize it.
SQL Server provides various tools to monitor and optimize
performance.

Ver.
Ver. 1.0
1.0 Slide
Slide22of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Monitoring Performance by Using SQL Server Profiler

SQL Server Profiler:


Provides a graphical user interface to monitor the life cycle of
the transactions being executed on a particular instance of the
database engine.
Tracks the login details of users, or the execution of individual
T-SQL statements or transactions.
Is used to identify the T-SQL statements or stored procedures
that slow down the performance of the server.
Tracks the events that occur in the database engine by
creating a trace.

Ver.
Ver. 1.0
1.0 Slide
Slide33of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Monitoring Performance by Using SQL Server Profiler (Contd.)

A trace:
Enables you to specify the events and the values for the
events that are to be monitored.
Allows you to apply filters.
Settings can be stored in a template.
SQL Server Profiler supports the following types of
templates:
Default template
Custom template

a user-defined
Is the template
template shipped that
with SQLcanServer
be reused.
Profiler. Under the default
template, SQL Server captures typical values from a trace, such as start
time, end time, and login name.

Ver.
Ver. 1.0
1.0 Slide
Slide44of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Monitoring Performance by Using SQL Server Profiler (Contd.)

To monitor the activities of users, the database


administrator needs to perform the following tasks:
1. Create a template.
2. Create and run a trace.

Let us see how to create a template.

Let us see how to create and run a trace.

Ver.
Ver. 1.0
1.0 Slide
Slide55of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Monitoring Performance by Using DMVs

Dynamic Management Views (DMVs):


Return server state information to monitor the health of a
server instance, diagnose problems, and tune performance.
Are a group of views and table-valued functions.
Are defined in the sys schema and are organized in various
categories.
The syntax to use a DMV is:
SELECT * FROM [DMV_NAME]
For example:
SELECT * FROM
sys.dm_tran_database_transactions

Displays the information about transactions at the database


level.

Ver.
Ver. 1.0
1.0 Slide
Slide66of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Monitoring Performance by Using DMVs (Contd.)

Click the following link to view an animation on DMVs.

Using DMVs

Ver.
Ver. 1.0
1.0 Slide
Slide77of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Just a minute

Which one of the following DMVs will you use to view the
connection information of all the available sessions?
sys.dm_exec_requests
sys.dm_exec_connections
sys.dm_exec_sessions
sys.dm_sql_referenced_entities

Solution:
sys.dm_exec_connections

Ver.
Ver. 1.0
1.0 Slide
Slide88of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Just a minute

Which of the following DMVs can be used to check the


details about the state of various I/O operations?
sys.dm_io_backup_tapes
sys.dm_io_pending_io_requests
sys.dm_io_cluster_shared_drives
sys.dm_clr_properties

Solution:
sys.dm_io_backup_tapes, sys.dm_io_pending_io_requests,
and sys.dm_io_cluster_shared_drives

Ver.
Ver. 1.0
1.0 Slide
Slide99of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Monitoring Performance by Using Database Engine Tuning Advisor

Database Engine Tuning Advisor:


Helps the database administrator tune the database.
Tests the database in various conditions by putting a workload
on it. Workload is a set of T-SQL statements that are executed
on the database.
Generates a report displaying the current state of database
objects on the basis of the throughput of the queries given in a
workload.
Provides suggestions to tune the database.

Let us see how to run the Database Engine Tuning Advisor.

Ver.
Ver. 1.0
1.0 Slide
Slide10
10of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Optimizing Performance

A database administrator has to ensure that the


performance of the queries does not suffer.
Indexes need to be continuously checked and regulated to
ensure desired performance.

Ver.
Ver. 1.0
1.0 Slide
Slide11
11of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Implementing Statistics

Statistics:
Refers to the information that the server collects about the
distribution of data in columns and indexes.
Is created automatically when indexes are created.
Can be automatically created and updated by SQL Server.
Can also be created manually by using the CREATE
STATISTICS statement.
Being maintained on a table can be listed by using the
sp_helpstats system stored procedure.
Can also be updated manually by using the UPDATE
STATISTICS statement.

Ver.
Ver. 1.0
1.0 Slide
Slide12
12of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Implementing Statistics (Contd.)

Click the following link to view an animation on using


statistics.

Ver.
Ver. 1.0
1.0 Slide
Slide13
13of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Implementing Statistics (Contd.)

Statistics IO is used to display information regarding the amount


of disk activity generated by a T-SQL statement.
Syntax:
SET STATISTICS IO { ON | OFF }

Ver.
Ver. 1.0
1.0 Slide
Slide14
14of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Implementing Plan Guides

Plan guides:
Allow you to attach query hints or a fixed query plan with the
query to optimize its performance.
Use the OPTION clause to attach a query hint.
Are of the following types:
OBJECT
SQL
TEMPLATE

Ver.
Ver. 1.0
1.0 Slide
Slide15
15of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Demo: Monitoring the Database

Problem Statement:
You are the database administrator at AdventureWorks, Inc.
Sam, one of the developers, has notified you that the query is
taking a long time to execute. To resolve the issue, you have
traced the queries being executed by Sam and stored the
workload in a file named SamTrace.txt. What should be done
to reduce the execution time taken by the queries?

Ver.
Ver. 1.0
1.0 Slide
Slide16
16of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Demo: Monitoring the Database (Contd.)

Solution:
To improve the performance of the queries, you should put this
trace file into SQL Server Database Tuning Advisor. For the
same, you need to perform the following tasks:
1. Start the analysis.
2. Set the tuning options.
3. Get the recommendations.

Ver.
Ver. 1.0
1.0 Slide
Slide17
17of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Tracking Data Changes

Sometimes, it is required to track the changes made on the


tables in a database.
SQL Server enables you to track data changes by using the
methods, Change Data Capture (CDC) and Change
Tracking.

Ver.
Ver. 1.0
1.0 Slide
Slide18
18of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Using CDC

CDC method is used to track the changes made by the


DML statements.
To use CDC to track changes, you need to perform the
following steps:
1. Enable CDC on a database.
2. Enable CDC on a table.
3. Track changes.

Ver.
Ver. 1.0
1.0 Slide
Slide19
19of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Using Change Tracking

You can also use the change tracking method to track the
changes made in the tables.
This method only captures the fact that certain changes
were made in the rows in a table. It, however, fails to
highlight the content that was changed.
To use change tracking, you need to perform the following
steps:
1. Enable change tracking on a database.
2. Enable change tracking on a table.
3. Implement change tracking.

Ver.
Ver. 1.0
1.0 Slide
Slide20
20of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Using Change Tracking (Contd.)

Before using the change tracking method, you need to


enable it on the database by using the ALTER DATABASE
statement.
After enabling change tracking on the database, you need
to enable it on the tables using the ALTER TABLE
statement.
The CHANGETABLE function is used to view the changes
made to the table.

Ver.
Ver. 1.0
1.0 Slide
Slide21
21of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Demo: Tracking Database Changes

Problem Statement:
The sales manager of AdventureWorks, Inc. needs to update
the e-mail address of an employee having ContactID 3, in the
Contact table. You need to track the updations made in the
Contact table by applying CDC.

Ver.
Ver. 1.0
1.0 Slide
Slide22
22of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Demo: Tracking Database Changes (Contd.)

Solution:
To track the updations made in the Contact table, you need to
perform the following tasks:
1. Enable CDC on the Contact table.
2. Update the Contact table.
3. View the track changes.

Ver.
Ver. 1.0
1.0 Slide
Slide23
23of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Summary

In this session, you learned that:


Performance of an SQL Server is measured by the time and
resources consumed in responding to queries of the users.
An event is any action generated within an instance of SQL
Server, such as the log on status, execution of T-SQL
statements, start or end of stored procedures, error returned by
SQL Server, or lock acquired or released by an object.
A trace captures and stores details of events you need to
monitor for an instance of SQL Server.
A template defines the standard set of events to be monitored in
a trace.
DMVs are a set of views and table-valued functions that return
server state information to the administrator.
Database Tuning Advisor is a tool that checks the performance
of a workload and suggests how to improve the performance.

Ver.
Ver. 1.0
1.0 Slide
Slide24
24of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
Summary (Contd.)

Statistics in SQL Server refers to information that the server


collects about the distribution of data in columns and indexes.
Statistics IO displays information regarding the amount of disk
activity generated by T-SQL statement.
SQL Server provides the CDC method to track the changes
made by the DML statements.
Before tracking the changes made by the DML statements, you
need to enable CDC on the database by executing the
sp_cdc_enable_db stored procedure.
The change tracking method only captures the fact that rows in
a table were changed, but does not capture the data that was
changed.

Ver.
Ver. 1.0
1.0 Slide
Slide25
25of
of50
26
Querying
QueryingData
and Using SQL Data
Managing Server
Using SQL Server
What’s Next?

Before the next session, please ensure to:


Read Chapter 8 and Chapter 9 of the book, Querying Data Using
SQL Server – II.
Go through the Additional Reference Links of Chapter 8 and
Chapter 9 of the book, Querying Data Using SQL Server- II
through Cloudscape.
Go through the recorded lectures on the following topics through
Cloudscape:
Monitoring Performance by Using Database Engine Tuning
Advisor
Using CDC
Complete the Lab@Home exercises based on the learning plan
available on the technology page on Cloudscape.

Ver.
Ver. 1.0
1.0 Slide
Slide26
26of
of50
26

You might also like