Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 4: Creating and Tuning Indexes

Similar presentations


Presentation on theme: "Module 4: Creating and Tuning Indexes"— Presentation transcript:

1 Module 4: Creating and Tuning Indexes

2 Overview Planning Indexes Creating Indexes Optimizing Indexes
Creating XML Indexes

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

4 How SQL Server Accesses Data
Table scan SQL Server reads all table pages Index SQL Server uses index pages to find rows

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

6 What Is a Heap? Heap A table without a clustered index
Pages stored in no particular order sys.partitions id index_id = 0 first_iam_page IAM Page Heap Data Pages

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

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 Practice: Creating Indexes

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" } ] WITH option Purpose ALLOW_ROW_LOCKS Enables/disables row-level locks on index ALLOW_PAGE_LOCKS Enables/disables page-level locks on index ONLINE Enables/disables access to index during creation FILLFACTOR Controls free space on leaf-level pages PAD_INDEX Controls free space on non-leaf-level pages

10 What Are Unique Indexes?
Ensures no duplicate values in index key CREATE UNIQUE NONCLUSTERED INDEX [AK_Employee_LoginID] ON [HumanResources].[Employee] ( [LoginID] ASC) EmployeeID LoginID Gender MaritalStatus 216 mike0 M S 231 fukiko0 242 pat0 291 pat0 F S Duplicate key value not allowed

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

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

13 What Are Partitioned Indexes?
Index is partitioned horizontally by range similar to a partitioned table sys.partitions id index_id partition_number hobt_id 1 2 < 2003 > 2005 Consider alignment with underlying table and inclusion of partitioning key in index key (for unique indexes)

14 Options for Incorporating Free Space in Indexes
Availability of free space affects performance of index updates FILLFACTOR determines the amount of free space on leaf nodes Use low FILLFACTOR for OLTP applications Use high FILLFACTOR for OLAP applications PAD_FILL determines the 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 Reports System stored procedures sp_help sp_helpindex Catalog views System functions

16 Practice: Creating Indexes
In this practice, you will: Create 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? Practice: Using the Database Engine Tuning Advisor Index Fragmentation Options for Defragmenting Indexes Practice: Defragmenting Indexes

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

19 Practice: Using the Database Engine Tuning Advisor
In this practice, you will: Use a script of Transact-SQL to analyze indexes in AdventureWorks Review the index recommendations

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

21 Options for Defragmenting Indexes
<= 30% fragmentation = Reorganize > 30% fragmentation = Rebuild ALTER INDEX AK_Product_Name ON Production.Product REORGANIZE ALTER INDEX AK_Product_Name ON Production.Product REBUILD

22 Practice: Defragmenting Indexes
In this practice, you will: Identify fragmented indexes Remove fragmentation by rebuilding the index

23 Lesson 4: Creating XML Indexes
What Are XML Indexes? Types of XML Index Practice: Creating XML Indexes

24 What Are XML Indexes? Indexes on columns of xml data type
B-tree representation of the nodes in the xml data value Improve performance because query engine does not need to shred XML data for every query

25 Types of XML Index Index type Description Primary
Clustered B-tree representation of the nodes contained in an xml column Required to support one or more secondary xml indexes Secondary Path For retrieving data by path and value Property For retrieving data by specifying a path Value For retrieving data by using imprecise paths

26 Practice: Creating XML Indexes
In this practice, you will: Record the performance statistics for an XML query Create a primary XML index Create a secondary XML index Investigate the performance statistics for an XML query

27 Lab: Creating and Tuning Indexes
Exercise 1: Creating Indexes Exercise 2: Tuning Indexes Exercise 3: Creating XML Indexes


Download ppt "Module 4: Creating and Tuning Indexes"

Similar presentations


Ads by Google