Indexes

9
Indexes CIS282

description

 

Transcript of Indexes

Page 1: Indexes

IndexesCIS282

Page 2: Indexes

IndexesHelp to select and find recordsServe as a “cross-reference”Can be based on one or more fields

Page 3: Indexes

Types of IndexesClustered: How table is physically ordered

Only one per tableNon-Clustered: Index is maintained

separately from table (data)Can be stored in a separate file group to speed

access (find pointers using one drive, find records on a second drive)

Page 4: Indexes

Index OrganizationData in SQL Server is organized by ‘pages’When an index entry is inserted in a full page

the data is split between two pagesBy default each page is filled to allow two

additions

Page 5: Indexes

Fill FactorAllows you to specify how much ‘room’ to

leave open on each pageSpecify the percentage of each page that is

used initially to store dataImpacts how often page splits occur

OLTP should have low percentageOLAP should have high percentage

Page 6: Indexes

“Good Index” CharacteristicsLow maintenance columns

Values don’t change frequentlyIncludes columns used frequently in Where

clauseDon’t have to go out to table for data

Covered indexes include multiple columns where related data is commonly used

Help search for ranges of records

Page 7: Indexes

“Poor Index” CharacteristicsColumns not used in queries (where clause)Index doesn’t return one or few rows

(selectivity)Exception: indexing foreign keys can speed up

joins significantlyMany columns in indexFew records in table

Page 8: Indexes

Unique Constraint v. Unique IndexNo significant differences between creating a

unique constraint and a unique indexData validation occurs in the same mannerQuery optimizer does not differentiate

between a unique index created by a constraint or created manually

Page 9: Indexes

Unique Constraint or Unique Index?A unique index makes sense when

uniqueness is a characteristic of the dataKnow that will always have a different

valueIf uniqueness must be enforced to ensure

data integrity, create a unique constraintNeed to ensure that have a different

valueUnique constraint allows null

Null treated as a value so can only occur once