1 / 24

Module 8 Improving Performance through Nonclustered Indexes

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?

aisha
Download Presentation

Module 8 Improving Performance through Nonclustered Indexes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module 8 Improving Performance through Nonclustered Indexes

  2. Module Overview • Designing Effective Nonclustered Indexes • Implementing Nonclustered Indexes • Using the Database Engine Tuning Advisor

  3. 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

  4. 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

  5. Nonclustered Indexes Over Heaps Root Index Page Index Pages Leaf Nodes Contain Row IDs Data Pages Heap

  6. Nonclustered Indexes Over Clustered Indexes Root Index Page Index Pages Leaf Nodes Contain Keys Clustering Key Root Index Page Index Pages Containing Data Clustered Index

  7. 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

  8. Demonstration 1A: Obtaining Index Information • In this demonstration, you will see several ways to view information about indexes

  9. 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

  10. 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 CREATETABLEdbo.Book ( ISBN nvarchar(20)PRIMARYKEY, Title nvarchar(50)NOTNULL, ReleaseDatedateNOTNULL, PublisherIDintNOTNULL ); GO CREATENONCLUSTEREDINDEXIX_Book_Publisher ONdbo.Book(PublisherID,ReleaseDateDESC); GO

  11. 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

  12. 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 CREATENONCLUSTEREDINDEXIX_Book_Publisher ONdbo.Book(PublisherID,ReleaseDateDESC) INCLUDE(Title); GO SELECTPublisherID, Title,ReleaseDate FROMdbo.Book WHEREReleaseDate>DATEADD(year,-1,SYSDATETIME()) ORDERBYPublisherID,ReleaseDateDESC; GO

  13. 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 ALTERINDEXIX_Book_PublisherONdbo.Book DISABLE; GO ALTERINDEXIX_Book_PublisherONdbo.Book REBUILD WITH ONLINE = ON; GO DROPINDEXIX_Book_PublisherONdbo.Book; GO

  14. 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 CREATETABLEdbo.Transfer (TransferIDintIDENTITY(1,1)PRIMARYKEY, TransferDatedateNOTNULL, FromAccountintNOTNULL, ToAccountintNOTNULL, Amount decimal(18,2)NOTNULL, TransferTypevarchar(1)NOTNULL, IsFinalizedbitNOTNULL ); GO CREATEINDEXIX_Transfer_IsFinalized ONdbo.Transfer(IsFinalized) WHEREIsFinalized= 0; GO

  15. Demonstration 2A: Nonclustered Indexes • In this demonstration, you will see how to: • Create covering indexes • View included columns in indexes

  16. 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

  17. 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

  18. Demonstration 3A: SQL Server Profiler • In this demonstration you will see how to use SQL Server Profiler

  19. 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 Reports and Recommendations Database and Database Objects

  20. Demonstration 3B: Database Engine Tuning Advisor • In this demonstration, you will see how to use Database Engine Tuning Advisor

  21. 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: 45minutes

  22. 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.

  23. 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?

  24. Module Review and Takeaways • Review Questions • Best Practices

More Related