module 8 improving performance through nonclustered indexes

24
Module 8 Improving Performance through Nonclustered Indexes

Upload: virgil-boone

Post on 27-Dec-2015

213 views

Category:

Documents


0 download

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

HeapHeap

id index_id>=2 root_page

Leaf NodesContain Row IDs

Leaf NodesContain Row IDs

Data Pages

Root Index Page

id index_id=0 root_page

Index Pages

Nonclustered Indexes Over Clustered Indexes

Clustered IndexClustered Index

id index_id>=2 root_page

Leaf NodesContain Keys

Leaf NodesContain Keys

Index Pages Containing Data

Root Index Page

id index_id=1 root_page Root Index Page

Index Pages

Clustering Key

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

CREATE TABLE dbo.Book( ISBN nvarchar(20) PRIMARY KEY, Title nvarchar(50) NOT NULL, ReleaseDate date NOT NULL, PublisherID int NOT NULL);GOCREATE NONCLUSTERED INDEX IX_Book_Publisher ON dbo.Book (PublisherID, ReleaseDate DESC);GO

CREATE TABLE dbo.Book( ISBN nvarchar(20) PRIMARY KEY, Title nvarchar(50) NOT NULL, ReleaseDate date NOT NULL, PublisherID int NOT NULL);GOCREATE NONCLUSTERED INDEX IX_Book_Publisher ON dbo.Book (PublisherID, ReleaseDate DESC);GO

• Created using CREATE INDEX statement specifying: A name for the index The table to be indexed The columns that make up the index key

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

CREATE NONCLUSTERED INDEX IX_Book_Publisher ON dbo.Book (PublisherID, ReleaseDate DESC) INCLUDE (Title);GO

SELECT PublisherID, Title, ReleaseDateFROM dbo.Book WHERE ReleaseDate > DATEADD(year,-1,SYSDATETIME())ORDER BY PublisherID, ReleaseDate DESC;GO

CREATE NONCLUSTERED INDEX IX_Book_Publisher ON dbo.Book (PublisherID, ReleaseDate DESC) INCLUDE (Title);GO

SELECT PublisherID, Title, ReleaseDateFROM dbo.Book WHERE ReleaseDate > DATEADD(year,-1,SYSDATETIME())ORDER BY PublisherID, ReleaseDate DESC;GO

• Covering indexes can greatly increase performance of queries

• INCLUDE clause allows storage of selected data columns at the leaf level of a nonclustered index

Dropping or Altering Nonclustered Indexes

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

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

• 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

Filtered Indexes

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);GOCREATE INDEX IX_Transfer_IsFinalized ON dbo.Transfer (IsFinalized) WHERE IsFinalized = 0;GO

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);GOCREATE INDEX IX_Transfer_IsFinalized ON dbo.Transfer (IsFinalized) WHERE IsFinalized = 0;GO

• 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

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

Reports and Recommendations

Workload

Database Engine Tuning

Advisor

Database and Database Objects

• Used to suggest index and statistics changes for improving performance

• Processes workloads captured by SQL Server Profiler as traces

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

Estimated time: 45 minutes

Virtual machine 623XB-MIA-SQL

User name AdventureWorks\Administrator

Password Pa$$w0rd

Lab ScenarioThe 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