Module08
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
813
On Slideshare
813
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • 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.
  • 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
  • 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.
  • 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.
  • 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.
  • 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
  • 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

Transcript

  • 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.
  • 16. Questions & Comments