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

More Related Content

PPTX
Oracle Index
PPTX
Introduction of sql server indexing
PDF
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
PPTX
SQL Server Index and Partition Strategy
DOCX
Index in sql server
PPTX
Sql server ___________session_17(indexes)
PDF
Database Indexes
PPTX
V25 sql index
Oracle Index
Introduction of sql server indexing
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
SQL Server Index and Partition Strategy
Index in sql server
Sql server ___________session_17(indexes)
Database Indexes
V25 sql index

What's hot (18)

PDF
Oracle SQL Part 2
PDF
Oracle SQL Part 3
PPT
4) databases
PPTX
Technical stream presentation
PPTX
Oracle SQL - Select Part -1 let's write some queries!
PDF
SQLite Database Tutorial In Android
PPT
Excel 2007 Unit F
PPTX
Oracle SQL - Grants, filters, groups and more
PPTX
Data Handning with Sqlite for Android
PDF
Waiting too long for Excel's VLOOKUP? Use SQLite for simple data analysis!
PDF
Access tips access and sql part 1 setting the sql scene
PPTX
Relational Database Management System
PPT
Excel 2007 Unit N
PPTX
MySQL index optimization techniques
ODP
Ms sql-server
PDF
IBM SPSS Statistics Interface - Navigation
PDF
Steps towards of sql server developer
Oracle SQL Part 2
Oracle SQL Part 3
4) databases
Technical stream presentation
Oracle SQL - Select Part -1 let's write some queries!
SQLite Database Tutorial In Android
Excel 2007 Unit F
Oracle SQL - Grants, filters, groups and more
Data Handning with Sqlite for Android
Waiting too long for Excel's VLOOKUP? Use SQLite for simple data analysis!
Access tips access and sql part 1 setting the sql scene
Relational Database Management System
Excel 2007 Unit N
MySQL index optimization techniques
Ms sql-server
IBM SPSS Statistics Interface - Navigation
Steps towards of sql server developer
Ad

Viewers also liked (12)

PPT
Texturappt
PPTX
A Descoberta dos habitats
PPTX
PPTX
S4 tarea4 garom
PDF
Whitepaper
PDF
Уникальный "коренной" метод от Lifelong Income
PPTX
Final presentation for teachers in action marissa holiber
PPT
Rld v3
PPT
Texturas-ppt
PPTX
Testes Unitários
PPT
S5 tarea5 garom
Texturappt
A Descoberta dos habitats
S4 tarea4 garom
Whitepaper
Уникальный "коренной" метод от Lifelong Income
Final presentation for teachers in action marissa holiber
Rld v3
Texturas-ppt
Testes Unitários
S5 tarea5 garom
Ad

Similar to Module08 (20)

PPTX
Sql server lesson6
PPTX
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
PPTX
dotnetMALAGA - Sql query tuning guidelines
PPTX
Query parameterization
PDF
DBMS and SQL Questions and Answers (1).pdf
PPT
Filtered Indexes In Sql 2008
PDF
SQL -Beginner To Intermediate Level.pdf
PPTX
02 database oprimization - improving sql performance - ent-db
PPT
1_SQL_11_13_Notes_on_how_to_gain_knowledge.ppt
PPTX
Getting to know oracle database objects iot, mviews, clusters and more…
PPT
Optimizing Data Accessin Sq Lserver2005
PPTX
SQL.pptx for the begineers and good know
PPTX
Taming the Data Science Monster with A New ‘Sword’ – U-SQL
PPT
Sydney Oracle Meetup - indexes
PDF
Sql Interview Questions
PDF
Sql
PDF
PDF
153680 sqlinterview
PDF
PPTX
Physical Design and Development
Sql server lesson6
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
dotnetMALAGA - Sql query tuning guidelines
Query parameterization
DBMS and SQL Questions and Answers (1).pdf
Filtered Indexes In Sql 2008
SQL -Beginner To Intermediate Level.pdf
02 database oprimization - improving sql performance - ent-db
1_SQL_11_13_Notes_on_how_to_gain_knowledge.ppt
Getting to know oracle database objects iot, mviews, clusters and more…
Optimizing Data Accessin Sq Lserver2005
SQL.pptx for the begineers and good know
Taming the Data Science Monster with A New ‘Sword’ – U-SQL
Sydney Oracle Meetup - indexes
Sql Interview Questions
Sql
153680 sqlinterview
Physical Design and Development

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Institutional Correction lecture only . . .
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
master seminar digital applications in india
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
01-Introduction-to-Information-Management.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Cell Types and Its function , kingdom of life
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
TR - Agricultural Crops Production NC III.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Institutional Correction lecture only . . .
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
master seminar digital applications in india
Anesthesia in Laparoscopic Surgery in India
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
01-Introduction-to-Information-Management.pdf
PPH.pptx obstetrics and gynecology in nursing
Cell Types and Its function , kingdom of life
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Renaissance Architecture: A Journey from Faith to Humanism

Module08

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

Editor's Notes

  • #5: Faculty Notes : For example : Indexes in SQL server are like an index that are given at back of any book which helps in finding topics you are looking for.
  • #6: Faculty Notes: Maintaining indexes requires time and resources. User should not create an index that would not be used frequently. The clustered index should be created before non clustered index. Clustered index changes the order of rows. The non clustered index would need to be rebuilt if it is built before clustered index. Typically, non clustered indexes are created on foreign keys
  • #7: Faculty Notes: How clustered indexes work SQL server 2005 performs the following steps when it uses a clustered index to search for a value: 1. SQL server 2005 obtains the address of the root page from sysindexes table. 2. The search values are compared with the keys of the root page 3. The highest key value is found on the page where the key value is less than or equal to the search value 4. The page pointer is followed to the next lowest level in the index 5. Steps 3 and 4 are repeated until the data page is reached 6. The rows of data are searched on the data page until the search value is found. If search value is not found on the data page ,no rows are returned by the query.
  • #8: Faculty notes: SQL server creates non clustered index by default .The data is present in random order but the logical ordering is specified by the index. The data rows may be randomly spread throught the table The non clustered index tree contains the index keys in the sorted order, with the leaf level of the index containing the pointer to the data page and the row no in the data page. SQL server 2005 performs the following steps when it uses a non clustered index to search for value: 1.SQL server obtains the address of the root page from the sysindexes table. 2. The search values are compared with the keys of the root page 3. The Page with the highest key value less than or equal to the search value is found 4. The page pointer is followed to the next lowest level in the index 5. Steps 3 and 4 are repeated until the data page is reached 6.The rows are searched on the leaf page for the specified value .If a match is not found; the table contains no matching rows. 7.If a match is found, the pointer is followed to the data page and row-id in the table, and the requested row is retrieved Non Clustered Index : The data is stored in one place, the index in another, with pointers to the storage location of the data. The items in the index are stored in the order of the index key values, but the information in the table is stored in a different order (which can be dictated by a clustered index). If no clustered index is created on the table, the rows are not guaranteed to be in any particular order.
  • #9: Faculty notes: UNIQUE creates an index in which each row should contain a different index value. CLUSTERED specifies a clustered index in which the data is sorted on the index attribute. NON CLUSTERED specifies a non clustered index that specifies only the logical Ordering of data, physically data is not sorted. Index_name specifies name of the index .it should be unique for a table, but may not be unique within a database. Table_name specifies name of the table that contains the attributes on which the index is to be created. Columns specifies name of the columns on which the index would be created.
  • #10: Faculty Notes Examples This example removes the index named au_id_ind in the authors table. USE pubs IF EXISTS (SELECT name FROM sysindexes WHERE name = 'au_id_ind') DROP INDEX authors.au_id_ind GO
  • #11: Faculty Notes : This Examples shows that user can create the index using create command Create index pk1_index on employees (EmployeeID) pk1_index is the Index name Employees is the table name present in Northwind database To view the index whether index is created or not use the following statements sp_help employees. Sp_helpindex employees To drop the index use the following command drop index employees.pk1_index