Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Module08

428 views

Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

Module08

  1. 1. 8.1 : Indexes SQL Server 2005
  2. 2. <ul><li>Index </li></ul><ul><li>Need of an index </li></ul><ul><li>Types of indexes </li></ul><ul><li>To Work with an index </li></ul><ul><li>To Create indexes </li></ul><ul><li>To Drop indexes </li></ul>Contents
  3. 3. Objectives <ul><li>At the end of this presentation you should be able to: </li></ul><ul><li>Understand the concept of indexes </li></ul><ul><li>Understand why an index is required in a database </li></ul><ul><li>Understand the different types of indexes </li></ul><ul><li>Working with an index </li></ul><ul><li>Creating and dropping indexes </li></ul>
  4. 4. What is Index <ul><li>Indexes are used to speed up data retrieval </li></ul><ul><li>Indexes also enforce uniqueness of rows. </li></ul><ul><li>An index is an internal table structure that SQL server 2005 uses to provide quick access to rows in a table based on the values of one or more columns. </li></ul>
  5. 5. Need of Indexes <ul><li>It Improves the speed of execution of queries. </li></ul><ul><li>It speeds joins between tables </li></ul><ul><li>They accelerate queries that join tables, and perform sorting and grouping. </li></ul><ul><li>It can be used to enforce uniqueness of rows </li></ul><ul><li>They are useful on columns in which the majority of data is unique. An index on columns containing large amount of duplicate data is not useful. </li></ul><ul><li>When user modifies the data of an indexed column, the associated indexes are updated automatically. </li></ul>
  6. 6. Types of indexes <ul><li>Indexes can be classified as </li></ul><ul><ul><li>Clustered Index </li></ul></ul><ul><ul><li>Non Clustered Index </li></ul></ul><ul><li>Clustered Index : The data is physically sorted. Only one clustered index can be created per table. So user should build it on attributes that have a high percentage of unique values and whose values do not change often. </li></ul>
  7. 7. Types of Index (Continued) <ul><li>Non Clustered Index : The physical order of the rows is not the same as that of the index order. </li></ul><ul><li>Non clustered indexes have to be built on attributes which are used in joins and the WHERE clause. </li></ul><ul><li>These are values which may change often </li></ul><ul><li>SQL server 2005 creates non clustered indexes by default when the CREATE INDEX command is given </li></ul><ul><li>There can be as many as 249 non clustered indexes per table . </li></ul>
  8. 8. To Work with Index <ul><li>Creating Index </li></ul><ul><li>Dropping Index </li></ul><ul><li>TO Create Index: User can create an index using the create index statement </li></ul>CREATE [UNIQUE] [CLUSTERED/NONCLUSTERED]INDEX index_name ON table_name (column_name [, column_name]…)
  9. 9. To Drop Index <ul><li>It removes one or more indexes from the current database. </li></ul><ul><li>The DROP INDEX statement does not apply to indexes created by defining PRIMARY KEY or UNIQUE constraints (created by using the PRIMARY KEY or UNIQUE options of either the CREATE TABLE or ALTER TABLE statements, respectively). </li></ul><ul><li>Syntax </li></ul>DROP INDEX 'table.index | view.index' [ ,...n ]
  10. 10. Example 1 - Index <ul><li>User can create the Index using Create Index statement </li></ul><ul><li>Create index pk1_Index on employees (EmployeeID) </li></ul><ul><li>( creates the index PK1_index on employees table) </li></ul><ul><li>User can view the index using statements </li></ul><ul><li>sp_help Employees </li></ul><ul><li>sp_helpindex Employees </li></ul><ul><li>User can drop the index using command </li></ul><ul><li>drop index employees.pk1_index </li></ul>
  11. 11. Example 2 Index <ul><li>CREATE TABLE publishers1 </li></ul><ul><li>( </li></ul><ul><li>pub_id char(4) NOT NULL CONSTRAINT UPKCL_pubind1 PRIMARY KEY CLUSTERED </li></ul><ul><li>CHECK (pub_id IN ('1389', '0736', '0877', '1622', '1756') OR pub_id LIKE '99[0-9][0-9]'), </li></ul><ul><li>pub_name varchar(40) NULL, </li></ul><ul><li>city varchar(20) NULL, </li></ul><ul><li>state char(2) NULL, </li></ul><ul><li>country varchar(30) NULL DEFAULT('USA') </li></ul><ul><li>) </li></ul>
  12. 12. Key Points <ul><li>An index is an internal table structure that SQL </li></ul><ul><li>server uses to provide quick access to rows. </li></ul><ul><li>Improves the speed of execution of queries. </li></ul><ul><li>When users modify the data of an indexed column the associated indexes are updated automatically. </li></ul><ul><li>Indexes can be classified as: </li></ul><ul><ul><li>Clustered Index </li></ul></ul><ul><ul><li>Non Clustered Index </li></ul></ul>
  13. 13. Key Points (Continued) <ul><li>In clustered Index, the data is physically sorted. Only one clustered index can be created per table. </li></ul><ul><li>The non-clustered indexes do not physically order the data on the data pages. </li></ul><ul><li>Drop Index statement is used for removing one or more indexes from the current database. </li></ul>
  14. 14. Activity Time (30 minutes) <ul><li>Activity: 8.1 </li></ul><ul><li>Create a unique clustered index on the Publisher_ID of the Publisher table. And list the index created. </li></ul><ul><li>Activity: 8.2 </li></ul><ul><li>Create a nonclustered index on pub_name. </li></ul><ul><li>Activity: 8.3 </li></ul><ul><li>Drop the index created in the Activity 1 and check to see whether the index is dropped. </li></ul>
  15. 15. Activity Time (30 minutes) continued <ul><li>Activity: 8.4 </li></ul><ul><li>Create and rebuild the index created in the Activity 1 to add another column Publisher_Name without dropping the index. (Which is called as a composite index; the index created on 2 or more columns). </li></ul><ul><li>Hint: to rebuild an index without dropping, use WITH DROP_EXISTING keyword used with the Create INDEX statement. </li></ul>
  16. 16. Questions & Comments

×