Transcript
Page 1: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Table Structures and Indexing

Page 2: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

The concept of indexing• If you were asked to search for the name “Adam Wilbert” in a phonebook, you

would go directly to the W section, and search from there alphabetically. This would be an easy task because the phonebook is indexed alphabetically.• However, if you were asked to search for the number (146)-221-4065 you would

have to go through all the numbers in the phonebook until you find a match

Page 3: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Indexes• Indexes allow the SQL server engine to perform fast, targeted data retrieval

rather than simply scanning through the entire table.• It is faster to search an indexed column than a non-indexed column• However, an index must be updated every time the table is updated. For

example, every time you insert, update or delete a row in the table, the indexes need to be updated accordingly.

ÞIndexes make searching for data faster, but might make deleting, inserting or updating new data slower.

ÞYou should create indexes on your most searched columns only

Page 4: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Index Types1. Clustered2. Non-clustered

Page 5: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Clustered Index• Determines how a table is written to disk. For example, if your clustered index is

created on the lastName column, the rows will be stored in the file on the disk alphabetically by last name, and when you retrieve the names from the table, they will be already ordered alphabetically. • You can have only one clustered index per table• A clustered index is automatically created on the primary key column, however

you can change it to any column in your table (including columns with non-unique values, such as the last name. But its not a good practice to apply a clustered index to a non-unique column).

Page 6: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Clustered Index

In this example, a clustered index is created on the primary keyThe leaf pages of the clustered indexes are the data pages

Page 7: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Non-clustered Index• A non-clustered index is completely separate from the data page (because the

data page has already been stored based on the clustered index) • On the leaf pages of the index, there are pointers to go to the data pages.• The pointer from the index to a data row is called a “row locator”

Page 8: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Non-clustered Index

Row

loca

tors

In this case, the data page is not stored alphabeticallyThe index leaf pages only point to the actual data in the data page

Page 9: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Best PracticesCreate non-clustered indexes for:• Foreign key columns• Columns used commonly in the following clasues:

o JOIN oWHERE oORDER BYoGROUP BY

Page 10: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Creating an indexCREATE [UNIQUE] [CLUSTERED | NONCLUSTERED] INDEX indexNameON tableName (columnList);

• You can specify a clustered or non-clustered index, with non-clustered being the default type when the type is not specified

• You can create an index on a column with unique values by including the UNIQUE keyword. The DB engine will not allow creating a unique index on a column that already includes duplicate data

• A clustered index is automatically created on the primary key column • A non-clustered index is automatically created on a column with a UNIQUE constraint

Page 11: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Creating an indexCREATE [UNIQUE] [CLUSTERED | NONCLUSTERED] INDEX indexNameON tableName (column1, column2 desc);

• You can create an index on one column or on more than one (composite index)• You can specify whether you want the index to be sorted on the column values in

an ascending or descending order (the default is ascending)

Page 12: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Index TypesIndex Types:

1. Clustered (always unique)2. Nonclustered:

1. Unique2. Non-unique

Page 13: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Viewing your IndexesTo view the indexes you have created on a table:

SELECT name, type_desc, is_uniqueFROM sys.indexesWHERE OBJECT_ID(‘schemaName.tableName')= object_id;

You will find the indexes you created explicitly as well as the implicitly created indexes (the primary key and the unique columns)

Page 14: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Example

Page 15: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

step1 – create the tableCREATE SCHEMA produce;GOCREATE TABLE produce.vegetable( vegetableID int NOT NULL CONSTRAINT Pkproduce_vegetable PRIMARY KEY, name varchar(15) NOT NULL CONSTRAINT AKproduce_vegetable_name UNIQUE, color varchar(10) NOT NULL, consistency varchar(10) NOT NULL, filler char(4000) NOT NULL);

Page 16: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

step2 – create the indexesCREATE INDEX Xproduce_vegetable_color ON produce.vegetable(color);CREATE INDEX Xproduce_vegetable_consistency ON produce.vegetable(consistency);CREATE UNIQUE INDEX Xproduce_vegetable_vegetableID_color ON produce.vegetable(vegetableID, color);

Page 17: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

step3 – view the indexes created on your tableIf you want to view the indexes created explicitly and implicitly on this table, run the following query:

SELECT name, type_desc, is_uniqueFROM sys.indexesWHERE OBJECT_ID(‘produce.vegetable')= object_id;

Page 18: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

step4 – the resultsname type_desc is_unique

PKproduce_vegetable CLUSTERED 1

Akproduce_vegetable_name NONCLUSTERED 1

Xproduce_vegetable_color NONCLUSTERED 0

Xproduce_vegetable_consistency NONCLUSTERED 0

Xproduce_vegetable_vegetableID_color NONCLUSTERED 1

Page 19: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Deleting an indexTo delete an index use the DROP INDEX statement:

DROP INDEX indexName ON schema.table;

Example:

DROP INDEX Xproduce_vegetable_consistency ON produce.vegetable;

Page 20: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

Performance Optimization with

Indexes

Page 21: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

When searching for a value from the clustered index column, SQL Server will perform a Seek

Page 22: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

A seek operation costs less than a scan operation, because it goes directly to the target row

Page 23: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

When searching for a value from a column without an index, SQL Server will perform a scan

Page 24: Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to

A scan operation costs more than a seek operation, because it goes through all the rows in the table ( Estimated CPU cost 0.0001581 vs. 0.0001647)