SlideShare a Scribd company logo
Indexes in MS SQL Server
Introduction
I bought a book from crossword; he packed the book and added two bookmarks into my pack. A
thought came to my mind. Why do I need a bookmark? I can easily memorize the page number
and the next time resume from the same page when I resume reading, or read them all over to
reach to the point where I stopped reading. But not all have a blessed memory; moreover, there
are better things to remember, my grandpa would rather bookmark and rely on it to help him
resume reading. It’s a kind of simple index, isn’t it?
This article focuses on how MS SQL Server uses indexes to read and write data. Data is arranged
by SQL Server in the form of extents and pages. Each extent is of size 64 KB, having 8 pages of
8KB sizes. An extent may have data from multiple or same table, but each page holds data from
a single table only. Logically, data is stored in record sets in the table. We have fields (columns)
identifying the type of data contained in each of the record sets. A table is nothing but a
collection of record sets; by default, rows are stored in the form of heaps unless a clustered index
has been defined on the table, in which case, record sets are sorted and stored on the clustered
index. The heaps structure is a simple arrangement where the inserted record is stored in the next
available space on the table page.
Heaps seem a great option when the motive is simply storing data, but when data retrieval steps
in, this option back fires. An index acts as a fire fighter in this scenario. Indexes are arranged in
the form of a B-Tree where the leaf node holds the data or a pointer to the data. Since the stored
data is in a sorted order, indexes precisely know which record is sitting where. Hence an index
optimizes and enhances the data retrieval immensely.
But everything comes at a cost; the price we pay for having an index on the table is, each time
there is an Insert/Update/Delete, SQL Server updates the active indexes on the table where these
DML are operated. Hence simply creating indexes madly for the sake of better data retrieval will
not serve the purpose. If there are 20 indexes on a table, each time a DML is done on the table,
all these 20 indexes shall be updated so that they can uniquely figure out the location of the
record. Let’s dive deep into the indexes.
Setup: All code has been tested on MS SQL Server 2008 R2.
Clustered Index (CI)
A clustered index is something that reorganizes the way records in the table are physically
stored. Therefore a table can have only one clustered index. The leaf nodes of a clustered index
contain the data pages, by which I mean the key-value pair in the clustered index has the index
key and the actual data value. Also remember, a clustered index will be created on a table by
default the moment a primary key is created on the table. A clustered index is something like
your train ticket B4/24, you know that you need to board coach B4 and sit on seat number 24. So
this index physically leads you to your actual seat.
We will follow this up closely with an example:
Hide Copy Code
USE TestDB
GO
CREATE TABLE Sales(
ID INT IDENTITY(1,1)
,ProductCode VARCHAR(20)
,Price FLOAT(53)
,DateTransaction DATETIME);
I have created a table Sales, and then created a Stored Procedure to insert 2,00,000 records into
the Sales table. This sizable chunk of data will help us to notice the differences very clearly.
Hide Copy Code
CREATE PROCEDURE InsertIntoSales
AS
SET NOCOUNT ON
BEGIN
DECLARE @PC VARCHAR(20)='A12CB'
DECLARE @Price INT = 50
DECLARE @COUNT INT = 0
WHILE @COUNT<200000
BEGIN
SET @PC=@PC+CAST(@COUNT AS VARCHAR(20))
SET @Price=@Price+@COUNT
INSERT INTO Sales VALUES (@PC,@Price,GETDATE())
SET @PC='A12CB'
SET @Price=50
SET @COUNT+=1
END
END
EXEC InsertIntoSales
Now we have created the table and inserted 2,00,000 records into it, but there is no index defined
on any column.
Press Control+M. This will “Include the Actual Execution Plan” in the results. Let's run the
below query.
Hide Copy Code
SET STATISTICS IO ON
SELECT * FROM Sales WHERE ID=189923
ID ProductCode Price DateTransaction
----------- ---------------- ---------- -----------------------
189923 A12CB189922 189972 2011-03-21 12:07:48.310
(1 row(s) affected)
Table 'Sales'. Scan count 1, logical reads 1129, physical reads 0,
read-ahead reads 0, lob logical reads 0,
lob physical reads 0, lob read-ahead reads 0.
(1 row(s) affected)
The Execution plan tab on the results show that the record has been retrieved on a table scan and
the logical reads are 1129.
Now let’s build a clustered index on the ID column of the Sales table.
Hide Copy Code
CREATE CLUSTERED INDEX CL_ID ON SALES(ID);
Let us press CTRL+M and rerun the same query:
Hide Copy Code
SET STATISTICS IO ON
SELECT * FROM Sales WHERE ID=189923
ID ProductCode Price DateTransaction
----------- ---------------- ------------ -----------------------
189923 A12CB189922 189972 2011-03-21 12:07:48.310
(1 row(s) affected)
Table 'Sales'. Scan count 1, logical reads 3, physical reads 0,
read-ahead reads 0, lob logical reads 0,
lob physical reads 0, lob read-ahead reads 0.
(1 row(s) affected)
The Execution plan tab on the results shows that the record has been retrieved on Index seek and
the logical reads are 3. After the clustered index creation, SQL Server has been able to reduce the
logical reads dramatically and the query has been optimized. Clearly the index knows where to
look for the record.
Non-Clustered Index (NCI)
A non-clustered index is a special type of index in which the logical order of the index does not
match the physical stored order of the rows on disk. The leaf node of a non-clustered index does
not consist of the data pages but a pointer to it. That goes to say that a non-clustered index can’t
survive on its own - it needs a base to live on. A non-clustered index uses a clustered index (if
defined) or the heap to build itself.
When a non-clustered index uses the heap, the leaf node (or the pointer) is a physical location of
the data. When it uses a clustered index, the leaf node (or the pointer) is the clustered index key
value and this key value in turn points to the actual data.
Part I: When NCI Uses a CI
Getting back to Sales, we already have a CI (CL_ID) on the ID column, now if we have a query
something like:
Hide Copy Code
SET STATISTICS IO ON
SELECT * FROM Sales WHERE ProductCode like 'A12CB908%' order by Price
Press Control+M and execute the query
There are arround 111 records retrived
-----------------------------------------------------
(111 row(s) affected)
Table 'Sales'. Scan count 1, logical reads 1130, physical reads 0,
read-ahead reads 0, lob logical reads 0,
lob physical reads 0, lob read-ahead reads 0.
(1 row(s) affected)
We find that the query first uses the clustered index to get 111 records and then uses a sort
operation; the logical reads are as high as 1130. There is also a missing index suggestion.
Let’s consider SQL Server’s advice and create a non-clustered index (NONCI_PC) on the
ProductCode column. Since we have a CI already, this NCI would be built on the CI.
Hide Copy Code
CREATE NONCLUSTERED INDEX NONCI_PC ON SALES(ProductCode);
Press Control+M and rerun the same query, this time around, we can see the data fetch plan
change.
Hide Copy Code
SET STATISTICS IO ON
SELECT * FROM Sales WHERE ProductCode like 'A12CB908%' order by Price
-------------------------------
(111 row(s) affected)
Table 'Sales'. Scan count 1, logical reads 351, physical reads 0,
read-ahead reads 7, lob logical reads 0,
lob physical reads 0, lob read-ahead reads 0.
(1 row(s) affected)
The logical reads have been minimized and the revised execution plan is as in the figure. This
was the example where anon-clustered index used a clustered index.
Part II: When NCI Uses a Heap
When there is no clustered index built on a table and a non-clustered index is built, it uses the
heap for data retrieval. The indexed column or columns are sorted along with a pointer to the
physical location of the data.
The big question is, how do I know if I should create an NCI on a CI or on a heap?
The answer is in the query, if data is queried typically on one particular column, it would be
beneficial to build NCI upon a CI, but when the case is something like what we have in the
present Sales example where we will be building a one-column NC index on a heap, the NCI
would be merely a two-column table containing the key-value pair (index key and physical
location, i.e., value). This would be the best optimization in this scenario. To follow this up, let’s
review the example closely.
With respect to the sales example, let’s delete the clustered index CL_ID created on the ID
column and re-evaluate.
Hide Copy Code
DROP INDEX Sales.CL_ID;
SET STATISTICS IO ON
SELECT * FROM Sales WHERE ProductCode like 'A12CB908%' order by Price
------------------------------------
(111 row(s) affected)
Table 'Sales'. Scan count 1, logical reads 114, physical reads 0,
read-ahead reads 0, lob logical reads 0,
lob physical reads 0, lob read-ahead reads 0.
(1 row(s) affected)
The logical reads have been further optimized and the execution plan also has been revised. In
this case, the query uses the non-clustered index to be run on the heap.
We have been able to create indexes so that our queries work with a minimal performance
overhead. So now the next big question is, Wouldn’t it be great if there was someone to help us
out in prompting on the indexes to be built based on our queries?
FRAGMENTATION:
Storing data non-contiguously on disk is known as fragmentation.
Internal Fragmentation: When records are stored non-contiguously inside the page, then it is
called internal fragmentation.
External Fragmentation: When on disk, the physical storage of pages and extents is not
contiguous. When the extents of a table are not physically stored contiguously on disk, switching
from one extent to another causes higher disk rotations, and this is called Extent Fragmentation.
SELECT OBJECT_NAME(OBJECT_ID), index_id,index_type_desc,index_level,
avg_fragmentation_in_percent,avg_page_space_used_in_percent,page_count
FROM sys.dm_db_index_physical_stats
(DB_ID(N'AdventureWorksLT'), NULL, NULL, NULL , 'SAMPLED')
ORDER BY avg_fragmentation_in_percent DESC
avg_fragmentation_in_percent: This is a percentage value that represents external fragmentation.
For a clustered table and leaf level of index pages, this is Logical fragmentation, while for heap,
this is Extent fragmentation. The lower this value, the better it is. If this value is higher than
10%, some corrective action should be taken.
avg_page_space_used_in_percent: This is an average percentage use of pages that represents to
internal fragmentation. Higher the value, the better it is. If this value is lower than 75%, some
corrective action should be taken.
Reducing fragmentation:
Reducing Fragmentation in a Heap: To reduce the fragmentation of a heap, create a clustered
index on the table. Creating the clustered index, rearrange the records in an order, and then place
the pages contiguously on disk.
Reducing Fragmentation in an Index: There are three choices for reducing fragmentation, and we
can choose one according to the percentage of fragmentation:
If avg_fragmentation_in_percent > 5% and < 30%, then use ALTER INDEX REORGANIZE:
This statement is replacement for DBCC INDEXDEFRAG to reorder the leaf level pages of the
index in a logical order. As this is an online operation, the index is available while the statement
is running.
If avg_fragmentation_in_percent > 30%, then use ALTER INDEX REBUILD: This is
replacement for DBCC DBREINDEX to rebuild the index online or offline. In such case, we can
also use the drop and re-create index method.

More Related Content

PDF
Revision booklet 6957 2016
PDF
IBM Cognos tutorial - ABC LEARN
PDF
Understanding dso (data store object) part 1%3a standard dso.doc
PDF
Introduction to Dating Modeling for Cassandra
POT
10359485
PDF
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
PDF
Windowing Functions - Little Rock Tech Fest 2019
PPT
Republican party organization revised 4-11-2011
Revision booklet 6957 2016
IBM Cognos tutorial - ABC LEARN
Understanding dso (data store object) part 1%3a standard dso.doc
Introduction to Dating Modeling for Cassandra
10359485
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
Windowing Functions - Little Rock Tech Fest 2019
Republican party organization revised 4-11-2011

Similar to Indexes in ms sql server (20)

PPTX
dotnetMALAGA - Sql query tuning guidelines
PPTX
Indy pass writing efficient queries – part 1 - indexing
PPT
Indexing Strategies
PPTX
Introduction of sql server indexing
PPTX
We Don't Need Roads: A Developers Look Into SQL Server Indexes
PPTX
Sql server lesson6
PDF
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
PDF
See sql server graphical execution plans in action tech republic
PDF
Enterprise dbs and Database indexing
PPTX
JSSUG: SQL Sever Index Tuning
PPTX
Understanding indices
PPTX
Geek Sync | Understand Indexes to Write Better Queries
PPT
Module08
PPT
Module08
PDF
3 indexes
PPTX
Sql performance tuning
PPTX
SQL Server Index and Partition Strategy
PPTX
Sql server ___________session_17(indexes)
PDF
Building better SQL Server Databases
PPTX
Query Optimization in SQL Server
dotnetMALAGA - Sql query tuning guidelines
Indy pass writing efficient queries – part 1 - indexing
Indexing Strategies
Introduction of sql server indexing
We Don't Need Roads: A Developers Look Into SQL Server Indexes
Sql server lesson6
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
See sql server graphical execution plans in action tech republic
Enterprise dbs and Database indexing
JSSUG: SQL Sever Index Tuning
Understanding indices
Geek Sync | Understand Indexes to Write Better Queries
Module08
Module08
3 indexes
Sql performance tuning
SQL Server Index and Partition Strategy
Sql server ___________session_17(indexes)
Building better SQL Server Databases
Query Optimization in SQL Server
Ad

Recently uploaded (20)

PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Sustainable Sites - Green Building Construction
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Geodesy 1.pptx...............................................
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Welding lecture in detail for understanding
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Well-logging-methods_new................
DOCX
573137875-Attendance-Management-System-original
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Construction Project Organization Group 2.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Lecture Notes Electrical Wiring System Components
Sustainable Sites - Green Building Construction
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Geodesy 1.pptx...............................................
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Digital Logic Computer Design lecture notes
bas. eng. economics group 4 presentation 1.pptx
Welding lecture in detail for understanding
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Well-logging-methods_new................
573137875-Attendance-Management-System-original
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Construction Project Organization Group 2.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Ad

Indexes in ms sql server

  • 1. Indexes in MS SQL Server Introduction I bought a book from crossword; he packed the book and added two bookmarks into my pack. A thought came to my mind. Why do I need a bookmark? I can easily memorize the page number and the next time resume from the same page when I resume reading, or read them all over to reach to the point where I stopped reading. But not all have a blessed memory; moreover, there are better things to remember, my grandpa would rather bookmark and rely on it to help him resume reading. It’s a kind of simple index, isn’t it? This article focuses on how MS SQL Server uses indexes to read and write data. Data is arranged by SQL Server in the form of extents and pages. Each extent is of size 64 KB, having 8 pages of 8KB sizes. An extent may have data from multiple or same table, but each page holds data from a single table only. Logically, data is stored in record sets in the table. We have fields (columns) identifying the type of data contained in each of the record sets. A table is nothing but a collection of record sets; by default, rows are stored in the form of heaps unless a clustered index has been defined on the table, in which case, record sets are sorted and stored on the clustered index. The heaps structure is a simple arrangement where the inserted record is stored in the next available space on the table page. Heaps seem a great option when the motive is simply storing data, but when data retrieval steps in, this option back fires. An index acts as a fire fighter in this scenario. Indexes are arranged in the form of a B-Tree where the leaf node holds the data or a pointer to the data. Since the stored data is in a sorted order, indexes precisely know which record is sitting where. Hence an index optimizes and enhances the data retrieval immensely. But everything comes at a cost; the price we pay for having an index on the table is, each time there is an Insert/Update/Delete, SQL Server updates the active indexes on the table where these DML are operated. Hence simply creating indexes madly for the sake of better data retrieval will not serve the purpose. If there are 20 indexes on a table, each time a DML is done on the table, all these 20 indexes shall be updated so that they can uniquely figure out the location of the record. Let’s dive deep into the indexes. Setup: All code has been tested on MS SQL Server 2008 R2. Clustered Index (CI) A clustered index is something that reorganizes the way records in the table are physically stored. Therefore a table can have only one clustered index. The leaf nodes of a clustered index contain the data pages, by which I mean the key-value pair in the clustered index has the index key and the actual data value. Also remember, a clustered index will be created on a table by default the moment a primary key is created on the table. A clustered index is something like your train ticket B4/24, you know that you need to board coach B4 and sit on seat number 24. So this index physically leads you to your actual seat.
  • 2. We will follow this up closely with an example: Hide Copy Code USE TestDB GO CREATE TABLE Sales( ID INT IDENTITY(1,1) ,ProductCode VARCHAR(20) ,Price FLOAT(53) ,DateTransaction DATETIME); I have created a table Sales, and then created a Stored Procedure to insert 2,00,000 records into the Sales table. This sizable chunk of data will help us to notice the differences very clearly. Hide Copy Code CREATE PROCEDURE InsertIntoSales AS SET NOCOUNT ON BEGIN DECLARE @PC VARCHAR(20)='A12CB' DECLARE @Price INT = 50 DECLARE @COUNT INT = 0 WHILE @COUNT<200000 BEGIN SET @PC=@PC+CAST(@COUNT AS VARCHAR(20)) SET @Price=@Price+@COUNT INSERT INTO Sales VALUES (@PC,@Price,GETDATE()) SET @PC='A12CB' SET @Price=50 SET @COUNT+=1 END END EXEC InsertIntoSales Now we have created the table and inserted 2,00,000 records into it, but there is no index defined on any column. Press Control+M. This will “Include the Actual Execution Plan” in the results. Let's run the below query. Hide Copy Code
  • 3. SET STATISTICS IO ON SELECT * FROM Sales WHERE ID=189923 ID ProductCode Price DateTransaction ----------- ---------------- ---------- ----------------------- 189923 A12CB189922 189972 2011-03-21 12:07:48.310 (1 row(s) affected) Table 'Sales'. Scan count 1, logical reads 1129, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. (1 row(s) affected) The Execution plan tab on the results show that the record has been retrieved on a table scan and the logical reads are 1129. Now let’s build a clustered index on the ID column of the Sales table. Hide Copy Code CREATE CLUSTERED INDEX CL_ID ON SALES(ID); Let us press CTRL+M and rerun the same query: Hide Copy Code SET STATISTICS IO ON SELECT * FROM Sales WHERE ID=189923 ID ProductCode Price DateTransaction ----------- ---------------- ------------ ----------------------- 189923 A12CB189922 189972 2011-03-21 12:07:48.310
  • 4. (1 row(s) affected) Table 'Sales'. Scan count 1, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. (1 row(s) affected) The Execution plan tab on the results shows that the record has been retrieved on Index seek and the logical reads are 3. After the clustered index creation, SQL Server has been able to reduce the logical reads dramatically and the query has been optimized. Clearly the index knows where to look for the record. Non-Clustered Index (NCI) A non-clustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk. The leaf node of a non-clustered index does not consist of the data pages but a pointer to it. That goes to say that a non-clustered index can’t survive on its own - it needs a base to live on. A non-clustered index uses a clustered index (if defined) or the heap to build itself. When a non-clustered index uses the heap, the leaf node (or the pointer) is a physical location of the data. When it uses a clustered index, the leaf node (or the pointer) is the clustered index key value and this key value in turn points to the actual data. Part I: When NCI Uses a CI Getting back to Sales, we already have a CI (CL_ID) on the ID column, now if we have a query something like: Hide Copy Code SET STATISTICS IO ON SELECT * FROM Sales WHERE ProductCode like 'A12CB908%' order by Price Press Control+M and execute the query
  • 5. There are arround 111 records retrived ----------------------------------------------------- (111 row(s) affected) Table 'Sales'. Scan count 1, logical reads 1130, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. (1 row(s) affected) We find that the query first uses the clustered index to get 111 records and then uses a sort operation; the logical reads are as high as 1130. There is also a missing index suggestion. Let’s consider SQL Server’s advice and create a non-clustered index (NONCI_PC) on the ProductCode column. Since we have a CI already, this NCI would be built on the CI. Hide Copy Code CREATE NONCLUSTERED INDEX NONCI_PC ON SALES(ProductCode); Press Control+M and rerun the same query, this time around, we can see the data fetch plan change. Hide Copy Code SET STATISTICS IO ON SELECT * FROM Sales WHERE ProductCode like 'A12CB908%' order by Price ------------------------------- (111 row(s) affected) Table 'Sales'. Scan count 1, logical reads 351, physical reads 0, read-ahead reads 7, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. (1 row(s) affected)
  • 6. The logical reads have been minimized and the revised execution plan is as in the figure. This was the example where anon-clustered index used a clustered index. Part II: When NCI Uses a Heap When there is no clustered index built on a table and a non-clustered index is built, it uses the heap for data retrieval. The indexed column or columns are sorted along with a pointer to the physical location of the data. The big question is, how do I know if I should create an NCI on a CI or on a heap? The answer is in the query, if data is queried typically on one particular column, it would be beneficial to build NCI upon a CI, but when the case is something like what we have in the present Sales example where we will be building a one-column NC index on a heap, the NCI would be merely a two-column table containing the key-value pair (index key and physical location, i.e., value). This would be the best optimization in this scenario. To follow this up, let’s review the example closely. With respect to the sales example, let’s delete the clustered index CL_ID created on the ID column and re-evaluate. Hide Copy Code DROP INDEX Sales.CL_ID; SET STATISTICS IO ON SELECT * FROM Sales WHERE ProductCode like 'A12CB908%' order by Price ------------------------------------ (111 row(s) affected) Table 'Sales'. Scan count 1, logical reads 114, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
  • 7. (1 row(s) affected) The logical reads have been further optimized and the execution plan also has been revised. In this case, the query uses the non-clustered index to be run on the heap. We have been able to create indexes so that our queries work with a minimal performance overhead. So now the next big question is, Wouldn’t it be great if there was someone to help us out in prompting on the indexes to be built based on our queries? FRAGMENTATION: Storing data non-contiguously on disk is known as fragmentation. Internal Fragmentation: When records are stored non-contiguously inside the page, then it is called internal fragmentation. External Fragmentation: When on disk, the physical storage of pages and extents is not contiguous. When the extents of a table are not physically stored contiguously on disk, switching from one extent to another causes higher disk rotations, and this is called Extent Fragmentation. SELECT OBJECT_NAME(OBJECT_ID), index_id,index_type_desc,index_level, avg_fragmentation_in_percent,avg_page_space_used_in_percent,page_count FROM sys.dm_db_index_physical_stats (DB_ID(N'AdventureWorksLT'), NULL, NULL, NULL , 'SAMPLED') ORDER BY avg_fragmentation_in_percent DESC
  • 8. avg_fragmentation_in_percent: This is a percentage value that represents external fragmentation. For a clustered table and leaf level of index pages, this is Logical fragmentation, while for heap, this is Extent fragmentation. The lower this value, the better it is. If this value is higher than 10%, some corrective action should be taken. avg_page_space_used_in_percent: This is an average percentage use of pages that represents to internal fragmentation. Higher the value, the better it is. If this value is lower than 75%, some corrective action should be taken. Reducing fragmentation: Reducing Fragmentation in a Heap: To reduce the fragmentation of a heap, create a clustered index on the table. Creating the clustered index, rearrange the records in an order, and then place the pages contiguously on disk. Reducing Fragmentation in an Index: There are three choices for reducing fragmentation, and we can choose one according to the percentage of fragmentation: If avg_fragmentation_in_percent > 5% and < 30%, then use ALTER INDEX REORGANIZE: This statement is replacement for DBCC INDEXDEFRAG to reorder the leaf level pages of the index in a logical order. As this is an online operation, the index is available while the statement is running. If avg_fragmentation_in_percent > 30%, then use ALTER INDEX REBUILD: This is replacement for DBCC DBREINDEX to rebuild the index online or offline. In such case, we can also use the drop and re-create index method.