Download presentation
Presentation is loading. Please wait.
Published byScarlett Johns Modified over 8 years ago
1
Table Structures and Indexing
2
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
3
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
4
Index Types 1.Clustered 2.Non-clustered
5
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).
6
Clustered Index In this example, a clustered index is created on the primary key The leaf pages of the clustered indexes are the data pages
7
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”
8
Non-clustered Index Row locators In this case, the data page is not stored alphabetically The index leaf pages only point to the actual data in the data page
9
Best Practices Create non-clustered indexes for: Foreign key columns Columns used commonly in the following clasues: o JOIN o WHERE o ORDER BY o GROUP BY
10
Creating an index CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED] INDEX indexName ON 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
11
Creating an index CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED] INDEX indexName ON 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)
12
Index Types Index Types: 1.Clustered (always unique) 2.Nonclustered: 1.Unique 2.Non-unique
13
Viewing your Indexes To view the indexes you have created on a table: SELECT name, type_desc, is_unique FROM sys.indexes WHERE 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)
14
Example
15
step1 – create the table CREATE SCHEMA produce; GO CREATE 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 );
16
step2 – create the indexes CREATE 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);
17
step3 – view the indexes created on your table If you want to view the indexes created explicitly and implicitly on this table, run the following query: SELECT name, type_desc, is_unique FROM sys.indexes WHERE OBJECT_ID(‘produce.vegetable')= object_id;
18
step4 – the results nametype_descis_unique PKproduce_vegetableCLUSTERED1 Akproduce_vegetable_nameNONCLUSTERED1 Xproduce_vegetable_colorNONCLUSTERED0 Xproduce_vegetable_consistencyNONCLUSTERED0 Xproduce_vegetable_vegetableID_colorNONCLUSTERED1
19
Deleting an index To delete an index use the DROP INDEX statement: DROP INDEX indexName ON schema.table; Example: DROP INDEX Xproduce_vegetable_consistency ON produce.vegetable;
20
Performance Optimization with Indexes
21
When searching for a value from the clustered index column, SQL Server will perform a Seek
22
A seek operation costs less than a scan operation, because it goes directly to the target row
23
When searching for a value from a column without an index, SQL Server will perform a scan
24
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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.