3. Using Indexes?
Indexing basics
Successful applications can ultimately fail due to bad
performance as they grow
You cannot optimize what you do not understand
Understand SQL Data storage
Index organization and utilization
4. SQL Server Storage Hierarchy
Database
Extent
8 contiguous 64K data pages
Once extent full, next record will take up a whole
additional extent.
Pre-allocating space saves time.
5. SQL Server Storage Hierarchy
Page
64K bytes
# of records/page varies with bytes/record
Types of Pages: Data and Index pages
Page Split
When page becomes full, it splits
New page allocated: ½ data from old page moved to
new
Rows
8060 bytes and 1024 columns
6. B-tree Key Points to Remember
Tree portion includes key attributes only
ordered as in create index statement
Keys packed in index pages
Fewer bytes per key -> more keys per page/extent
-> fewer page faults per access.
Clustered indexes have records at leafs
Records are in data pages
Data pages sequentially linked
Non-Clustered indexes point into heap or tree
portion of clustered index
7. Create INDEX Options
CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ]
INDEX index_name
ON <object> ( column [ ASC | DESC ] [ ,...n ] )
[ INCLUDE ( column_name [ ,...n ] ) ]
[ WHERE <filter_predicate> ]
[ WITH ( <relational_index_option> [ ,...n ] ) ]
[ ON { partition_scheme_name ( column_name ) … ]
PAD_INDEX = { ON | OFF } |
FILLFACTOR = fillfactor |
SORT_IN_TEMPDB = { ON | OFF } |
IGNORE_DUP_KEY = { ON | OFF } |
STATISTICS_NORECOMPUTE = { ON | OFF } |
DROP_EXISTING = { ON | OFF } |
ONLINE = { ON | OFF } |
ALLOW_ROW_LOCKS = { ON | OFF } |
ALLOW_PAGE_LOCKS = { ON | OFF } |
MAXDOP = max_degree_of_parallelism |
DATA_COMPRESSION = { NONE | ROW | PAGE} [ ON PARTITIONS ( {
<partition_number_expression> | <range> } [ , ...n ] ) ]}
13. Maintaining Indexes
Page Splits
Insert/delete order and rate critical
Fragmentation
Not OS fragementation – e.g. defrag tool
Happens when database grows, pages split, and
then data eventually deleted.
Btrees great on maintaining balance on insertions,
but with deletes, can end up with many pages
containing small # of records.
14. Fragmentation Problems
Wasted space
Sql server allocates an extend at a time
Could end up with an extent, containing single page, with
single record.
Thrashing (way too many disk hits)
Could end up with page 1 of data on one extend,
page 2 on another, page 3 on the first, page 4 on
another, ….
Records all over the place
Bit better for inserts but really bad for reads!
16. Summary
All data manipulation language statements can
benefit, from indexes, but inserts, deletes, and
updates are slowed.
Indexes take up space and require page hits.
Clustered indexes usually faster than non-
clustered
Indexes need to be maintained on a regular
basis – use DMVs
Use indexing strategy for XML and CLR data as
well.