SQL Server Indexes Overview Indexes are used to

  • Slides: 13
Download presentation
SQL Server Indexes

SQL Server Indexes

Overview • Indexes are used to help speed search results in a database. A

Overview • Indexes are used to help speed search results in a database. A careful use of indexes can greatly improve search speeds. Over use of indexes, however, can painfully slow database inserts and updates.

Types of indexes • Clustered Indexes • Non Clustered Indexes • • Standard non

Types of indexes • Clustered Indexes • Non Clustered Indexes • • Standard non clustered Unique Filtered Composite (more than one field)

Clustered Indexes • A clustered index physically reorders the table contents • Clustered indexes

Clustered Indexes • A clustered index physically reorders the table contents • Clustered indexes are usually assigned to a tables primary key • There can only be one clustered index per table. CREATE CLUSTERED INDEX ix_Customer. Key ON Customer(Customer. Key)

Non Clustered Indexes • Non Clustered indexes form a separate structure called a B-Tree

Non Clustered Indexes • Non Clustered indexes form a separate structure called a B-Tree that greatly speeds searches. • Instead of going through thousands or millions of rows to find a value, the Btree enables a search using only a very few records. • Indexes can slow INSERTS, UPDATES and DELETES because the B-Tree index must be rebuilt each time

B-Tree Structure

B-Tree Structure

Syntax for Standard Non Clustered Index CREATE NONCLUSTERED INDEX Ix_Last. Name ON Customer(Last. Name)

Syntax for Standard Non Clustered Index CREATE NONCLUSTERED INDEX Ix_Last. Name ON Customer(Last. Name) The term NONCLUSTERED is optional

Unique, Composite, and Filtered Indexes CREATE UNIQUE INDEX ix_Email ON Customer(Customer. Email) CREATE INDEX

Unique, Composite, and Filtered Indexes CREATE UNIQUE INDEX ix_Email ON Customer(Customer. Email) CREATE INDEX ix_Address ON Customer. Address(City, State, Zip) CREATE INDEX ix_Apartment ON Customer. Address(Apartment) WHERE Apartment IS NOT NULL

Columnstore Indexes • This is a new type of index available only in Enterprise

Columnstore Indexes • This is a new type of index available only in Enterprise Editions of SQL Server 2012 or later. Instead of storing the data rows contiguously across pages it stores the data in columns contiguously across pages. • These are best used in data warehousing or read only databases

Forcing an index • The database management system query optimization system will ignore indexes

Forcing an index • The database management system query optimization system will ignore indexes in tables with few rows. It is more efficient to skip the index with under 10, 000 rows or so, but you can force an index: SELECT * FROM Employee WHERE Employee. Name='John Smith' WITH (NOLOCK, INDEX(ix_Employee. Name))

Enabling, Disabling and Dropping Indexes • ALTER INDEX ix_Employee. Name ON Employee • DROP

Enabling, Disabling and Dropping Indexes • ALTER INDEX ix_Employee. Name ON Employee • DROP INDEX ix_Employee. Name ON Employee DISABLE REBUILD

Testing Queries • SQL Server contains tools for testing and comparing query results

Testing Queries • SQL Server contains tools for testing and comparing query results

Best Practices • Indexes should be applied on Columns used often in searches. •

Best Practices • Indexes should be applied on Columns used often in searches. • Foreign keys make good candidates for indexes • It takes careful testing to be sure where to place indexes