Download 6232B Module 08

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Microsoft Jet Database Engine wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Relational model wikipedia , lookup

SQL wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

PL/SQL wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Transcript
Module 8
Improving Performance
through Nonclustered
Indexes
Module Overview
• Designing Effective Nonclustered Indexes
• Implementing Nonclustered Indexes
• Using the Database Engine Tuning Advisor
Lesson 1: Designing Effective Nonclustered Indexes
• What is a Nonclustered Index?
• Nonclustered Indexes over Heaps
• Nonclustered Indexes over Clustered Indexes
• Methods for Obtaining Index Information
• Demonstration 1A: Obtaining Index Information
What is a Nonclustered Index?
• Table is structured as a heap or clustered index

Heap = Index ID 0

Clustered Index = Index ID 1
• Additional indexes can be created

Called "Nonclustered Indexes"

Also based on balanced B-Trees

Leaf levels point to base table structure rather than containing
data

Nonclustered Indexes = Index ID 2 and higher

Improve the performance of frequently-used queries

Impact on data modification performance needs to be
considered
Nonclustered Indexes Over Heaps
id
index_id>=2
root_page
Index Pages
Leaf Nodes
Contain Row IDs
id
index_id=0
Data Pages
Heap
root_page
Root Index
Page
Nonclustered Indexes Over Clustered Indexes
id
index_id>=2
root_page
Root Index
Page
Index Pages
Leaf Nodes
Contain Keys
Clustering Key
id
index_id=1
root_page
Index Pages Containing Data
Clustered Index
Root Index
Page
Methods for Obtaining Index Information
• SQL Server Management Studio

Object Explorer, Index Properties, Reports
• System Stored Procedures
• Catalog Views
• Dynamic Management Views and Functions

sys.dm_db_index_physical_stats

sys.dm_db_index_usage_stats

sys.dm_db_index_operational_stats
• System Functions
Demonstration 1A: Obtaining Index Information
• In this demonstration, you will see several ways to view
information about indexes
Lesson 2: Implementing Nonclustered Indexes
• Creating Nonclustered Indexes
• Performance Impact of Lookups in Nested Loops
• INCLUDE Clause
• Dropping or Altering Nonclustered Indexes
• Filtered Indexes
• Demonstration 2A: Nonclustered Indexes
Creating Nonclustered Indexes
• Created using CREATE INDEX statement specifying:

A name for the index

The table to be indexed

The columns that make up the index key
CREATE TABLE dbo.Book
( ISBN nvarchar(20) PRIMARY KEY,
Title nvarchar(50) NOT NULL,
ReleaseDate date NOT NULL,
PublisherID int NOT NULL
);
GO
CREATE NONCLUSTERED INDEX IX_Book_Publisher
ON dbo.Book (PublisherID, ReleaseDate DESC);
GO
Performance Impact of Lookups in Nested Loops
• Once a nonclustered index is traversed, SQL Server needs
to return the relevant data
• Lookups are used to retrieve the data rows from the base
table

Key Lookup (for tables with a clustered index)

RID Lookup (for tables as heaps)
• Lookups can be very expensive
INCLUDE Clause
• Covering indexes can greatly increase performance of queries
• INCLUDE clause allows storage of selected data columns at the
leaf level of a nonclustered index
CREATE NONCLUSTERED INDEX IX_Book_Publisher
ON dbo.Book (PublisherID, ReleaseDate DESC)
INCLUDE (Title);
GO
SELECT PublisherID, Title, ReleaseDate
FROM dbo.Book
WHERE ReleaseDate > DATEADD(year,-1,SYSDATETIME())
ORDER BY PublisherID, ReleaseDate DESC;
GO
Dropping or Altering Nonclustered Indexes
• Indexes are removed via the DROP INDEX statement

Does not apply to constraint-based internal indexes

Dropping a clustered index converts the table to a heap
• Maintenance operations are performed by ALTER INDEX

Structural changes to the indexes are not permitted
ALTER INDEX IX_Book_Publisher ON dbo.Book
DISABLE;
GO
ALTER INDEX IX_Book_Publisher ON dbo.Book
REBUILD WITH ONLINE = ON;
GO
DROP INDEX IX_Book_Publisher ON dbo.Book;
GO
Filtered Indexes
• Indexes contain a leaf level entry for every row in the table
• Filtered indexes are defined with a WHERE clause

Only contain rows that match the predicate

Filter must use simple logic
CREATE TABLE dbo.Transfer
( TransferID int IDENTITY(1,1) PRIMARY KEY,
TransferDate date NOT NULL,
FromAccount int NOT NULL,
ToAccount int NOT NULL,
Amount decimal(18,2) NOT NULL,
TransferType varchar(1) NOT NULL,
IsFinalized bit NOT NULL
);
GO
CREATE INDEX IX_Transfer_IsFinalized
ON dbo.Transfer (IsFinalized)
WHERE IsFinalized = 0;
GO
Demonstration 2A: Nonclustered Indexes
• In this demonstration, you will see how to:

Create covering indexes

View included columns in indexes
Lesson 3: Using the Database Engine Tuning Advisor
• SQL Server Profiler
• Demonstration 3A: SQL Server Profiler
• Database Engine Tuning Advisor
• Demonstration 3B: Database Engine Tuning Advisor
SQL Server Profiler
• SQL Server Profiler traces queries sent to SQL Server
• Captures selected columns when selected events occur

Events can be filtered
• Provides templates for traces
• Traces can be used for

Capturing queries when performance tuning

Diagnosing issues such as deadlocks

Replaying traces

Correlation with performance monitor logs
• SQL Trace stored procedures offer a lower-impact
alternative to SQL Server Profiler
Demonstration 3A: SQL Server Profiler
• In this demonstration you will see how to use SQL Server
Profiler
Database Engine Tuning Advisor
• Used to suggest index and statistics changes for improving
performance
• Processes workloads captured by SQL Server Profiler as
traces
Workload
Database
Engine Tuning
Advisor
Database and Database Objects
Reports and
Recommendations
Demonstration 3B: Database Engine Tuning Advisor
• In this demonstration, you will see how to use Database
Engine Tuning Advisor
Lab 8: Improving Performance through Nonclustered
Indexes
• Exercise 1: Nonclustered index usage review
• Exercise 2: Improving nonclustered index designs
• Exercise 3: SQL Server Profiler and Database Engine
Tuning Advisor
• Challenge Exercise 4: Nonclustered index design (Only if
time permits)
Logon information
Virtual machine
623XB-MIA-SQL
User name
AdventureWorks\Administrator
Password
Pa$$w0rd
Estimated time: 45 minutes
Lab Scenario
The marketing system includes a query that is constantly
executed and is performing too slowly. It retrieves 5000
web log entries beyond a given starting time. Previously, a
non-clustered index was created on the SessionStart
column. When 100 web log entries were being retrieved at
a time, the index was being used. The developer is puzzled
that changing the request to 5000 entries at a time has
caused SQL Server to ignore the index he built. You need to
investigate the query and suggest the best non-clustered
index to support the query. You will then test your
suggestion.
After you have created the new index, the developer noted
the cost of the sort operation and tried to create another
index that would eliminate the sort. You need to explain to
him why SQL Server has decided not to use this index.
Later you will learn to set up a basic query tuning trace in SQL
Server Profiler and use the trace captured in Database Engine
Tuning Advisor.
If time permits, you will design a required nonclustered index.
Lab Review
• Question: Do you ever need to include a column that is
part of the table's clustering key as an included column in
a nonclustered index when trying to create a covering
index?
• Question: If so, why? If not, why not and should you
include it anyway?
Module Review and Takeaways
• Review Questions
• Best Practices