Sie sind auf Seite 1von 4

ClusteredIndexvsNonClusteredIndex

1Votes InSQLdatabase,therearetwodifferenttypesofindexingand
thoseareClusteredIndex
andNonClusteredIndex.
Beforegoingtothistopicletsdiscussaboutindexingindatabase.

WhatisIndexing
Indexisoneofthedatabaseobjectandindexingcanbedoneinoneormorecolumnsofthe
databasetable.Thepurposeofdoingindexingissimplei.e,tospeeduptheretrievalprocessof
rowsinthetable.Indexingcanbeappliedtotablesorviewstospeeduptheretrievalprocess.
Indexcanbeconsideredascopyofthedataoftableandwillbesortedbasedonsomeway.For
exampleifwehaveatablecalledCategoryasshownbelow

CategoryTable

CategoryIDINTPK
CategoryNameVARCHAR(50)

CategoryAddedDATETIME
CategoryDeletedDATETIME

AsshownaboveinthetablestructureCategoryIDisaprimarykeyandwilldefaulthavethe
clusteredindex.Soallprimarykeyswilldefaulthavetheclusteredindexassociatedwithit.Now
consideranotherscenariowhereinwesearchtherecordsbasedonCategoryNameandsaythe
tablehasmorethan1millionrecordsinthattableitwillsurelygivetheperformanceissueswhile
filteringrecordsbasedonCategoryNameasitdoesfulltablescaninternally.

SotoavoidtheperformanceissueswecansetCategoryNameasNonclusteredIndex.Sonow
itsortsthedatabasedonCategoryNameandstoresitinthediskofthemachineandhugely
increasestheperformanceaswell.

ClusteredIndex
LetsaddsomedataintothetableCategoryandletsseehowthetablesearchhappensthrough
queryandletsseehowwecanminimizenumberofcomparisonsusingclusteredindex
CategoryID CategoryName CategoryAdded CategoryDeleted

1 Category1 NULL NULL

2 Category2 NULL NULL

3 Category3 NULL NULL

4 Category4 NULL NULL

5 Category5 NULL NULL

Belowisthesampleimageofhowthetablescanishappenedwithoutclusteredindex

Belowisthesampleimageofhowthetabledataisstructuredafterclusteredindex

Asshowninthefirstfigureabove,tableCategoryhaveprimarykeyCategoryIDwhichisby
defaulthaveaclusteredindex.Primarykeyasweknowdoesnotallownullanddoesnotallow
duplicatevalues.

BelowarethelistofpointstobekeptinmindwhilecreatingaClusteredIndex

Therecouldbeonlyoneclusteredindexallowedinatable.

Itisgoodtoaddtheclusteredindextothecolumnwhichisusedmostlyinwhereclausethe
most.Becauseitgreatlyaffectstheperformanceofthetable.
Wheneverindexischanged,reorderingofdataishappenedinthetable.

Whenclusteredindexiscreateditformsabinarytreeandbinarytreeinfostoredinthedisc
separately.Samplebinarytreediagramshownabove.

DuringUpdate,Delete,Insertstatementsreorderingwillhappeninthetable.

Nowletsseehowthedatacomparedinbinarytreeinaquery.SupposeIhaveaquerytofindthe
categoryhavingCategoryID=5

Select*fromCategorywhereCategoryID=5

Asshownintheabovebinarytreefigure

5ismatchedwith3,

5ismatchedwith4

5ismatchedwith5soonly3comparisons.

Soheretotalcomparisonsare3whichislesscomparedtocomparisonswithoutclusteredindex5.

NonClusteredIndex
Nonclusteredindexwillbehelpfulforthesituationwhereinonecolumnifwehavemultiple
repeatedvalues.ForexampleCategoryAddedcolumncanhavesamedateincasecategory
addedatthesamedateandtime.

Nonclusteredindexhastobemanuallycreatedunlikeclusteredindexwhichwillbecreatedon
creatingtheprimarykeyforatable.Wecanaddmorethanonenonclusteredindexinatableand
wecancombinemorethanonecolumnfornonclusteredindex.
Nonclusteredindexalwaysdependontheclusteredindexofthetable.Thiscanbeexplainedwith
anexampleofbook.Bylookingattheindexpagewecanfigureoutthetopicfromthepage
numberasitisalreadysorted.Andwhilesearchingforthepagenumber(say200)youmayendup
beinginpage300thenyouwillgobacktofindthepagenumber200orifyouendupbeingin
page100thenyouwillgoforwardtofindthepagenumber200.

Sothisconceptissimilartoclusteredindexconceptofbinarytree.Sofromthisconceptwecan
makeoutnonclusteredindexdependonclusteredindex.

BelowarethepointsofNonClusteredindexhastobekeptinmindwhilecreatingNonClustered
Index

Nonclusteredindexactslikeapointertothedatafortherowsinatable.

Nonclusteredindexwilldependonthenumberofrecordswhicharefetchedfromour
applicationaswell.Ifitismorethatmakesensetoaddanonclusteredindextothecolumnon
whichwearefilteringthedata.Meantimeaddingmorenonclusteredindexinatablecanaffect
theperformanceofthetableaswell.

Incasecolumnotherthanprimarykeymarkedasclusteredindexthenprimarykeywillby
defaultbenonclusteredindex.

Wecancreateanonclusteredindexbyexpandingthecategorytableandrightclickonthe
indexesfolder.SohereNonclusteredindexwillbelikepointerstothebinarytreecreatedin
clusteredindexandwhilefetchingthedataitcheckthedatatobetraversedfromthepointer.

Ifthetablehasnotindexeditwouldbydefaultusetheheapstructureanddoatablescan.So
onceaftercreatingindexitwilluseseekwhichdoesnotcomparealltherecordsanditshown
intheabovecomparisonsaswell.

Das könnte Ihnen auch gefallen