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 (20)

PPT
Module05
PPTX
Sql Server Data Tools - Codenamed JUNEAU
PDF
Multi-thematic spatial databases
PPT
Module02
PPT
SQL Server 2008 for .NET Developers
PPT
Module04
PDF
High Performance Front-End Development
PPT
Module03
PPTX
Sql server ___________session3-normailzation
PPT
Module06
PPTX
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...
PDF
X query language reference
PDF
Spatialware_2_Sql08
PPTX
Alasql.js - SQL сервер на JavaScript
PPTX
Alasql - база данных SQL на JavaScript (MoscowJS)
PDF
5 tsssisu sql_server_2012
PDF
Transact sql data definition language - ddl- reference
PPT
Module01
PPT
Module07
PDF
Multidimensional model programming
Module05
Sql Server Data Tools - Codenamed JUNEAU
Multi-thematic spatial databases
Module02
SQL Server 2008 for .NET Developers
Module04
High Performance Front-End Development
Module03
Sql server ___________session3-normailzation
Module06
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...
X query language reference
Spatialware_2_Sql08
Alasql.js - SQL сервер на JavaScript
Alasql - база данных SQL на JavaScript (MoscowJS)
5 tsssisu sql_server_2012
Transact sql data definition language - ddl- reference
Module01
Module07
Multidimensional model programming
Ad

Similar to Module08 (20)

PPS
07 qmds2005 session10
PDF
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
PPTX
Sql performance tuning
DOCX
Indexes in ms sql server
PPTX
Query Optimization in SQL Server
PPTX
dotnetMALAGA - Sql query tuning guidelines
PPTX
presentation is on database for sql and stored procedures
PPTX
Index the obvious and not so obvious
PPTX
Sql server lesson6
PDF
SQA server performance tuning
PDF
MySQL INDEXES
PDF
Geek Sync I Consolidating Indexes in SQL Server
PPTX
We Don't Need Roads: A Developers Look Into SQL Server Indexes
PPTX
Database_Indexing_AND ITTS TYPES PRESENTATION
PPT
Intro to tsql unit 7
PPTX
Index_2
PDF
Indexing techniques
PPTX
Ms sql server tips 1 0
PPT
Indexing
07 qmds2005 session10
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
Sql performance tuning
Indexes in ms sql server
Query Optimization in SQL Server
dotnetMALAGA - Sql query tuning guidelines
presentation is on database for sql and stored procedures
Index the obvious and not so obvious
Sql server lesson6
SQA server performance tuning
MySQL INDEXES
Geek Sync I Consolidating Indexes in SQL Server
We Don't Need Roads: A Developers Look Into SQL Server Indexes
Database_Indexing_AND ITTS TYPES PRESENTATION
Intro to tsql unit 7
Index_2
Indexing techniques
Ms sql server tips 1 0
Indexing

Recently uploaded (20)

PDF
Pre independence Education in Inndia.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Cell Structure & Organelles in detailed.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
master seminar digital applications in india
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pre independence Education in Inndia.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Basic Mud Logging Guide for educational purpose
102 student loan defaulters named and shamed – Is someone you know on the list?
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Cell Structure & Organelles in detailed.
Microbial diseases, their pathogenesis and prophylaxis
O7-L3 Supply Chain Operations - ICLT Program
VCE English Exam - Section C Student Revision Booklet
master seminar digital applications in india
human mycosis Human fungal infections are called human mycosis..pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
2.FourierTransform-ShortQuestionswithAnswers.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Week 4 Term 3 Study Techniques revisited.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra

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