|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
如果WHERE子句的查询条件里使用比较操作符LIKE和REGEXP,MySQL只有在搜索模板的第一个字符不是通配符的情况下才能使用索引。比如说,如果查询条件是LIKEabc%‘,MySQL将使用索引;如果查询条件是LIKE%abc’,MySQL将不使用索引。
IfyoucreateatableonAdaptiveServer,butdonotcreateaclusteredindex,thetableisstoredasaheap.Thedatarowsarenotstoredinanyparticularorder.Thissectiondescribeshowselect,insert,delete,andupdateoperationsperformonheapswhenthereisno"useful"indextoaidinretrievingdata.
Thephrase"nousefulindex"isimportantindescribingtheoptimizersdecisiontoperformatablescan.Sometimes,anindexexistsonthecolumnsnamedinawhereclause,buttheoptimizerdeterminesthatitwouldbemorecostlytousetheindexthantoperformatablescan.
Otherchaptersinthisbookdescribehowtheoptimizercostsqueriesusingindexesandhowyoucangetmoreinformationaboutwhytheoptimizermakesthesechoices.
Tablescansarealwaysusedwhenyouselectallrowsinatable.Theonlyexceptioniswhenthequeryincludesonlycolumnsthatarekeysinanonclusteredindex.
Formoreinformation,see"Indexcovering".
ThefollowingsectionsdescribehowAdaptiveServerlocatesrowswhenatablehasnousefulindex.
Lockschemesanddifferencesbetweenheaps
Thedatapagesinanallpages-lockedtablearelinkedintoadoubly-linkedlistofpagesbypointersoneachpage.Pagesindata-only-lockedtablesarenotlinkedintoapagechain.
Inanallpages-lockedtable,eachpagestoresapointertothenextpageinthechainandtothepreviouspageinthechain.Whennewpagesneedtobeinserted,thepointersonthetwoadjacentpageschangetopointtothenewpage.WhenAdaptiveServerscansanallpages-lockedtable,itreadsthepagesinorder,followingthesepagepointers.
Pagesarealsodoubly-linkedateachindexlevelofallpages-lockedtables,andtheleaflevelofindexesondata-only-lockedtables.Ifanallpages-lockedtableispartitioned,thereisonepagechainforeachpartition.
Anotherdifferencebetweenallpages-lockedtablesanddata-only-lockedtablesisthatdata-only-lockedtablesusefixedrowIDs.ThismeansthatrowIDs(acombinationofthepagenumberandtherownumberonthepage)donotchangeinadata-only-lockedtableduringnormalqueryprocessing.
RowIDschangeonlywhenoneoftheoperationsthatrequiredata-rowcopyingisperformed,forexample,duringreorgrebuildorwhilecreatingaclusteredindex.
ForinformationonhowfixedrowIDsaffectheapoperations,see"Deletingfromadata-onlylockedheaptable"and"Data-only-lockedheaptables".
Selectoperationsonheaps
Whenyouissueaselectqueryonaheap,andthereisnousefulnonclusteredindex,AdaptiveServermustscaneverydatapageinthetabletofindeveryrowthatsatisfiestheconditionsinthequery.Theremaybeonerow,manyrows,ornorowsthatmatch.
Allpages-lockedheaptables
Forallpages-lockedtables,AdaptiveServerreadsthefirstcolumninsysindexesforthetable,readsthefirstpageintocache,andfollowsthenextpagepointersuntilitfindsthelastpageofthetable.
Data-onlylockedheaptables
Sincethepagesofdata-only-lockedtablesarenotlinkedinapagechain,aselectqueryonaheaptableusesthetablesOAMandtheallocationpagestolocatealltherowsinthetable.TheOAMpagepointstotheallocationpages,whichpointtotheextentsandpagesforthetable.
Insertingdataintoanallpages-lockedheaptable
Whenyouinsertdataintoanallpages-lockedheaptable,thedatarowisalwaysaddedtothelastpageofthetable.Ifthereisnoclusteredindexonatable,andthetableisnotpartitioned,thesysindexes.rootentryfortheheaptablestoresapointertothelastpageoftheheaptolocatethepagewherethedataneedstobeinserted.
Ifthelastpageisfull,anewpageisallocatedinthecurrentextentandlinkedontothechain.Iftheextentisfull,AdaptiveServerlooksforemptypagesonotherextentsbeingusedbythetable.Ifnopagesareavailable,anewextentisallocatedtothetable.
Conflictsduringheapinserts
Oneofthesevereperformancelimitsonheaptablesthatuseallpageslockingisthatthepagemustbelockedwhentherowisadded,andthatlockishelduntilthetransactioncompletes.Ifmanyusersaretryingtoinsertintoanallpages-lockedheaptableatthesametime,eachinsertmustwaitfortheprecedingtransactiontocomplete.
Thisproblemoflast-pageconflictsonheapsistruefor:
Singlerowinsertsusinginsert
Multiplerowinsertsusingselectintoorinsert...select,orseveralinsertstatementsinabatch
Bulkcopyintothetable
Someworkaroundsforlast-pageconflictsonheapsinclude:
Switchingtodatapagesordatarowslocking
Creatingaclusteredindexthatdirectstheinsertstodifferentpages
Partitioningthetable,whichcreatesmultipleinsertpointsforthetable,givingyoumultiple"lastpages"inanallpages-lockedtable
Otherguidelinesthatapplytoalltransactionswheretheremaybelockconflictsinclude:
Keepingtransactionsshort
Avoidingnetworkactivityanduserinteractionwheneverpossible,onceatransactionacquireslocks
Insertingdataintoadata-only-lockedheaptable
Whenusersinsertdataintoadata-only-lockedheaptable,AdaptiveServertrackspagenumberswheretheinsertshaverecentlyoccurred,andkeepsthepagenumberasahintforfuturetasksthatneedspace.Subsequentinsertstothetablearedirectedtooneofthesepages.Ifthepageisfull,AdaptiveServerallocatesanewpageandreplacestheoldhintwiththenewpagenumber.
Blockingwhilemanyusersaresimultaneouslyinsertingdataismuchlesslikelytooccurduringinsertstodata-only-lockedheaptables.Whenblockingoccurs,AdaptiveServerallocatesasmallnumberofemptypagesanddirectsnewinsertstothosepagesusingthesenewlyallocatedpagesashints.
Fordatarows-lockedtables,blockingoccursonlywhiletheactualchangestothedatapagearebeingwritten;althoughrowlocksareheldforthedurationofthetransaction,otherrowscanbeinsertedonthepage.Therow-levellocksallowmultipletransactiontoholdlocksonthepage.
Theremaybeslightblockingondata-only-lockedtables,becauseAdaptiveServerallowsasmallamountofblockingaftermanypageshavejustbeenallocated,sothatthenewlyallocatedpagesarefilledbeforeadditionalpagesareallocated.
Ifconflictsoccurduringheapinserts
Conflictsduringinsertstoheaptablesaregreatlyreducedfordata-only-lockedtables,butcanstilltakeplace.Iftheseconflictsslowinserts,someworkaroundscanbeused,including:
Switchingtodatarowslocking,ifthetableusesdatapageslocking
Usingaclusteredindextospreaddatainserts
Partitioningthetable,whichprovidesadditionalhintsandallowsnewpagestobeallocatedoneachpartitionwhenblockingtakesplace
Deletingdatafromaheaptable
Whenyoudeleterowsfromaheaptable,andthereisnousefulindex,AdaptiveServerscansthedatarowsinthetabletofindtherowstodelete.Ithasnowayofknowinghowmanyrowsmatchtheconditionsinthequerywithoutexaminingeveryrow.
Deletingfromanallpages-lockedheaptable
Whenadatarowisdeletedfromapageinanallpages-lockedtable,therowsthatfollowitonthepagemoveupsothatthedataonthepageremainscontiguous.
Deletingfromadata-onlylockedheaptable
Whenyoudeleterowsfromadata-only-lockedheaptable,atablescanisrequiredifthereisnousefulindex.TheOAMandallocationpagesareusedtolocatethepages.
Thespaceonthepageisnotrecoveredimmediately.Rowsindata-only-lockedtablesmustmaintainfixedrowIDs,andneedtobereinsertedinthesameplaceifthetransactionisrolledback.
Afteradeletetransactioncompletes,oneofthefollowingprocessesshiftsrowsonthepagetomakethespaceusagecontiguous:
Thehousekeeperprocess
Aninsertthatneedstofindspaceonthepage
Thereorgreclaim_spacecommand
Deletingthelastrowonapage
Ifyoudeletethelastrowonapage,thepageisdeallocated.Ifotherpagesontheextentarestillinusebythetable,thepagecanbeusedagainbythetablewhenapageisneeded.
Ifallotherpagesontheextentareempty,theentireextentisdeallocated.Itcanbeallocatedtootherobjectsinthedatabase.Thefirstdatapageforatableoranindexisneverdeallocated.
Updatingdataonaheaptable
Likeotheroperationsonheaps,anupdatethathasnousefulindexonthecolumnsinthewhereclauseperformsatablescantolocatetherowsthatneedtobechanged.
Allpages-lockedheaptables
Updatesonallpages-lockedheaptablescanbeperformedinseveralways:
Ifthelengthoftherowdoesnotchange,theupdatedrowreplacestheexistingrow,andnodatamovesonthepage.
Ifthelengthoftherowchanges,andthereisenoughfreespaceonthepage,therowremainsinthesameplaceonthepage,butotherrowsmoveupordowntokeeptherowscontiguousonthepage.
Therowoffsetpointersattheendofthepageareadjustedtopointtothechangedrowlocations.
Iftherowdoesnotfitonthepage,therowisdeletedfromitscurrentpage,andthe"new"rowisinsertedonthelastpageofthetable.
Thistypeofupdatecancauseaconflictonthelastpageoftheheap,justasinsertsdo.Ifthereareanynonclusteredindexesonthetable,allindexreferencestotherowneedtobeupdated.
Data-only-lockedheaptables
Oneoftherequirementsfordata-only-lockedtablesisthattherowIDofadatarowneverchanges(exceptduringintentionalrebuildsofthetable).Therefore,updatestodata-only-lockedtablescanbeperformedbythefirsttwomethodsdescribedabove,aslongastherowfitsonthepage.
Butwhenarowinadata-only-lockedtableisupdatedsothatitnolongerfitsonthepage,aprocesscalledrowforwardingperformsthefollowingsteps:
Therowisinsertedontoadifferentpage,and
ApointertotherowIDonthenewpageisstoredintheoriginallocationfortherow.
Indexesdonotneedtobemodifiedwhenrowsareforwarded.AllindexesstillpointtotheoriginalrowID.
Iftherowneedstobeforwardedasecondtime,theoriginallocationisupdatedtopointtothenewpage--theforwardedrowisnevermorethanonehopawayfromitsoriginallocation.
Rowforwardingincreasesconcurrencyduringupdateoperationsbecauseindexesdonothavetobeupdated.Itcanslowdataretrieval,however,becauseataskneedstoreadthepageattheoriginallocationandthenreadthepagewheretheforwardeddataisstored.
Forwardedrowscanbeclearedfromatableusingthereorgcommand.
Formoreinformationonupdates,see"Howupdateoperationsareperformed".
为了在某种程序上弥补这一缺陷,许多SQL命令都有一个DELAY_KEY_WRITE项。这个选项的作用是暂时制止MySQL在该命令每插入一条新记录和每修改一条现有之后立刻对索引进行刷新,对索引的刷新将等到全部记录插入/修改完毕之后再进行。 |
|