Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 3: Creating and Tuning Indexes. Planning Indexes Creating Indexes Optimizing Indexes.

Similar presentations


Presentation on theme: "Module 3: Creating and Tuning Indexes. Planning Indexes Creating Indexes Optimizing Indexes."— Presentation transcript:

1 Module 3: Creating and Tuning Indexes

2 Planning Indexes Creating Indexes Optimizing Indexes

3 Lesson 1: Planning Indexes How SQL Server Accesses Data What Is a Heap? What Is a Clustered Index? What Is a Nonclustered Index?

4 How SQL Server Accesses Data Index SQL Server reads all table pages SQL Server uses index pages to find rows Table Scan SQL Server 2008 introduces enhanced full-text indexing

5 What Is a Heap? A table without a clustered index Pages stored in no particular order Heap idindex_id=0first_iam_page IAM Page Data Pages

6 What Is a Clustered Index? One clustered index per table B-tree stores data pages in order of index key Leaf Nodes idindex_id=1root_page Data Pages Intermediate Level Intermediate Level Index Pages Root Index Page

7 What Is a Nonclustered Index? B-tree references underlying heap or clustered index Up to 249 nonclustered indexes per table Heap or Clustered Index Heap or Clustered Index idindex_id>1root_page Data Pages Leaf Nodes Index Pages Root Index Page

8 Lesson 2: Creating Indexes Overview of Creating Indexes What Are Unique Indexes? Considerations for Creating Indexes with Multiple Columns When to Create Indexes on Computed Columns What Are Partitioned Indexes? Options for Incorporating Free Space in Indexes Methods for Obtaining Index Information

9 Overview of Creating Indexes CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED ] INDEX index_name ON { table | view } ( column [ ASC | DESC] [,... n ] ) INCLUDE ( column [,... n ] ) [ WITH option [,... n ] ] [ ON { partition_scheme (column) | filegroup | default } ] Use SQL Server Management Studio -OR- CREATE INDEX Transact-SQL statement

10 What Are Unique Indexes? CREATE UNIQUE NONCLUSTERED INDEX [AK_Employee_LoginID] ON [HumanResources].[Employee] ( [LoginID] ASC ) EmployeeIDLoginIDGenderMaritalStatus… 216mike0MS… 231fukiko0MM… 242pat0MS… 291pat0FS… … Duplicate key value not allowed

11 Included columns Considerations for Creating Indexes with Multiple Columns Composite indexes Define most unique column first Include up to 16 columns and 900 bytes in key Nonkey columns included in index CREATE NONCLUSTERED INDEX AK_Employee_LoginID ON HumanResources.Employee ( LoginID ASC) INCLUDE ( ContactID, NationalIDNumber ) CREATE NONCLUSTERED INDEX K_Contact_LastName_First Name ON Peson.Contact ( LastName ASC, FirstName ASC ) Improve query coverage and performance

12 When to Create Indexes on Computed Columns You can create indexes on computed columns when: NUMERIC_ROUNDABORT option is set to OFF The expression is deterministic and precise ANSI_NULLS connection_level option is ON Column does not evaluate to text, ntext or image data types Required options are set to ON when index is created and when changes cause index to update Query optimizer might ignore an index on a computed column

13 Index is partitioned horizontally by range similar to a partitioned table What Are Partitioned Indexes? Consider aligning with underlying table and inclusion of partitioning in index key (unique) < 20032003 - 20042004 - 2005> 2005 idindex_idpartition_numberhobt_id 12167567567657 22275675676665 ………… sys.partitions

14 Options for Incorporating Free Space in Indexes Free space affects index update performance FILLFACTOR determines amount of free space on leaf nodes Use low FILLFACTOR for OLTP applications Use high FILLFACTOR for OLAP applications PAD_INDEX determines amount of free space on non-leaf index nodes CREATE UNIQUE NONCLUSTERED INDEX [AK_Employee_LoginID] ON [HumanResources].[Employee] ([LoginID] ASC) WITH FILLFACTOR = 65, PAD_INDEX = ON)

15 Methods for Obtaining Index Information SQL Server Management Studio: Object Explorer Index Properties window System stored procedures: sp_help Catalog Views System functions Reports sp_helpindex

16 Demonstration: Creating Indexes In this demonstration, you will see how to: Create an index by using SQL Server Management Studio Examine an index by using SQL Server Management Studio Create an index by using Transact-SQL

17 Lesson 3: Optimizing Indexes What Is the Database Engine Tuning Advisor? Index Fragmentation Options for Defragmenting Indexes

18 What Is the Database Engine Tuning Advisor? Analyzes database performance under workload Graphical and command-line interfaces Reports and Recommendations Workload Database Engine Tuning Advisor Database and Database Objects

19 Demonstration: Using the Database Engine Tuning Advisor In this demonstration, you will see how to: Use a Transact-SQL script to analyze indexes in AdventureWorks Review the index recommendations

20 Index Fragmentation How does fragmentation occur? SQL Server reorganizes index pages when data is modified – causes index pages to split SQL Server Management Studio – Index Properties System function - sys.dm_db_index_physical_stats Detecting fragmentation Internal – pages are not full External – pages are out of logical sequence Types of fragmentation:

21 Options for Defragmenting Indexes Reorganize Less than 30% fragmentation Greater than 30% fragmentation ALTER INDEX AK_Product_Name ON Production.Product REORGANIZE Rebuild ALTER INDEX AK_Product_Name ON Production.Product REBUILD

22 Demonstration: Defragmenting Indexes In this demonstration, you will see how to: Identify fragmented indexes Remove fragmentation by rebuilding the index

23 Lab: Creating and Optimizing Indexes Exercise 1: Creating Indexes Exercise 2: Optimizing Indexes Logon information Virtual machineNY-SQL-01 User nameAdministrator Password Pa$$w0rd Estimated time: 45 minutes

24 Lab Scenario The Production department at Adventure Works has spent the last six months reviewing its processes and application support requirements. Out of this review have come a number of issues that need immediate attention and a number of new initiatives. As each initiative is started, the impact it will have on the database and any new requirements it introduces are discussed with the senior database developer and an appropriate course of action is agreed on.

25 Lab Review In SQL Server Management Studio, how do you view the index properties of a table? What does the option Fillfactor = 90 do when creating an index? When you check the fragmentation properties on the AK_Product_Name index after addressing the fragmentation, the fragmentation level is only reduced by 5%. What did you do wrong?

26 Module Review and Takeaways Review Questions Best Practices and Troubleshooting Tools


Download ppt "Module 3: Creating and Tuning Indexes. Planning Indexes Creating Indexes Optimizing Indexes."

Similar presentations


Ads by Google