module 3: creating and tuning indexes. planning indexes creating indexes optimizing indexes

26
Module 3: Creating and Tuning Indexes

Upload: gregory-pettengill

Post on 30-Mar-2015

262 views

Category:

Documents


4 download

TRANSCRIPT

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

Module 3: Creating and Tuning Indexes

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

Module 3: Creating and Tuning Indexes

• Planning Indexes

• Creating Indexes

• Optimizing Indexes

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

Lesson 1: Planning Indexes

• How SQL Server Accesses Data

• What Is a Heap?

• What Is a Clustered Index?

• What Is a Nonclustered Index?

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

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

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

What Is a Heap?

• A table without a clustered index

• Pages stored in no particular order

HeapHeap

id index_id=0 first_iam_page

IAM Page

Data Pages

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

What Is a Clustered Index?

• One clustered index per table

• B-tree stores data pages in order of index key

Leaf NodesLeaf Nodes

id index_id=1 root_page

Data PagesData Pages

Intermediate

Level

Intermediate

LevelIndex PagesIndex Pages

Root Index Page

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

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

id index_id>1 root_page

Data PagesData Pages

Leaf NodesLeaf Nodes

Index PagesIndex Pages

Root Index Page

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

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

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

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

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

What Are Unique Indexes?

CREATE UNIQUE NONCLUSTERED INDEX [AK_Employee_LoginID] ON [HumanResources].[Employee] ( [LoginID] ASC )

EmployeeID LoginID Gender MaritalStatus …

216 mike0 M S …

231 fukiko0 M M …

242 pat0 M S …

291 pat0 F S …

Duplicate key value not allowed

Duplicate key value not allowed

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

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_FirstName ON Peson.Contact ( LastName ASC, FirstName ASC )

• Improve query coverage and performance

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

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 columnQuery optimizer might ignore an index on a computed column

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

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)

< 2003 2003 - 2004 2004 - 2005 > 2005

id index_id partition_number hobt_id

1 2 1 67567567657

2 2 2 75675676665

… … … …

sys.partitions

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

Options for Incorporating Free Space in Indexes

Free space affects index update performance

• FILLFACTOR determines amount of free spaceon 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)

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

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

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

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

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

Lesson 3: Optimizing Indexes

• What Is the Database Engine Tuning Advisor?

• Index Fragmentation

• Options for Defragmenting Indexes

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

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

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

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

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

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:

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

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 ONProduction.Product REBUILD

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

Demonstration: Defragmenting Indexes

In this demonstration, you will see how to:

• Identify fragmented indexes

• Remove fragmentation by rebuilding the index

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

Lab: Creating and Optimizing Indexes

• Exercise 1: Creating Indexes

• Exercise 2: Optimizing Indexes

Logon information

Virtual machine NY-SQL-01

User name Administrator

Password Pa$$w0rd

Estimated time: 45 minutes

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

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.

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

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?

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

Module Review and Takeaways

• Review Questions

• Best Practices and Troubleshooting

• Tools