Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.

Similar presentations


Presentation on theme: "SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index."— Presentation transcript:

1 SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index * Use the Index Tuning Wizard * Understand Index Enhancements

2 SQL/Lesson 7/Slide 2 of 32 Implementing Indexes Getting Started *An index is an internal table structure that SQL Server uses to provide quick access to rows of a table based on the values of one or more columns *Advantages of Using Indexes 3Improve the speed of the execution of queries 3Enforce uniqueness of data i.e. Indexes can enforce entity integrity. 3Indexes can improve the performance of queries that use joins to retrieve tables.

3 SQL/Lesson 7/Slide 3 of 32 Implementing Indexes Getting Started (Contd.) *Disadvantages of Using Indexes 3Takes time to create an index  Takes large amount of disk space to store data along with the original data source—the table 3Gets updated each time the data is modified *Types of Indexes 3Clustered index 3Nonclustered index

4 SQL/Lesson 7/Slide 4 of 32 Implementing Indexes Getting Started (Contd.) *Clustered Index 3In a clustered index: ä The data is physically sorted ä Only one clustered index can be created per table *Nonclustered Index 3In a nonclustered index: ä The physical order of the rows is not the same as the index order

5 SQL/Lesson 7/Slide 5 of 32 Implementing Indexes Getting Started (Contd.) ä Nonclustered indexes are typically created on columns used in joins and WHERE clauses, and whose values may be modified frequently ä SQL Server creates nonclustered indexes by default when the CREATE INDEX command is given ä There can be as many as 249 nonclustered indexes per table

6 SQL/Lesson 7/Slide 6 of 32 Implementing Indexes Getting Started (Contd.) *Indexes and Heap Structures 3SQL Server supports indexes defined on any column in a table, including computed columns ä If a table does not have any clustered index, data is not stored in a particular order. This structure is called a heap

7 SQL/Lesson 7/Slide 7 of 32 Implementing Indexes Getting Started (Contd.) *Features of Indexes 3Indexes accelerate queries that join tables, and perform sorting and grouping. 3Indexes can be used to enforce uniqueness of rows. 3Indexes are useful on columns in which the majority of data is unique. 3When you modify the data of an indexed column, the associated indexes are updated automatically. 3You require time and resources to maintain indexes. You should not create an index that is not used frequently.

8 SQL/Lesson 7/Slide 8 of 32 Implementing Indexes Getting Started (Contd.) 3A clustered index should be created before a nonclustered index. A clustered index changes the order of rows. A nonclustered index would need to be rebuilt if it is built before a clustered index  Typically, nonclustered indexes are created on foreign keys. *Syntax CREATE [UNIQUE] [CLUSTERED|NONCLUSTERED] INDEX index_name ON table_name(column_name[,column_name]…)

9 SQL/Lesson 7/Slide 9 of 32 Implementing Indexes Optimizing Query Execution *The Employee table contains a large amount of data. The first name of each employee and the name of the Department are required to create a report. However, it takes a long time to execute the following query. SELECT vFirstName, cDeptName FROM Employee JOIN Department ON Employee.cDeptno = Department.cDeptno Suggest and implement a solution for faster data retrieval.

10 SQL/Lesson 7/Slide 10 of 32 Implementing Indexes Task List *Identify how to speed up data retrieval *Draft the statement to create an index *Create the index in the database *Verify that the index has been created *Verify that the query execution is faster

11 SQL/Lesson 7/Slide 11 of 32 Implementing Indexes Identify how to speed up data retrieval *Indexes are used to: 3Speed up data retrieval 3Enforce the uniqueness of rows *Result: 3To speed up data retrieval, use indexes

12 SQL/Lesson 7/Slide 12 of 32 Implementing Indexes Draft the statement to create an index * Action: 3 The tables on which the index would be created are: Department and Employee 3 The attributes on which the index would be created are: cDeptNo of Employee and cDeptNo of Department 3 The types of indexes to be created are: Employee - Nonclustered index; Department - Clustered index

13 SQL/Lesson 7/Slide 13 of 32 Implementing Indexes Create the index in the database *Action: 3 In the Query Analyzer window, type: CREATE NONCLUSTERED INDEX idxdno ON Employee(cDeptNo) CREATE CLUSTERED INDEX idxDeptDno ON Department(cDeptno) 3Press F5 to execute the code

14 SQL/Lesson 7/Slide 14 of 32 Implementing Indexes Verify that the index has been created *To verify that the index has been created, use the sp_helpindex command *Syntax sp_helpindex table_name *Action: 3 In the Query Analyzer window, type: sp_helpindex Employee sp_helpindex Department

15 SQL/Lesson 7/Slide 15 of 32 Implementing Indexes Verify that the query execution is faster *Action: 3Execute the query after creating the index. If there is a lot of data, you can note the difference in speed

16 SQL/Lesson 7/Slide 16 of 32 Implementing Indexes Wait a while… * How many clustered indexes can be created per table? * Which index organizes data logically but does not store data physically?

17 SQL/Lesson 7/Slide 17 of 32 Implementing Indexes Composite Indexes *It is simply an index that has more than one column in its key. *Helpful wit queries that search for rows based on all of the values in multiple columns, such as LastName & FirstName. *Up to 16 columns can be used in an index. The combined size of the columns cannot exceed 900 bytes. *SQL Server doesn’t use the composite index unless the first column in the key is specified. *When building a composite index, use the column with the most unique values as the first column.

18 SQL/Lesson 7/Slide 18 of 32 Implementing Indexes Unique Indexes *A unique index will ensure that the values must be unique within the table. *Unique indexes are how SQL Server enforces PRIMARY KEY & UNIQUE constraints for entity integrity.

19 SQL/Lesson 7/Slide 19 of 32 Implementing Indexes Index Tuning Wizard *Index Tuning Wizard available in SQL Server is used to select and create the best possible set of indexes and information regarding a database * Uses of the Index Tuning Wizard 3 For a given workload, the best possible combination of indexes for a database is recommended 3 The effects of the proposed recommendation about the indexes, distribution of queries among tables, and the query performance in the workload will be analyzed 3 For a small set of problem queries, the way to tune the database will be recommended 3 It will specify the advanced options such as disk space constraints that can be customized

20 SQL/Lesson 7/Slide 20 of 32 Implementing Indexes Index Enhancements *Fill Factor 3FILLFACTOR clause improves performance of the system by minimizing the amount of page splitting that occurs each time an index page becomes full 3Syntax CREATE CLUSTERED INDEX index_name ON table_name (column_name) WITH FILLFACTOR = percentage_fillfactor *Pad_Index 3Specifies the space to leave open on each page (node) in the intermediate levels of the index

21 SQL/Lesson 7/Slide 21 of 32 Implementing Indexes Index Enhancements (Contd.) * Implications of NULL in Unique Indexes: 3In a table, a unique index cannot be created on a single column if that column contains NULL in more than one row * DBCC SHOWCONTIG: 3The DBCC SHOWCONTIG command is primarily used to find out why the table or the index is heavily fragmented 3Syntax DBCC SHOWCONTIG [ (table_id [, index_id]) ]

22 SQL/Lesson 7/Slide 22 of 32 Implementing Indexes Index Enhancements (Contd.) * The DBCC INDEXDEFRAG: 3 The DBCC INDEXDEFRAG command is used to defragment clustered and secondary indexes of the specified table or view 3 Syntax DBCC INDEXDEFRAG

23 SQL/Lesson 7/Slide 23 of 32 Implementing Indexes Wait a while… * Neha wants to minimize the amount of page splitting that occurs each time an index page is full. What should she use?

24 SQL/Lesson 7/Slide 24 of 32 Implementing Indexes Performance Considerations (Contd.) * Index Usage Criteria: 3 SQL Server cannot use an index until and unless the query contains a column in a valid search argument or join clause that matches at least the first column of the index

25 SQL/Lesson 7/Slide 25 of 32 Implementing Indexes *Choosing columns for Index *Primary keys and foreign keys should always be indexed. *Create indexes on columns that you often search on. *Create indexes on columns that are often used to sort the results of a query. *Create unique indexes on a primary key or alternate key. *If you often retrieve data in sorted order by a particular column, consider putting a clustered index on that column. *Consider using a non-clustered index on the primary key of a table if the table uses an IDENTITY column as its primary key.

26 SQL/Lesson 7/Slide 26 of 32 Implementing Indexes Summary In this lesson, you learned that: * Indexes are created to enhance the performance of queries. * There are two types of indexes – clustered and nonclustered. * Indexes are created using the CREATE INDEX statement. * Data is physically sorted in a clustered index. * Clustered indexes should be built on an attribute whose values are unique and do not change often. * In a nonclustered index, the physical order of rows is not the same as that of the index order.

27 SQL/Lesson 7/Slide 27 of 32 Implementing Indexes Summary (Contd.) * A nonclustered index should be built on an attribute which is normally used in joins and the WHERE clause. The values of this attribute may often change. * A nonclustered index is the default index that is created with the CREATE INDEX command. *The Index Tuning Wizard can be used to analyze the optimal use of indexes in the query entered in the Query Analyzer window. *SQL Server provides the FILLFACTOR clause to improve performance of the system by minimizing the amount of page splitting that occurs each time an index page becomes full.

28 SQL/Lesson 7/Slide 28 of 32 Implementing Indexes Summary (Contd.) *The DBCC SHOWCONTIG command is mainly used to find out whether the table or index is heavily fragmented. Table fragmentation normally occurs when a large number of insert and update operations are performed on the table. *The DBCC INDEXDEFRAG command is used to defragment clustered and secondary indexes of the specified table or view.


Download ppt "SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index."

Similar presentations


Ads by Google