0% found this document useful (0 votes)
9 views

SQL Server Tuning

SQL Server Tuning
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SQL Server Tuning

SQL Server Tuning
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

SQL Server Tuning

Clustered Index
Clustered Index
SQL Server clustered index creates a physical
sorted data structure of the table rows according
to the defined index key.

Table data can be sorted physically in only one


direction for this reason we can define only one
clustered index per table.
Clustered Index

The root and intermediate levels contain the index


key values and page pointers.
The page pointers point to the previous and
subsequent index pages of their own.
These two levels don’t store any row data. At the
same time, index pages hold information about the
ahead and behind index page numbers.
Clustered Index
Root level: The top level of the B-tree is called as
root level. The root level is the starting point of the
data searching
Intermediate level: This level provides a connection
between root and leaf levels. SQL Server does not
create an intermediate level when the amount of
data rows are too small
Leaf Level: This level is the lowest level of the
clustered index and all records are stored at this
level
Clustered Index
when we want to query a row that’s record number
10, the storage engine will travel across the following
red dotted path. The searching mechanism begins its
travel at the root level and reaches the data row at
the leaf level.
Non Clustered Index
A non-clustered index is an index that doesn’t
physically sort the database records.
Rather, a non-clustered index is stored at a
separate location from the actual database table.
It is the non-clustered index which is actually
similar to an index of a book.
A book index is stored at a separate location,
while the actual content of the book is separately
located.
Example
CREATE Database BookStore;
GO
USE BookStore;
CREATE TABLE Books
(
id INT PRIMARY KEY NOT NULL,
name VARCHAR(50) NOT NULL,
category VARCHAR(50) NOT NULL,
price INT NOT NULL
)
Example
USE BookStore

INSERT INTO Books

VALUES
(1, 'Book1', 'Cat1', 1800),
(2, 'Book2', 'Cat2', 1500),
(3, 'Book3', 'Cat3', 2000),
(4, 'Book4', 'Cat4', 1300),
(5, 'Book5', 'Cat5', 1500),
(6, 'Book6', 'Cat6', 5000),
(7, 'Book7', 'Cat7', 8000),
(8, 'Book8', 'Cat8', 5000),
(9, 'Book9', 'Cat9', 5400),
(10, 'Book10', 'Cat10', 3200)
Example
USE BookStore
EXECUTE sp_helpindex Books
SELECT * FROM Books
CREATE CLUSTERED INDEX <index_name>
ON <table_name>(<column_name> ASC/DESC)
USE BookStore
ALTER TABLE Books
DROP CONSTRAINT
PK__Books__3213E83F7DFA309B
GO
Example
use BookStore
CREATE CLUSTERED INDEX IX_tblBook_Price
ON Books(price ASC)
SELECT * FROM Books

Non Clustered Index:


CREATE NONCLUSTERED INDEX <index_name>
ON <table_name>(<column_name> ASC/DESC)
Example
use BookStore
CREATE NONCLUSTERED INDEX IX_tblBook_Name
ON Books(name ASC)

You might also like