5. Index types
• Clustered
• Non-Clustered
• Unique
• Columnstore (2012 onwards)
• Filtered
• Spatial
• XML
• Full-text
www.dageop.com
All About Storage
6. Clustered Vs Non-clustered indexes
• Clustered indexes sort and store the data rows in the table or view based on their
key values.
• There can be only one clustered index per table
• If table is not having clustered index then its data rows are stored in an unordered
structure called heap.
• Nonclustered index have a Separate structure from data rows. A nonclustered
index contains the nonclustered index key values and each key value entry has a
pointer to the data row that contains the key value.
• Both clustered and nonclustered indexes can be unique. This means no two rows
can have the same value for the index key.
www.dageop.com
All About Storage
7. Clustered Vs Non-clustered indexes
www.dageop.com
All About Storage
Credits: msdn
1-100 101-200 201-300 301-400
1
101
201
301
1
201
Data pages having actual
records
Stores pointer to
1. Index key for CL IX
2. Row ID for Heap tables
8. Clustered Vs Non-clustered indexes
www.dageop.com
All About Storage
CLUSTERED INDEX NON-CLUSTERED INDEX
Controls the physical order of the data
pages.
Data pages of Cl-IX always include all the
columns in the table
Key column affects how the pages are
stored in B-Tree index structure
Does not affect ordering and storing of
the data.
11. Indexing a View
VIEW
PROS
• Certain columns in a table
• Combination of 2 or more tables with filter by row & column
• Not stored in physical disk
• Effective for security
CONS
• Performance affected during complex joins
• Large number of records rendered
www.dageop.com
All About Storage
12. PROS
• Improves performance
• Use same B-Tree Structure similar to a table index.
• Indexed views work best when the underlying data is infrequently updated.
CONS
• Stored in physical drive and occupies space.
• Changes to underlying tables are restricted (Schemabinding option)
• The table must be in the same database
• Columns from tables only and not from another view.
• Must have the same ownership.
• Columns must be specified (* don’t work)
• Outer Join cannot be used in indexed view
• The maintenance of an indexed view > cost of maintaining a table index.
Possibility:
• Unique, Clustered and Non-clustered index.
Indexing a View
www.dageop.com
All About Storage
13. Indexing a View
• Create an indexed view, with schemabinding option in a regular view.
www.dageop.com
All About Storage
14. Indexing a View
• The first index should be an Unique index or Clustered Index
• Below index makes the view as indexed view.
www.dageop.com
All About Storage
17. Managing Fragmentation
Storing data in different extents randomly on disk is known as fragmentation.
• Three types of fragmentation
• Internal Fragmentation
• Loads of free space in pages (eg., Fillfactor <= 50%)
• Random deletes
• Page splits due to inserts or updates
• Logical Fragmentation
• The disk drives have to jump around to different areas to read a table which is
stored in non-contiguous way.
• Heavy deletes can cause pages to be from off the page chain.
• Extent Fragmentation
• When Extent of a table or index are not contiguous. Extents are intermingled.
• It is a kind of internal fragmentation but instead of Pages this happens in extents.
www.dageop.com
All About Storage
19. Managing Fragmentation
Available Solutions:
Good way:
• Rebuild
• Reorganize
Bad way:
• Recreate the index by dropping existing (Not advisable)
• Create Clustered Index on the heap table and drop the index.
www.dageop.com
All About Storage
20. Managing Fragmentation
Page 1
Data Row 1
Data Row 2
Data Row 3
Data Row 4
Data Row 5
Page 2
Data Row 1
Data Row 2
Data Row 3
Data Row 4
Data Row 5
Statistics of Non Fragmented Pages
• Page Read Request : 2
• Extent Switches : 0
• Used Disk Space by table : 16KB
• Avg. Fragmentation in % : 0
Non Fragmented Pages
www.dageop.com
All About Storage
21. Managing Fragmentation
Fragmented Pages
Statistics of Non Fragmented Pages
• Page Read Request : 6
• Extent Switches : 5
• Used Disk Space by table : 48 KB
• Avg. Fragmentation in % : > 80
Data Row 1
Data Row 2
Page 6
Data Row 11
Data Row 12
Page 2
Data Row 3
Page 4
Data Row7
Data Row 8
Page 3
Data Row 5
Data Row 6
Page 5
Data Row 9
Data Row 10
Data Row 4
Page 1
Extent 1
Extent 2
Extent 3
www.dageop.com
All About Storage
23. Managing Fragmentation
• There are two important column in the table with the other column
• Avg_Fragmentation_in_Percent • Avg_Page_Space _Used_In_Percent
• Values in %
• Represents internal
Fragmentation
• > Value Better
• < 75 % Corrective Action
Required
• Values in %
• Represents External
Fragmentation
• Extent Fragmentation
for Heap
• < Value Better
• > 10 % Corrective
Action Required
www.dageop.com
All About Storage
24. Managing Fragmentation
• Avg_Page_Space_Used_In_Percent
• This column represents
• average percentage use of pages
• internal fragmentation
• Higher the value, the better it is
• If the value is lower then 75% Corrective action is required
• Avg_Fragmentation_in_Percent
• This column will have percentage value that represent external fragmentation.
• For a clustered table and leaf level of index pages, this is logical fragmentation
• For heap this is extent fragmentation
• The lower the value , The better it is
• If the value is higher then 10% corrective action required.
www.dageop.com
All About Storage
25. Managing Fragmentation
Repairing Fragmentation
• Reducing Fragmentation in a Heap: Create a clustered index, to rearrange the
records in order, and the pages are placed contiguously on disk
• Reducing Fragmentation in a Index: Three ways to reduce fragmentation based on
the percentage of fragementation.
• If avg_fragementation_in_percent > 5 % and < 30%:
• use ALTER INDEX REORGANIZE: Replacement for DBCC INDEXDEFRAG
• Reorders the leaf level pages of the index in a logical order.
• This is an online operation and the index is available while the
statement is running.
www.dageop.com
All About Storage
26. • avg_fragmentation_in_percent > 30%,:
• use ALTER INDEX REBUILD: Replacement for DBCC
DBREINDEX
• Rebuild the index online or offline.
• Drop and re-create the clustered index: (NOT recommended)
• Re-creating a clustered index redistributes the data and results
in full data pages.
• FILLFACTOR option can be applied.
www.dageop.com
All About Storage
Managing Fragmentation
27. Managing Fragmentation
• Repairing Fragmentation
ALTER INDEX REORGANIZE
ALTER INDEX REBUILD
REDUCING FRAGMENTATION ON INDEX
ONLY IF
FRAGMENTATION
> 5 % & < 30%
ONLY IF
FRAGMENTATION
> 30%
www.dageop.com
All About Storage
29. Review
Index Types
Types of Index
Requirement of Index
Impact of Index
Indexing a View
View
Creating Index on View
Performance Difference
Managing Fragmentation
Types of fragmentation
Correcting Fragementation
www.dageop.com
All About Storage