Module 4 Creating and Tuning IndexesContents:Lesson 1: Planning Indexes        4-2Lesson 2: Creating Indexes       4-10Les...
Information in this document, including URL and other Internet Web site references, is subject to changewithout notice. Un...
Module 4: Creating and Tuning Indexes      4–1**************************************** Illegal for non-trainer use *******...
4–2      Module 4: Creating and Tuning IndexesLesson 1: Planning Indexes**************************************** Illegal f...
Module 4: Creating and Tuning Indexes       4–3How SQL Server Accesses Data**************************************** Illega...
4–4      Module 4: Creating and Tuning IndexesWhat Is a Clustered Index?**************************************** Illegal f...
Module 4: Creating and Tuning Indexes   4–5                                 first value is found by using the clustered in...
4–6      Module 4: Creating and Tuning IndexesWhat Is a Heap?**************************************** Illegal for non-trai...
Module 4: Creating and Tuning Indexes     4–7  ■   Contain data that is written and rarely read, like an audit log. An ind...
4–8      Module 4: Creating and Tuning IndexesWhat Is a Nonclustered Index?**************************************** Illega...
Module 4: Creating and Tuning Indexes     4–9                           Nonclustered indexes are designed to improve the p...
4–10     Module 4: Creating and Tuning IndexesLesson 2: Creating Indexes**************************************** Illegal f...
Module 4: Creating and Tuning Indexes   4–11Overview of Creating Indexes**************************************** Illegal f...
4–12      Module 4: Creating and Tuning IndexesLocking options            The ALLOW_ROW_LOCKS and ALLOW_PAGE_LOCKS options...
Module 4: Creating and Tuning Indexes    4–13What Are Unique Indexes?**************************************** Illegal for ...
4–14     Module 4: Creating and Tuning IndexesExample of creating a     You can create a unique index in SQL Server Manage...
Module 4: Creating and Tuning Indexes   4–15Considerations for Creating Indexes with Multiple Columns*********************...
4–16     Module 4: Creating and Tuning IndexesGuidelines for creating   When you create a composite index, consider the fo...
Module 4: Creating and Tuning Indexes   4–17When to Create Indexes on Computed Columns************************************...
4–18   Module 4: Creating and Tuning Indexes                        Note The query optimizer ignores an index on a compute...
Module 4: Creating and Tuning Indexes     4–19What Are Partitioned Indexes?**************************************** Illega...
4–20     Module 4: Creating and Tuning Indexes                           For More Information For more information about c...
Module 4: Creating and Tuning Indexes      4–21Options for Incorporating Free Space in Indexes****************************...
4–22      Module 4: Creating and Tuning Indexes                             Note The FILLFACTOR option’s main purpose is t...
Module 4: Creating and Tuning Indexes   4–23Methods for Obtaining Index Information***************************************...
4–24     Module 4: Creating and Tuning Indexes                           The following example shows how to obtain index i...
Module 4: Creating and Tuning Indexes   4–25Function                          Provides information aboutINDEXPROPERTY     ...
4–26     Module 4: Creating and Tuning IndexesPractice: Creating Indexes**************************************** Illegal f...
Module 4: Creating and Tuning Indexes   4–27                         6. In the New Index dialog box, enter the following v...
4–28     Module 4: Creating and Tuning IndexesLesson 3: Optimizing Indexes**************************************** Illegal...
Module 4: Creating and Tuning Indexes    4–29What Is the Database Engine Tuning Advisor?**********************************...
4–30   Module 4: Creating and Tuning Indexes                        Analysis can be performed in either of the following t...
Module 4: Creating and Tuning Indexes     4–31Practice: Using the Database Engine Tuning Advisor**************************...
4–32     Module 4: Creating and Tuning Indexes                            6. Click Workload.sql, and then click Open.     ...
Module 4: Creating and Tuning Indexes     4–33Index Fragmentation**************************************** Illegal for non-...
4–34   Module 4: Creating and Tuning Indexes                        To view details of index fragmentation in SQL Server M...
Module 4: Creating and Tuning Indexes     4–35Options for Defragmenting Indexes**************************************** Il...
4–36     Module 4: Creating and Tuning Indexes                          Note If fragmentation is less than 30 percent and ...
Module 4: Creating and Tuning Indexes   4–37Practice: Defragmenting Indexes**************************************** Illega...
4–38     Module 4: Creating and Tuning Indexes                                        avg_fragmentation_in_percent as Frag...
Module 4: Creating and Tuning Indexes   4–39Lesson 4: Creating XML Indexes**************************************** Illegal...
4–40     Module 4: Creating and Tuning IndexesWhat Are XML Indexes?**************************************** Illegal for no...
Module 4: Creating and Tuning Indexes   4–41  ■   You must set the ARITHABORT option to ON when an XML index is created an...
4–42    Module 4: Creating and Tuning IndexesTypes of XML Index**************************************** Illegal for non-tr...
9767404 - Creating and Tuning Indexes
9767404 - Creating and Tuning Indexes
9767404 - Creating and Tuning Indexes
9767404 - Creating and Tuning Indexes
9767404 - Creating and Tuning Indexes
9767404 - Creating and Tuning Indexes
9767404 - Creating and Tuning Indexes
9767404 - Creating and Tuning Indexes
9767404 - Creating and Tuning Indexes
9767404 - Creating and Tuning Indexes
Upcoming SlideShare
Loading in …5
×

9767404 - Creating and Tuning Indexes

1,372 views

Published on

SQL - Creating and Tuning Indexes

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,372
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

9767404 - Creating and Tuning Indexes

  1. 1. Module 4 Creating and Tuning IndexesContents:Lesson 1: Planning Indexes 4-2Lesson 2: Creating Indexes 4-10Lesson 3: Optimizing Indexes 4-28Lesson 4: Creating XML Indexes 4-39Lab: Creating Indexes 4-46
  2. 2. Information in this document, including URL and other Internet Web site references, is subject to changewithout notice. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with anyreal company, organization, product, domain name, e-mail address, logo, person, place or event is intended orshould be inferred. Complying with all applicable copyright laws is the responsibility of the user. Withoutlimiting the rights under copyright, no part of this document may be reproduced, stored in or introduced intoa retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying,recording, or otherwise), or for any purpose, without the express written permission of MicrosoftCorporation.The names of manufacturers, products, or URLs are provided for informational purposes only and Microsoftmakes no representations and warranties, either expressed, implied, or statutory, regarding thesemanufacturers or the use of the products with any Microsoft technologies. The inclusion of a manufacturer orproduct does not imply endorsement of Microsoft of the manufacturer or product. Links are provided tothird party sites. Such sites are not under the control of Microsoft and Microsoft is not responsible for thecontents of any linked site or any link contained in a linked site, or any changes or updates to such sites.Microsoft is not responsible for webcasting or any other form of transmission received from any linked site.Microsoft is providing these links to you only as a convenience, and the inclusion of any link does not implyendorsement of Microsoft of the site or the products contained therein.Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rightscovering subject matter in this document. Except as expressly provided in any written license agreementfrom Microsoft, the furnishing of this document does not give you any license to these patents, trademarks,copyrights, or other intellectual property.©2006 Microsoft Corporation. All rights reserved.Microsoft, JScript, MSDN, Outlook, PowerPoint, Visual Basic, Visual C#, Visual C++, Visual FoxPro, Windows,and Windows Server are either registered tradmarks or trademarks of Microsoft Corporation in the UnitedStates and/or other countries.The names of actual companies and products mentioned herein may be the trademarks of their respectiveowners.
  3. 3. Module 4: Creating and Tuning Indexes 4–1**************************************** Illegal for non-trainer use ***************************************Module objectives After completing this module, students will be able to: ■ Plan indexes. ■ Create indexes. ■ Optimize indexes. ■ Create XML indexes.Introduction An index is a collection of pages associated with a table (or view) used to speed retrieval of rows from the table or enforce uniqueness. For example, without an index, you would have to go through an entire textbook one page at a time to find information about a topic. Microsoft® SQL Server™ 2005 uses indexes to point to the location of a row on a data page instead of having to look through all the data pages of a table. An index contains keys built from one or more columns in the table. These keys are stored in a way that enables SQL Server 2005 to find the rows associated with the key values quickly and efficiently. This module provides an overview of planning, creating, and optimizing indexes. It explains the differences between heaps, clustered indexes, and nonclustered indexes and where you would use each one. It describes how to create the different types of indexes and how to configure and maintain them to attain optimal performance benefits. Note The information related to indexes on tables presented in this module is also relevant to indexes on views. For simplicity and to avoid continually repeating “table and/or view,” this module focuses on tables. However, you should keep in mind the applicability of the material to views.
  4. 4. 4–2 Module 4: Creating and Tuning IndexesLesson 1: Planning Indexes**************************************** Illegal for non-trainer use ***************************************Lesson objectives After completing this lesson, students will be able to: ■ Describe how SQL Server accesses data. ■ Define a clustered index and identify when to use a clustered index. ■ Define a heap and identify when to use heaps. ■ Define a nonclustered index and identify when to use a nonclustered index.Introduction In this lesson, you will learn the three fundamental indexing options offered by SQL Server 2005: clustered indexes, heaps, and nonclustered indexes. You will learn the benefits and disadvantages of each option and be able to identify when to use each option. Planning useful indexes is one of the most important aspects of improving query performance. It requires an understanding of both index structure and how the data is used. Understanding the basics of how data is stored and accessed is the first step in understanding how indexes work, why you would want to use them, and the reasons you would want to use each of the different indexing options provided by SQL Server 2005.
  5. 5. Module 4: Creating and Tuning Indexes 4–3How SQL Server Accesses Data**************************************** Illegal for non-trainer use ***************************************How SQL Server accesses SQL Server accesses data in one of two ways:data ■ By scanning all the data pages in a table—called a table scan. When SQL Server performs a table scan, it: 1. Starts at the beginning of the table. 2. Scans from page to page through all the rows in the table. 3. Extracts the rows that meet the criteria of the query. ■ By using indexes. When SQL Server uses an index, it: 1. Traverses the index tree structure to find rows that the query requests. 2. Extracts only the needed rows that meet the criteria of the query. SQL Server first determines whether an index exists. Then the query optimizer—the component responsible for generating the optimal execution plan for a query— determines whether scanning a table or using the index is more efficient for accessing data. For More Information For more information about how SQL Server accesses data, see “Query Processing Architecture” in SQL Server Books Online.
  6. 6. 4–4 Module 4: Creating and Tuning IndexesWhat Is a Clustered Index?**************************************** Illegal for non-trainer use ***************************************What is a clustered A clustered index sorts and stores the data rows of the table in order based on theindex? clustered index key. The clustered index is implemented as a B-tree. Each page in a B- tree is called an index node. The top node of the B-tree is called the root node. The bottom level of nodes in the index is called the leaf level. Any index levels between the root node and the leaf nodes are collectively known as intermediate nodes. Each page in the intermediate or bottom levels has a pointer to the preceding and subsequent pages, forming a doubly linked list. This structure provides a highly efficient mechanism to speed the process of locating data. In a clustered index, the root and intermediary nodes contain index pages holding index rows. Each index row contains a key value and a pointer to either an intermediate- level page in the B-tree or a data row in the leaf level of the index. The pages in each level of the index are linked in a doubly linked list. Because a clustered index determines the order in which table rows are actually stored, each table can have only one clustered index—the table’s rows cannot be stored in more than one order. Important Columns with the following data types cannot be used as the key in a clustered index: ntext, text, varchar(max), nvarchar(max), varbinary(max), xml, or image.When to use a clustered Because you can have only one clustered index per table, you must ensure that you useindex it to achieve the maximum benefit possible. Before you create a clustered index, you need to understand how your data will be accessed. Because a clustered index determines the order in which SQL Server 2005 stores the table’s data rows, clustered indexes are more suited to certain types of data and usage patterns. Clustered indexes are most effective when used to support queries that do the following: ■ Return a range of values by using operators such as BETWEEN, >, >=, <, and <=. Because the table data is physically stored in index order, after the row with the
  7. 7. Module 4: Creating and Tuning Indexes 4–5 first value is found by using the clustered index, rows with subsequent indexed values are guaranteed to be physically adjacent. ■ Return data sorted using the ORDER BY or GROUP BY clause. An index on the columns specified in the ORDER BY or GROUP BY clause might remove the need for the database engine to sort the data because the rows are already sorted. This improves query performance. ■ Return data combined by using JOIN clauses; typically these are foreign key columns. ■ Return large result sets.Considerations for using When you define a clustered index, you should define the index key with as fewclustered indexes columns as possible. Keeping your clustered key value small increases the number of index rows that can be placed on an index page and decreases the number of levels that must be traversed. This minimizes input/output (I/O). Also, consider columns that have one or more of the following attributes: ■ Are unique or contain many distinct values. This includes columns defined as IDENTITY because the column is guaranteed to be unique within the table. Key value uniqueness is maintained explicitly, by using the UNIQUE keyword, or implicitly, by using an internal unique identifier. If a clustered index contains duplicate values, SQL Server must distinguish between rows that contain identical values in the key column or columns. It does this by using a 4-byte integer (uniquifier value) in an additional system-only uniquifier column. These unique identifiers are internal to SQL Server and are not accessible to the user. ■ Are used frequently to sort the data retrieved from a table because this will save the cost of a sort operation whenever a query sorts results on that column. ■ Are frequently accessed sequentially.When not to use Clustered indexes are not a good choice when:clustered indexes ■ The data in the indexed columns will change frequently. Changes to a clustered index mean that the entire row of data must be moved because the database engine must keep the data values of a row in physical order. This is an important consideration in high-volume transaction processing systems in which data is typically volatile. ■ The index keys are wide. Wide keys are composite keys of several columns or several large-size columns. All nonclustered indexes use the key values from the clustered index as lookup keys. Any nonclustered indexes defined on the same table will be significantly larger because the nonclustered index entries contain the clustering key and also the key columns defined for that nonclustered index.
  8. 8. 4–6 Module 4: Creating and Tuning IndexesWhat Is a Heap?**************************************** Illegal for non-trainer use ***************************************What is a heap? A heap is a table without a clustered index. The data rows are not stored in any particular order, and there is no particular order to the sequence of the data pages. The data pages are not linked in a linked list. SQL Server always maintains data pages in a heap unless a clustered index is defined on the table. SQL Server uses Index Allocation Map (IAM) pages to maintain heaps. IAM pages: ■ Contain information about where SQL Server stores the extents of a heap. The sys.partitions system table stores a pointer to the first IAM page associated with a heap. This will be a record with index_id = 0. ■ Enable navigation through the heap to find available space when new rows are inserted into the table. ■ Associate data pages with the table. The data pages and the rows within them are not in any specific order and are not linked. The only logical association between data pages is that which is recorded in the IAM pages.When to use a heap You will use a heap by default whenever you do not define a clustered index on a table. You would choose to use a heap when the table contains data that is structured or used in a way that is not suitable for the implementation of a clustered index. You can still implement nonclustered indexes on a table that uses a heap. Consider using a heap for tables that: ■ Contain volatile data where rows are added, deleted, and updated frequently. The overhead of index maintenance can be more costly than the benefits. ■ Contain small amounts of data. Using a table scan to find data can be quicker than maintaining and using an index. ■ Contain predominantly duplicate data rows. A table scan can be quicker than an index lookup is.
  9. 9. Module 4: Creating and Tuning Indexes 4–7 ■ Contain data that is written and rarely read, like an audit log. An index can be an unnecessary storage and maintenance overhead.Important When you create a PRIMARY KEY constraint on a table, a unique index ona column (or columns) is automatically created. By default, this index is clustered,meaning that the table will no longer be stored as a heap. You can specify a nonclusteredindex when you create the constraint by using the NONCLUSTERED argument. Formore information about primary keys, see “PRIMARY KEY Constraints” in SQL ServerBooks Online.
  10. 10. 4–8 Module 4: Creating and Tuning IndexesWhat Is a Nonclustered Index?**************************************** Illegal for non-trainer use ***************************************What is a nonclustered Nonclustered indexes have the same B-tree structure as clustered indexes except thatindex? the data rows of the underlying table are not sorted and stored in order based on their nonclustered keys. In the nonclustered index, the data and the index are stored separately, and the leaf level of the index consists of index pages instead of data pages. The rows in the nonclustered index are stored in the order of the index key values, but the referenced data rows are not guaranteed to be in any particular order unless a clustered index is created on the table. Each index row in the nonclustered index contains the nonclustered key value and a row locator. This locator points to the data row if the table is a heap and contains the clustered index key for the row if the table has a clustered index. If the indexed table has a clustered index, the column or columns defined in the clustered index are automatically appended to the end of each nonclustered index on the table. This can produce a covered query without specifying the clustered index columns in the definition of the nonclustered index. For example, if a table has a clustered index on column C, a nonclustered index on columns B and A will have as its key values columns B, A, and C. A table can have up to 249 nonclustered indexes. Nonclustered indexes can be defined on a table regardless of whether the table uses a clustered index or a heap. Important Columns with the following data types cannot be used as the key in a nonclustered index: ntext, text, varchar(max), nvarchar(max), varbinary(max), xml, or image.When to use a Nonclustered indexes are useful when users require multiple ways to search data. Fornonclustered index example, a user might frequently search through a gardening database looking for both the common and the scientific names of plants. You could create a nonclustered index for retrieving the scientific names and a clustered index for retrieving common names.
  11. 11. Module 4: Creating and Tuning Indexes 4–9 Nonclustered indexes are designed to improve the performance of frequently used queries that are not covered by a clustered index. If your table already has a clustered index and you need to index another column, you have no choice but to use a nonclustered index. Before you create nonclustered indexes, you should understand how your data will be accessed. You achieve maximum query performance improvements when an index contains all columns in a query. The term coverage refers to the number of columns involved in a query that are supported by an index. With total coverage, the query optimizer can locate all the column values within the index, meaning that the heap or clustered index data is not accessed and resulting in fewer disk I/O operations. However, wide keys and too many indexes can also be detrimental to general database performance. Consider using a nonclustered index when: ■ You want to improve the performance of queries that use JOIN or GROUP BY clauses. You should create multiple nonclustered indexes on columns involved in the join and grouping operations and a clustered index on any foreign key columns. ■ Your table has low update frequency but contains large volumes of data—for example, a decision support application. Such applications contain large amounts of primarily read-only data and can benefit significantly from many nonclustered indexes. With many indexes, the query optimizer has more indexes to choose from to determine the fastest access method, and the low update characteristics of the database mean that index maintenance is unlikely to impede performance. ■ You know that your queries do not return large result sets. ■ You need to index columns that are frequently involved in search conditions of a query—such as a WHERE clause—that return exact matches. ■ You need to index columns that contain many distinct values, such as a combination of last name and first name. Nonclustered indexes work best on columns in which the data selectivity ranges from highly selective to unique.Considerations for using When you create a nonclustered index, consider the following guidelines:nonclustered indexes ■ Create clustered indexes before nonclustered indexes. SQL Server automatically rebuilds existing nonclustered indexes when: ● An existing clustered index is dropped. ● A clustered index is created. ● The columns that define the clustered index are changed. ■ Avoid large numbers of nonclustered indexes when your table is used in an online transaction processing (OLTP) application or is updated frequently. Large numbers of indexes on a table affect the performance of INSERT, UPDATE, and DELETE statements because all indexes must be updated as data in the table changes.Facts about nonclustered Remember the following facts when using nonclustered indexes:indexes ■ The database engine will create a nonclustered index unless you specify that the index should be clustered. ■ The query optimizer will not use an index if the table contains very few distinct values because a table scan is generally more efficient. ■ The order of the leaf-level pages of a nonclustered index differs from the physical order of the table.
  12. 12. 4–10 Module 4: Creating and Tuning IndexesLesson 2: Creating Indexes**************************************** Illegal for non-trainer use ***************************************Lesson objectives After completing this lesson, students will be able to: ■ Describe the syntax and options for creating and dropping indexes. ■ Describe unique indexes and the considerations and syntax for creating unique indexes. ■ Describe the differences between composite indexes and indexes with included columns. ■ Describe the requirements and syntax for creating indexes on computed columns. ■ Describe partitioned indexes and the considerations for creating partitioned indexes. ■ Describe considerations for incorporating free space into indexes. ■ Describe methods that can be used to obtain information about indexes.Introduction Once you have decided what you want to index and whether to use a clustered or nonclustered index, you must create the index. Many options are available to you when you create an index, and these can have a major impact on the performance and maintainability of the index. In this lesson, you will learn how to create indexes and how to use some of the key index configuration options to improve the usefulness, performance, and maintainability of the indexes you create. Warning Do not index columns that are defined by using ntext, text, varchar(max), nvarchar(max), varbinary(max), xml, or image data types. Columns with these data types cannot be indexed.
  13. 13. Module 4: Creating and Tuning Indexes 4–11Overview of Creating Indexes**************************************** Illegal for non-trainer use ***************************************Introduction You can create indexes by using either SQL Server Management Studio or Transact-SQL.Creating indexes You can create an index by using Object Explorer in SQL Server Management Studio or with the CREATE INDEX Transact-SQL statement. To create an index by using Transact-SQL, use the CREATE INDEX statement. The CREATE INDEX statement has the following syntax. CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name ON { table | view } ( column [ ASC | DESC ] [ ,...n ] ) INCLUDE ( column [ ,...n ] ) [WITH [PAD_INDEX = { ON | OFF }] [[,] FILLFACTOR = fillfactor ] [[,] IGNORE_DUP_KEY = { ON | OFF }] [[,] ONLINE = { ON | OFF }] [[,] ALLOW_ROW_LOCKS = { ON | OFF }] [[,] ALLOW_PAGE_LOCKS = { ON | OFF }]] [ON {partition_scheme (column) | filegroup | “default” } ] The WITH options shown in the preceding syntax are discussed in the remainder of this lesson. For More Information The CREATE INDEX statement offers additional options not discussed here. For complete details on creating indexes by using Transact-SQL, see “CREATE INDEX (Transact-SQL)” in SQL Server Books Online. The following sample code creates an ascending nonclustered index named AK_Employee_LoginID on the LoginID column in the HumanResources.Employee table of the AdventureWorks database. CREATE NONCLUSTERED INDEX [AK_Employee_LoginID] ON [HumanResources].[Employee] ( [LoginID] ASC)
  14. 14. 4–12 Module 4: Creating and Tuning IndexesLocking options The ALLOW_ROW_LOCKS and ALLOW_PAGE_LOCKS options are new in SQL Server 2005. The following table summarizes the function of these options. Option Description ALLOW_ROW_ Controls whether row-level locks are allowed when accessing the LOCKS index. If row-level locks are not allowed, page-level and table-level locks are used instead. ALLOW_PAGE_ Controls whether page-level locks are allowed when accessing the LOCKS index. If page-level locks are not allowed, row-level and table-level locks are used instead. The database engine will choose the appropriate level of lock within the constraints configured by using the ALLOW_ROW_LOCKS and ALLOW_PAGE_LOCKS options. If both ALLOW_ROW_LOCKS and ALLOW_PAGE_LOCKS are OFF, only table-level locks will be used. Here is the preceding example with row locks disallowed. CREATE NONCLUSTERED INDEX [AK_Employee_LoginID] ON [HumanResources].[Employee] ( [LoginID] ASC) WITH ALLOW_ROW_LOCKS = OFF For More Information For more information about the ALLOW_ROW_LOCKS and ALLOW_PAGE_LOCKS options, see “Customizing Locking for an Index” in SQL Server Books Online.Online vs. offline The ONLINE option is also new in SQL Server 2005. This option specifies whether underlying tables and associated indexes are available for queries and data modification during the index operation associated with the CREATE INDEX statement. Here is the preceding example with online access enabled. CREATE NONCLUSTERED INDEX [AK_Employee_LoginID] ON [HumanResources].[Employee] ( [LoginID] ASC) WITH ONLINE = ON Note Online index operations are available only in SQL Server 2005 Enterprise Edition. For More Information For more information about the ONLINE option, see “How Online Index Operations Work” in SQL Server Books Online.
  15. 15. Module 4: Creating and Tuning Indexes 4–13What Are Unique Indexes?**************************************** Illegal for non-trainer use ***************************************What is a unique index? A unique index is an index that ensures that all data in an indexed column is unique and does not contain duplicate values. When a unique index exists, the database engine checks for duplicate values each time data is added by using an insert operation. Insert operations that would generate duplicate key values are rolled back, and the database engine displays an error message. This is true even if the insert operation changes many rows but causes only one duplicate. However, if an attempt is made to enter data for which there is a unique index and the IGNORE_DUP_KEY clause is set to ON in the CREATE INDEX statement, only the rows violating the unique index fail. When you create an index on a table that already contains data, the database engine ensures that there are no existing duplicate values. For More Information For more information about unique indexes, see “Creating Unique Indexes” in SQL Server Books Online.When to create a unique You create a unique index for clustered or nonclustered indexes when the data itself isindex inherently unique. Create unique indexes only on columns in which entity integrity can be enforced. For example, you should not create a unique index on the LastName column of the Person.Contact table in the AdventureWorks database because some employees might have the same last names. If uniqueness must be enforced, create PRIMARY KEY or UNIQUE constraints on the column instead of creating a unique index. Note If a table has a PRIMARY KEY or UNIQUE constraint, SQL Server automatically creates a unique index when you execute the CREATE TABLE or ALTER TABLE statement. By default, this will be a clustered index, but you can force it to be nonclustered by using the NONCLUSTERED option of the CREATE TABLE statement.
  16. 16. 4–14 Module 4: Creating and Tuning IndexesExample of creating a You can create a unique index in SQL Server Management Studio by selecting theunique index Unique check box in the New Index dialog box when creating the index. The following example shows the Transact-SQL required to create a unique ascending nonclustered index named AK_Employee_LoginID on the LoginID column in the HumanResources.Employee table of the AdventureWorks database. CREATE UNIQUE NONCLUSTERED INDEX [AK_Employee_LoginID] ON [HumanResources].[Employee] ( [LoginID] ASC)
  17. 17. Module 4: Creating and Tuning Indexes 4–15Considerations for Creating Indexes with Multiple Columns**************************************** Illegal for non-trainer use ***************************************What is a composite A composite index specifies more than one column as the key value. Queryindex? performance is enhanced by using composite indexes, especially when users regularly search for information in more than one way. However, wide keys increase the storage requirements of an index. Composite indexes have the following requirements and limitations: ■ Up to 16 columns can be combined to form a single composite index. ■ The sum of the lengths of the columns that make up the composite index cannot exceed 900 bytes. ■ The WHERE clause of a query must reference the first column of the composite index for the query optimizer to use the composite index. Covering indexes are not subject to this restriction. ■ The columns in a composite index must all be from the same table, except when an index is created on a view, in which case, they must all be from the same view. ■ A composite index on (column1, column2) is not the same as an index on (column2, column1)—each has a distinct column order. Note When an index contains all the columns referenced by a query, it is typically referred to as covering the query. Covered queries reduce disk I/O and can greatly improve query performance because the query optimizer can obtain all search results from the index pages without the need to get additional data from the data pages.When to create a Create composite indexes when:composite index ■ Two or more columns are best searched as a key. ■ Queries reference only the columns in the index.
  18. 18. 4–16 Module 4: Creating and Tuning IndexesGuidelines for creating When you create a composite index, consider the following guidelines:composite indexes ■ Define the most unique column first. The first column defined in the CREATE INDEX statement is referred to as the highest order. ■ Use composite indexes for tables with multiple column keys. ■ Use composite indexes to increase query performance and reduce the number of indexes that you create on a table.Included columns SQL Server 2005 allows you to specify additional table columns whose content is stored with a nonclustered index. By including nonkey columns, you can create nonclustered indexes that cover more queries. Nonkey columns have the following additional benefits: ■ They can be data types not allowed as index key columns. ■ They are not considered by the database engine when calculating the number of index key columns or index key size. For More Information For more information about included columns, see “Index with Included Columns” in SQL Server Books Online.Example of creating You can create an index with included columns by using SQL Server Managementcomposite indexes Studio. To do so, select the additional columns to include in the Include Columns section of the New Index dialog box. The following example shows the Transact-SQL required to include the ContactID and NationalIDNumber columns on an index named AK_Employee_LoginID on the LoginID column in the HumanResources.Employee table of the AdventureWorks database. CREATE UNIQUE NONCLUSTERED INDEX [AK_Employee_LoginID] ON [HumanResources].[Employee] ( [LoginID] ASC) INCLUDE ( [ContactID], [NationalIDNumber])
  19. 19. Module 4: Creating and Tuning Indexes 4–17When to Create Indexes on Computed Columns**************************************** Illegal for non-trainer use ***************************************When to create indexes You can create indexes on computed columns when:on computed columns ■ The computed column expression is deterministic and precise. Deterministic expressions always return the same result for a specified set of inputs. Precise expressions do not include float and real data types and do not include these data types in their definition. ■ The ANSI_NULLS connection-level option is ON when the CREATE TABLE statement is executed. The OBJECTPROPERTY function reports whether the option is on through the IsAnsiNullsOn property. ■ The computed column expression that is defined for the computed column cannot evaluate to the text, ntext, or image data types. ■ The connection on which the index is created—and all connections attempting INSERT, UPDATE, or DELETE statements that will change values in the index— have six SET options set to ON and one option set to OFF. The following options must be set to ON: ● ANSI_NULLS ● ANSI_PADDING ● ANSI_WARNINGS ● CONCAT_NULL_YIELDS_NULL ● QUOTED_IDENTIFIER ● ARITHABORT Note In SQL Server 2005, setting ANSI_WARNINGS to ON implicitly sets ARITHABORT to ON. In earlier versions of SQL Server, ARITHABORT must be explicitly set to ON. ■ The NUMERIC_ROUNDABORT option must be set to OFF.
  20. 20. 4–18 Module 4: Creating and Tuning Indexes Note The query optimizer ignores an index on a computed column for any SELECT statement that is executed by a connection that does not have these same option settings. For More Information For more information about included columns, see “Creating Indexes on Computed Columns” in SQL Server Books Online.
  21. 21. Module 4: Creating and Tuning Indexes 4–19What Are Partitioned Indexes?**************************************** Illegal for non-trainer use ***************************************What are partitioned In SQL Server 2005, just as you can divide tables into partitions based on value ranges,indexes? you can also partition indexes. Similar to partitioned tables, partitioned indexes are clustered or nonclustered indexes in which the index pages are separated horizontally into multiple physical locations based on a range of values in the index column. The physical locations for partitions are filegroups. You partition indexes for the same reason that you partition tables: to improve performance and to make large indexes more manageable by allowing you to focus management tasks on individual partitions rather than on the entire index.What is index alignment? An index that is partitioned in the same way as its table is referred to as being “aligned” with the table. A partitioned index is aligned if it meets the following rules: ■ The partitioning key is compatible with that of the table. ■ The index has the same number of partitions as the table. ■ The range of values stored in the partitions matches those of the table.Creating partitioned By default, creating an index on a partitioned table creates an aligned index. Thisindexes approach should meet the needs of most scenarios. However, you can also create unaligned partitioned indexes that use their own partitioning logic. Before you can do this, you must: 1. Create a partition function to specify how the index that uses the function can be partitioned. 2. Create a partition scheme to specify the placement of the partitions of a partition function on filegroups.
  22. 22. 4–20 Module 4: Creating and Tuning Indexes For More Information For more information about creating partition functions and partition schemes, see “Creating Partitioned Tables and Indexes” and “Scenarios for Choosing Tuning Options” in SQL Server Books Online.Partition keys and index When you create a unique partitioned index, you must include the partitioning key inkeys the index. This can have an impact on your database design. For example, a Sales table might use a unique OrderID field to identify sales orders, but for manageability purposes, you might want to partition the table by time span. In this case, you need to either include the OrderDate and OrderID in a composite index, create an unaligned index by using the OrderDate field, or redesign the OrderID field to include the date. When you create a partitioned clustered index, SQL Server automatically adds the partitioning key to the index. SQL Server will also add the partitioning key to nonclustered indexes created on tables that have already been partitioned. SQL Server will also automatically partition the nonclustered index to be aligned with the table if you omit specific partitioning configuration details when you create the index.
  23. 23. Module 4: Creating and Tuning Indexes 4–21Options for Incorporating Free Space in Indexes**************************************** Illegal for non-trainer use ***************************************Introduction The availability of free space in an index page can have a significant effect on the performance of index update operations. If an index record must be inserted and there is no free space, a new index page must be created and the contents of the old page split across the two pages. This can affect performance if it happens too frequently. SQL Server 2005 offers two important options that give you control over the amount of free space maintained within an index: FILLFACTOR and PAD_INDEX.The FILLFACTOR option The FILLFACTOR option allows you to allocate a percentage (0 to 100) of free space on the leaf-level index pages to reduce page splitting. This percentage determines how much the leaf-level pages should be filled. For example, a fill factor of 65 percent fills the leaf-level pages 65 percent, leaving 35 percent of the page space free for new rows. The following table shows the FILLFACTOR option settings and the typical environments in which these fill factor values are used. FILLFACTOR Leaf-level Non-leaf-level Activity on key Typical business percentage pages pages values environment 0 (default) Fill Leave room within None to light OLAP completely the upper level of modification the index tree 1–99 Fill to Leave room for Moderate to Mixed or OLTP specified one index entry heavy percentage modification 100 Fill Leave room for None to light OLAP completely one index entry modification The sysindexes system table stores the fill factor value that was last applied to an index, along with other index information. You can change the default fill factor value at the server level by using the sp_configure system stored procedure.
  24. 24. 4–22 Module 4: Creating and Tuning Indexes Note The FILLFACTOR option’s main purpose is to postpone page splitting. Therefore, it is not appropriate for tables clustered on an identity column because they are not subject to page splitting.Guidelines for setting the The fill factor value that you specify on a table depends on how often data is modifiedFILLFACTOR option (INSERT and UPDATE statements) and your organization’s environment. Generally, you should: ■ Use a low fill factor value for online transaction processing (OLTP) environments. This provides maximum room for growth in tables where rows are inserted frequently or index key values are frequently modified. ■ Use a high fill factor value for online analytical processing (OLAP) environments. For More Information For more information about the FILLFACTOR option, see “Fill Factor” in SQL Server Books Online.The PAD_INDEX option The PAD_INDEX option allows you to specify the percentage to which to fill the non- leaf-level index pages. You can use the PAD_INDEX option only when FILLFACTOR is specified because the PAD_INDEX percentage value is determined by the percentage value specified for FILLFACTOR. The following table shows the impact of FILLFACTOR option settings when you use the PAD_INDEX option and the typical environment in which PAD_INDEX values are used. FILLFACTOR Leaf-level Non-leaf-level Activity on key Typical business percentage pages pages values environment 1–99 Fill to specified Fill to specified Moderate to OLTP percentage percentage heavy modification By default, SQL Server always leaves enough room to accommodate at least one row of the maximum index size for each non-leaf-level page, regardless of the fill factor value. The number of items on the non-leaf-level index page is never fewer than two, regardless of the fill factor value. For More Information For more information about the PAD_INDEX option, see “Fill Factor” in SQL Server Books Online.Example of configuring You can configure the FILLFACTOR and PAD_INDEX settings when you create anindex free space index by using Object Explorer in SQL Server Management Studio. In the New Index dialog box, in the Options tab, select the Set Fill Factor check box. This allows you to enter a fill factor value and enables the Pad Index check box, which allows you to turn PAD_INDEX on and off. The following example shows the Transact-SQL required to set a FILLFACTOR of 65 percent and turn on the PAD_INDEX option when creating an index. CREATE UNIQUE NONCLUSTERED INDEX [AK_Employee_LoginID] ON [HumanResources].[Employee] ( [LoginID] ASC) WITH ( FILLFACTOR = 65, PAD_INDEX = ON)
  25. 25. Module 4: Creating and Tuning Indexes 4–23Methods for Obtaining Index Information**************************************** Illegal for non-trainer use ***************************************Introduction You might require information about existing indexes before you create, modify, or remove an index. SQL Server 2005 provides many ways to obtain information about indexes. These can be categorized as follows: ■ SQL Server Management Studio ■ System stored procedures ■ Catalog views ■ System functionsUsing SQL Server SQL Server Management Studio provides visual tools to view information about indexesManagement Studio to within the management environment. The following table lists the most commonlyobtain index information used tools. SQL Server Management Studio tool Description Object Explorer Allows you to navigate through the database hierarchy to view a list of the indexes that exist on a table Properties window Allows you to view the properties of any index. Accessed by right-clicking the index in Object Explorer and then clicking Properties Reports Allows you to produce database-level reports showing: ■ How the users and system use the indexes ■ The number of operations performed on the indexesUsing system stored The sp_helpindex stored procedure returns details of the indexes created on a specifiedprocedures to obtain table. The information returned for each index is the index name, description, and keys.index information The sp_help stored procedure returns more extensive details for a table, but it also includes the same information about indexes that is returned by sp_helpindex.
  26. 26. 4–24 Module 4: Creating and Tuning Indexes The following example shows how to obtain index information on the Production.Product table by using the sp_helpindex stored procedure. EXEC sp_helpindex [Production.Product] The results are shown in the following table. index_name index_description index_keys AK_Product_Name nonclustered, unique, located on Name PRIMARY AK_Product_ nonclustered, unique, located on ProductNumber ProductNumber PRIMARY AK_Product_rowguid nonclustered, unique, located on rowguid PRIMARY PK_Product_ProductID clustered, unique, primary key ProductID located on PRIMARY For More Information For more information about these stored procedures, see “sp_help (Transact-SQL)” and “sp_helpindex (Transact-SQL)” in SQL Server Books Online.Using catalog views to The following table lists the catalog views that provide information about indexes andobtain index information the type of information they provide. Catalog view Provides information about sys.indexes Index type, filegroup or partition scheme ID, and the current setting of index options that are stored in metadata sys.index_columns Column ID, position within the index, type (key or nonkey), and sort order (ASC or DESC) sys.stats Statistics associated with an index, including statistic name and whether it was created automatically or by a user sys.stats_columns Column ID associated with the statistic sys.xml_columns XML index type, primary or secondary, and the secondary type and description For More Information For more information about these catalog views, see “Viewing Index Information” in SQL Server Books Online.Using system functions The following table lists the system functions that provide information about indexesto obtain index and the type of information they provide.information Function Provides information about sys.dm_db_index_physical_stats Index size and fragmentation statistics sys.dm_db_index_operational_stats Current index and table I/O statistics sys.dm_db_index_usage_stats Index usage statistics by query type INDEXKEY_PROPERTY Index column position within the index and column sort order (ASC or DESC)
  27. 27. Module 4: Creating and Tuning Indexes 4–25Function Provides information aboutINDEXPROPERTY Index type, number of levels, and current setting of index options that are stored in metadataINDEX_COL Name of the key column of the specified indexFor More Information For more information about these functions, see “ViewingIndex Information” in SQL Server Books Online.
  28. 28. 4–26 Module 4: Creating and Tuning IndexesPractice: Creating Indexes**************************************** Illegal for non-trainer use ***************************************Goals The goal of this practice is to enable you to create an index by using SQL Server Management Studio and by using Transact-SQL.Preparation Ensure that virtual machine 2779A-MIA-SQL-04 is running and that you are logged on as Student. If a virtual machine has not been started, perform the following steps: 1. Close any other running virtual machines. 2. Start the virtual machine. 3. In the Log On to Windows dialog box, complete the logon procedure by using the user name Student and the password Pa$$w0rd.To create an index by Perform the following steps to create an index by using SQL Server Managementusing SQL Server Studio:Management Studio 1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, and then click SQL Server Management Studio. 2. In the Connect to Server dialog box, specify the values in the following table, and then click Connect. Property Value Server type Database Engine Server name MIAMI Authentication Windows Authentication 3. If Object Explorer is not visible, click Object Explorer on the View menu. 4. In Object Explorer, expand Databases, AdventureWorks, Tables, Person.Contact, and Indexes. 5. Right-click Indexes, and then click New Index.
  29. 29. Module 4: Creating and Tuning Indexes 4–27 6. In the New Index dialog box, enter the following values. Property Value Index name IX_Contact_LastName_FirstName Index type Nonclustered 7. Click the Add button, select the FirstName and LastName columns, and then click OK. 8. Click LastName in the Index key columns list, and then click the Move Up button to move LastName above FirstName. 9. Click the Options page on the left in the New Index dialog box. 10. Select the Set fill factor check box, and enter a fill factor of 65. 11. Select the Allow online processing of DML statements while creating the index check box. 12. Click the Included Columns page on the left in the New Index dialog box. 13. Click the Add button, select the Title, MiddleName, and Suffix columns, and then click the OK button. 14. Click OK in the New Index dialog box to create the index. 15. When the command has completed, right-click the Indexes folder in Object Explorer, and then click Refresh to verify that the IX_Contact_LastName_FirstName index has been created. 16. Keep SQL Server Management Studio open. You will use it in the next procedure.To create an index by Perform the following steps to create an index by using Transact-SQL:using Transact-SQL 1. In SQL Server Management Studio, click the New Query button on the toolbar. 2. In the new, blank query window, type the following Transact-SQL code. USE AdventureWorks CREATE NONCLUSTERED INDEX IX_Contact_LastName_FirstName_Transact ON Person.Contact (LastName ASC,FirstName ASC) INCLUDE (Title,MiddleName,Suffix) WITH (FILLFACTOR = 65, ONLINE = ON) 3. Click the Execute button on the toolbar. 4. When the command has completed successfully, right-click the Indexes folder in Object Explorer, and then click Refresh to verify that the IX_Contact_LastName_FirstName_Transact index has been created. 5. Close SQL Server Management Studio. Click No if prompted to save files.
  30. 30. 4–28 Module 4: Creating and Tuning IndexesLesson 3: Optimizing Indexes**************************************** Illegal for non-trainer use ***************************************Lesson objectives After completing this lesson, students will be able to: ■ Describe the Database Engine Tuning Advisor. ■ Define index fragmentation. ■ Describe the considerations for defragmenting indexes.Introduction The effectiveness of your indexes plays a major role in the overall performance of your database. Therefore, it is important to make sure that your indexes are designed and implemented from the outset in a way that best supports your applications. Once the indexes are implemented, you must maintain the indexes to ensure their continued optimal performance. As data is added, changed, and deleted in the database, indexes become fragmented. Depending on your business environment and the purpose of the database application, fragmentation can be either good or bad for performance, but it needs to be managed appropriately to suit your needs. This lesson looks at the main tools provided in SQL Server 2005 that help you to optimize your index design and maintain those indexes to control fragmentation at an appropriate level. For More Information For more information about index optimization, see “Optimizing Indexes” in SQL Server Books Online.
  31. 31. Module 4: Creating and Tuning Indexes 4–29What Is the Database Engine Tuning Advisor?**************************************** Illegal for non-trainer use ***************************************Introduction The Database Engine Tuning Advisor is a new tool in SQL Server 2005. Prior to SQL Server 2005, some Database Engine Tuning Advisor functionality was provided by the Index Tuning Wizard. The Database Engine Tuning Advisor evaluates more types of events and structures and provides higher quality recommendations. The Database Engine Tuning Advisor provides two interfaces: ■ A stand-alone graphical user interface tool for tuning databases and viewing tuning recommendations and reports. ■ A command-line utility (dta.exe) that provides access to Database Engine Tuning Advisor functionality from the command line and scripts. Caution On first use, the Database Engine Tuning Advisor must be initialized by a user with system administrator permissions. After a user with system administrator permissions has initialized the Database Engine Tuning Advisor, any users that are members of the db_owner fixed database role can use the Database Engine Tuning Advisor to tune tables on databases that they own.How the Database The Database Engine Tuning Advisor is a tool that analyzes the performance effects ofEngine Tuning Advisor workloads run against one or more databases. A workload is a set of Transact-SQLworks statements that executes against databases that you want to tune. The workload source can be a file containing Transact-SQL statements, a trace file generated by SQL Profiler, or a table of trace information, again generated by SQL Profiler.
  32. 32. 4–30 Module 4: Creating and Tuning Indexes Analysis can be performed in either of the following two modes. Analysis mode Description Evaluate In evaluate mode, the Database Engine Tuning Advisor compares the cost of the current configuration (C) with that of a user- specified configuration (U), for the same workload. C is always a real configuration because it consists of physical design structures that currently exist in the database. In comparison, U is a configuration that consists of real and hypothetical physical design structures. If the Database Engine Tuning Advisor reports that the cost of U is lower than the cost of C, it is likely that the physical design of U will perform better than C will. Tune In tune mode, a database administrator already knows that a part of the database physical design should be fixed but wants the Database Engine Tuning Advisor to recommend the best physical design structures for the rest of the configuration. After analyzing the effects of a workload on your databases, the Database Engine Tuning Advisor provides its recommendations. These recommendations include suggested changes to the database such as new indexes, indexes that should be dropped, and depending on the tuning options you set, partitioning recommendations. Recommendations are provided as a set of Transact-SQL statements that would effect the suggested changes. You can view the Transact-SQL and save it for later review and application, or you can choose to implement the recommended changes immediately. For More Information For more information about the Database Engine Tuning Advisor, see “Tuning the Physical Database Design” in SQL Server Books Online.
  33. 33. Module 4: Creating and Tuning Indexes 4–31Practice: Using the Database Engine Tuning Advisor**************************************** Illegal for non-trainer use ***************************************Goals The goal of this practice is to use the Database Engine Tuning Advisor to analyze a database, provide index recommendations, and apply recommendations.Preparation Ensure that virtual machine 2779A-MIA-SQL-04 is running and that you are logged on as Student. If a virtual machine has not been started, perform the following steps: 1. Close any other running virtual machines. 2. Start the virtual machine. 3. In the Log On to Windows dialog box, complete the logon procedure by using the user name Student and the password Pa$$w0rd.To analyze a database by Perform the following steps to use the Database Engine Tuning Advisor to analyzeusing the Database a database:Engine Tuning Advisor 1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, point to Performance Tools, and then click Database Engine Tuning Advisor. 2. In the Connect to Server dialog box, specify the values in the following table, and then click Connect. Property Value Server type Database Engine Server name MIAMI Authentication Windows Authentication 3. In the Session name box, type Module4. 4. Under Workload, ensure that File is selected, and then click the Browse for a workload file button. 5. Navigate to the D:Practices folder.
  34. 34. 4–32 Module 4: Creating and Tuning Indexes 6. Click Workload.sql, and then click Open. 7. Select AdventureWorks in the Database for workload analysis list. 8. In the Select databases and tables to tune section, select the AdventureWorks database. 9. Click the drop-down arrow in the Selected Tables column for the AdventureWorks database. 10. Clear the check box in the Name column header to clear all tables, and then individually select the SalesOrderDetail and SalesOrderHeader tables. 11. Click the drop-down arrow in the Selected Tables column for the AdventureWorks database again to hide the list of tables. 12. Click the Tuning Options tab. 13. Clear the Limit tuning time check box. 14. Select Do not keep any existing PDS. 15. Click Advanced Options. 16. Select Generate online recommendations where possible. 17. Click OK to close the Advanced Tuning Options dialog box. 18. On the Actions menu, click Start Analysis, and then wait for the analysis to be completed. 19. Keep the Database Engine Tuning Advisor open. You will need it in the next procedure.To review Database Perform the following steps to review the recommendations made by the DatabaseEngine Tuning Advisor Engine Tuning Advisor:recommendations 1. In the Recommendations window, ensure that the Show existing objects check box under the list of indexes is cleared. 2. Click the Reports tab. 3. Review the information in the Tuning Summary pane, and note the Expected percentage improvement value. 4. In the Select report list, click Index usage report (current). 5. Scroll to the right until you can see the Percent Usage column. 6. In the Select report list, click Index usage report (recommended). 7. Scroll to the right until you can see the Percent Usage column. Note the differences between this and the previous report. 8. Click the Recommendations tab. 9. Scroll to the right until you can see the Definition column. 10. Click the first definition, and then review the Transact-SQL. Notice the Copy to Clipboard button. 11. Click Close to close the SQL Script Preview window. 12. Close the Database Engine Tuning Advisor.
  35. 35. Module 4: Creating and Tuning Indexes 4–33Index Fragmentation**************************************** Illegal for non-trainer use ***************************************What is fragmentation? Index fragmentation is the inefficient use of pages within an index. Fragmentation occurs over time as data is modified. For example, when rows of data are added to or deleted from a table, or when values in the indexed columns are changed, SQL Server adjusts the index pages to accommodate the changes and to maintain the storage of the indexed data. The adjustment of the index pages is known as a page split. The splitting process increases the size of a table and the time that is needed to process queries.Types of fragmentation There are two types of index fragmentation, external fragmentation and internal fragmentation, as described in the following table. Type of fragmentation Description Internal Inefficient use of pages within an index because the amount of data stored within each page is less than the data page can contain. Internal fragmentation results in increased logical and physical I/O, and more memory is required to store the rows in cache. These extra reads can lead to degradation in query performance. However, insert- intensive tables can benefit from internal fragmentation because there is less chance that an insert will cause a page spilt. External Inefficient use of pages within an index because the logical order of the pages is wrong. External fragmentation slows disk access to the rows as a result of disk head seeking and is never beneficial.Detecting fragmentation You can use SQL Server Management Studio or the sys.dm_db_index_physical_stats dynamic management function to determine the extent to which your indexes are fragmented.
  36. 36. 4–34 Module 4: Creating and Tuning Indexes To view details of index fragmentation in SQL Server Management Studio, open the Properties window for the index you are interested in, and then click the Fragmentation page. In addition to some general properties about the pages in the index, the Properties window shows the average page fullness and the total fragmentation across the index as a percentage. The higher the value, the more fragmented the index. You can use sys.dm_db_index_physical_stats to view fragmentation in a specific index, all indexes on a table or indexed view, all indexes in a database, or all indexes in all databases. The arguments passed to the sys.dm_db_index_physical_stats function include the IDs of the database, table, index, and partition that you want to evaluate. The result set of the function includes the avg_fragmentation_in_percent column, which shows the average index fragmentation as a percentage (the same figure shown in the index’s Properties window in SQL Server Management Studio). For More Information For more information about the sys.dm_db_index_physical_stats function, see “sys.dm_db_index_physical_stats” in SQL Server Books Online. The following example code shows how to obtain the average fragmentation of all the indexes on the Production.Product table by using sys.dm_db_index_physical_stats. SELECT a.index_id, name, avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats (DB_ID(NAdventureWorks), OBJECT_ID(NProduction.Product), NULL, NULL, NULL) AS a JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id; The results will look similar to this. index_id name avg_fragmentation_in_percent 1 PK_Product_ProductID 23.0769230769231 2 AK_Product_ProductNumber 50 3 AK_Product_Name 66.6666666666667 4 AK_Product_rowguid 50
  37. 37. Module 4: Creating and Tuning Indexes 4–35Options for Defragmenting Indexes**************************************** Illegal for non-trainer use ***************************************Options for There are two options for defragmenting an index: reorganizing and rebuilding.defragmenting indexes Reorganizing an index defragments the leaf level of clustered and nonclustered indexes on tables by physically reordering the leaf-level pages to match the logical order (left to right) of the leaf nodes. Having the pages in order improves index-scanning performance. The index is reorganized within the existing pages allocated to it; no new pages are allocated. If an index spans more than one file, the files are reorganized one at a time. Pages do not migrate between files. Reorganizing an index also compacts the index pages. Any empty pages created by this compaction are removed, providing additional available disk space. Compaction is based on the fill factor value in the sys.indexes catalog view. Rebuilding an index drops the index and creates a new one. In doing this, fragmentation is removed, disk space is reclaimed by compacting the pages using the specified or existing fill factor setting, and the index rows are reordered in contiguous pages (allocating new pages as needed). This can improve disk performance by reducing the number of page reads required to obtain the requested data. For More Information For more information about defragmentation of indexes, see “Reorganizing and Rebuilding Indexes” in SQL Server Books Online.Reorganizing vs. The decision as to whether you reorganize or rebuild your index to removerebuilding an index fragmentation should be based on the level of fragmentation that exists in the index as reported by SQL Server Management Studio or sys.dm_db_index_physical_stats. The following table provides guidance on the best approach to removing varying degrees of fragmentation. avg_fragmentation_in_percent Action < = 30% Reorganize > 30% Rebuild
  38. 38. 4–36 Module 4: Creating and Tuning Indexes Note If fragmentation is less than 30 percent and reorganizing the index provides little improvement, you should try to rebuild the index.Reorganizing an index You can use the REORGANIZE clause of the ALTER INDEX statement to defragment indexes. The ALTER INDEX statement with the REORGANIZE clause replaces the DBCC INDEXDEFRAG statement in earlier versions of SQL Server. The following code example shows the syntax of the ALTER INDEX statement when it is used to reorganize the AK_Product_Name index of the Production.Product table. ALTER INDEX AK_Product_Name ON Production.Product REORGANIZE You can also reorganize all of the indexes on a table, as shown in the following example. ALTER INDEX ALL ON Production.Product REORGANIZERebuilding an index You can use the REBUILD clause of the ALTER INDEX statement to defragment indexes. The ALTER INDEX statement with the REBUILD clause replaces the DBCC DBREINDEX statement in earlier versions of SQL Server. The following example shows the syntax of the ALTER INDEX statement when it is used to rebuild the AK_Product_Name index of the Production.Product table. ALTER INDEX AK_Product_Name ON Production.Product REBUILD
  39. 39. Module 4: Creating and Tuning Indexes 4–37Practice: Defragmenting Indexes**************************************** Illegal for non-trainer use ***************************************Goals The goal of this practice is to enable you to identify fragmentation in an index and to defragment the index by either reorganizing it or rebuilding it.Preparation Ensure that virtual machine 2779A-MIA-SQL-04 is running and that you are logged on as Student. If a virtual machine has not been started, perform the following steps: 1. Close any other running virtual machines. 2. Start the virtual machine. 3. In the Log On to Windows dialog box, complete the logon procedure by using the user name Student and the password Pa$$w0rd.To identify Perform the following steps to identify which indexes on thefragmentation in an HumanResources.Employee table contain fragmentation:index 1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, and then click SQL Server Management Studio. 2. In the Connect to Server dialog box, specify the values in the following table, and then click Connect. Property Value Server type Database Engine Server name MIAMI Authentication Windows Authentication 3. In SQL Server Management Studio, click the New Query button on the toolbar. 4. In the new, blank query window, type the following Transact-SQL code. USE AdventureWorks SELECT a.index_id as [Index ID], name as [Index Name],
  40. 40. 4–38 Module 4: Creating and Tuning Indexes avg_fragmentation_in_percent as Fragmentation FROM sys.dm_db_index_physical_stats (DB_ID(NAdventureWorks), OBJECT_ID(HumanResources.Employee),NULL, NULL, NULL) AS a JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id ORDER BY Fragmentation DESC 5. Click the Execute button on the toolbar. 6. When the command has completed, review the results, and identify an index that contains a high level of fragmentation—for example, the PK_Employee_EmployeeID index. 7. Keep SQL Server Management Studio open. You will use it in the next procedure.To defragment an index Perform the following steps to defragment the PK_Employee_EmployeeID index: 1. In SQL Server Management Studio, click the New Query button on the toolbar. 2. In the new, blank query window, type the following Transact-SQL code. USE AdventureWorks ALTER INDEX PK_Employee_EmployeeID ON HumanResources.Employee REBUILD 3. Click the Execute button on the toolbar. 4. When the command has completed, rerun the previous query to report the levels of fragmentation, and review the changed results. 5. Close SQL Server Management Studio. Click No if prompted to save files.
  41. 41. Module 4: Creating and Tuning Indexes 4–39Lesson 4: Creating XML Indexes**************************************** Illegal for non-trainer use ***************************************Lesson objectives After completing this lesson, students will be able to: ■ Describe XML indexes. ■ Identify types of XML indexes.Introduction SQL Server 2005 provides extensive native support for Extensible Markup Language (XML) data. XML instances are stored in the xml type columns as large objects (LOBs). These XML instances can be large, and the stored binary representation of xml data type instances can be up to 2 gigabytes (GB). Such large amounts of data can have a significant impact on the performance of queries; therefore, SQL Server 2005 supports XML indexes to speed up these operations. This lesson describes what XML indexes are and the different types of XML indexes. It describes the considerations you must take into account when creating XML indexes and the syntax used to create the XML indexes.
  42. 42. 4–40 Module 4: Creating and Tuning IndexesWhat Are XML Indexes?**************************************** Illegal for non-trainer use ***************************************What are XML indexes? XML indexes are indexes on table columns that contain xml data types. When a query includes an xml column, the query processor must parse the XML each time the query is executed. You can significantly improve the performance of that query by creating an XML index on that column. When you create an XML index, SQL Server creates a clustered B-tree representation of the nodes in the XML data and stores it in an internal table. The XML index is clustered on the table’s primary key. When you issue queries including the indexed xml column, the query optimizer will use the indexed representation in all situations unless you need to return the document, in which case, the XML itself will be returned.Requirements for using Before using an XML index, you must be aware of the following:XML indexes ■ You must create a clustered primary key on the table containing the indexed xml column before you can create an XML index. ■ You cannot change the clustered primary key of a table if an XML index exists on the table. You will have to drop all XML indexes on the table before modifying the primary key. ■ You can create one primary XML index per xml column. ■ You cannot have an XML index and a non-XML index on the same table with the same name. ■ You cannot use the IGNORE_DUP_KEY and ONLINE options when you create an XML index. ■ You cannot create an XML index on an xml column in a view, on a table-valued variable with xml columns, or on xml variables. ■ You must drop an XML index on an xml column before using the ALTER TABLE statement to change the index from typed to untyped or vice versa.
  43. 43. Module 4: Creating and Tuning Indexes 4–41 ■ You must set the ARITHABORT option to ON when an XML index is created and when any data modification operation (INSERT, UPDATE, or DELETE) is performed on the xml column.For More Information For more information about XML indexes, see “Indexes onxml Data Type Columns” in SQL Server Books Online.
  44. 44. 4–42 Module 4: Creating and Tuning IndexesTypes of XML Index**************************************** Illegal for non-trainer use ***************************************Two types of XML XML indexes fall into two categories:indexes ■ Primary XML index. This is a clustered B-tree representation of the nodes in the XML data. The first index on an xml type column must be the primary XML index. ■ Secondary XML index. This is a nonclustered index of the primary XML index. A primary XML index must exist before any secondary index can be created. There are three types of secondary XML index: ● Path indexes. Improve the performance of queries that use paths and values to select data ● Property indexes. Improve the performance of queries that use paths to select data ● Value indexes. Improve the performance of queries that use imprecise paths to select data A primary XML index will always improve query performance if an xml column contains anything but trivial XML documents. Secondary XML indexes will provide additional performance benefits depending on the types of queries you make against the xml column.Primary XML indexes Use the CREATE PRIMARY XML INDEX statement to create a primary XML index on an xml column, as shown in the following example. CREATE PRIMARY XML INDEX PXML_ProductModel_CatalogDescription ON Production.ProductModel (CatalogDescription)Path XML indexes You should consider creating a secondary path index if you intend to execute queries that retrieve data from the xml column by specifying a path and value—for example, if you intend to execute a query that checks for the existence of an Extensible Query Language (XQuery) expression such as /ItemList/Item[@ProductID="1"].

×