Beruflich Dokumente
Kultur Dokumente
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
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.