资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
为了适应公司新战略的发展,保障停车场安保新项目的正常、顺利开展,特制定安保从业人员的业务技能及个人素质的培训计划sql,server,如何查看执行计划看懂执行计划例子1以AdventureWorks的DatabaseLog查询为例:SELECT*FROMAdventureWorksXXR2.dbo.DatabaseLog?Cachedplansizehowmuchmemorytheplangeneratedbythisquerywilltakeupinstoredprocedurecache.Thisisausefulnumberwheninvestigatingcacheperformanceissuesbecauseyoullbeabletoseewhichplansaretakingupmorememory.?EstimatedOperatorCostwevealreadyseenthisasthepercentagecostinFigure1.?EstimatedSubtreeCosttellsustheaccumulatedoptimizercostassignedtothisstepandallprevioussteps,butremembertoreadfromrighttoleft.Thisnumberismeaninglessintherealworld,butisamathematicalevaluationusedbythequeryoptimizertodeterminethecostoftheoperatorinquestion;itrepresentstheamountoftimethattheoptimizerthinksthisoperatorwilltake.?Estimatednumberofrowscalculatedbasedonthestatisticsavailabletotheoptimizerforthetableorindexinquestion.Thelogicaloperatorsaretheresultsoftheoptimizerscalculationsforwhatshouldhappenwhenthequeryexecutes.Thephysicaloperatorsrepresentwhatactuallyoccurred.Thelogicalandphysicaloperatorsareusuallythesame,butnotalwaysTheSubtreeissimplythesectionoftheexecutiontreethatwehavelookedatsofar,workingrighttoleftagain,andtoptobottom.Allestimationsarebasedonthestatisticsavailableonthecolumnsandindexesinanytable.TheI/OCostandCPUcostarenotactualoperators,butratherthecostnumbersassignedbytheQueryOptimizerduringitscalculations.ThesenumbersareusefulwhendeterminingwhethermostofthecostisI/O-based(asinthiscase),orifwereputtingaloadontheCPU.Abiggernumbermeansmoreprocessinginthisarea.Youllnotethat,inthiscase,theoperatorcostandthesubtreecostarethesame,sincethetablescanistheonlyoperator.Formorecomplextrees,withmoreoperators,youllseethatthecostaccumulatesastheindividualcostforeachoperatorisaddedtothetotal.Yougetthefullcostoftheplanfromthefinaloperationinthequeryplan,inthiscasetheSelectoperator.Againweseetheestimatednumberofrows.Thisisdisplayedforeachoperationbecauseeachoperationisdealingwithdifferentsetsofdata.Whenwegettomorecomplicatedexecutionplans,youllseethenumberofrowschangeasvariousoperatorsperformtheirworkonthedataasitpassesbetweeneachoperator.Knowinghowtherowsareaddedorfilteredoutbyeachoperatorhelpsyouunderstandhowthequeryisbeingperformedwithintheexecutionprocess.Anotherimportantpieceofinformation,whenattemptingtotroubleshootperformanceissues,istheBooleanvaluedisplayedforOrdered.Thistellsyouwhetherornotthedatathatthisoperatorisworkingwithisinanorderedstate.Certainoperations,forexample,anORDERBYclauseinaSELECTstatement,mayrequiredatatobeplacedinaparticularorder,sortedbyaparticularvalueorsetofvalues.KnowingwhetherornotthedataisinanOrderedstatehelpsshowwhereextraprocessingmaybeoccurringtogetthedataintothatstate.NodeIDistheordinal,whichsimplymeansnumberedinorder,ofthenodeitself,interestinglyenoughnumberedlefttoright,despitethefactthattheoperationsarebestreadrighttoleft.右键查看节点属性:文本格式显示执行计划:SETSHOWPLAN_ALLON;后续TSQL将显示预估的执行计划,不真正执行恢复:SETSHOWPLAN_ALLOFF;如果要查看实际执行计划:SETSTATISTICSPROFILEON例子2参考:首先,打开【SQLServerManagementStudio】,输入一个查询语句看看SQLServer是如何显示查询计划的吧。select,fromOrdersViewasvwhere=XX-12-1and=XX-12-1and10)等操作时,效率会较好。下面我们在Id列来对此表加上一个聚集索引CREATECLUSTEREDINDEXIX_IdONPerson(Id)再次执行同样的查询语句:SELECT*FromPersonWHEREName=公子执行计划如下:为什么建的聚集索引在Id列,会对扫描有影响呢?更何况与Name条件也没关系啊?其实,你加了聚集索引之后,表就由堆表变成了聚集表。我们知道聚集表的数据存在于聚集索引的叶级节点。因此,聚集扫描与表扫描其实差别不大,要说差别大,也得看where条件里是什么,以后返回的数据。就本条SQL语句而言,效率差别并不大。可以看看I/O统计信息:表扫描:聚集索引扫描:此处超出本文范畴了,效率不在本文考虑范围内,本文只考虑的是,各种扫描的区别,以及为何会产生。3、聚集索引查找聚集索引查找:扫描聚集索引中特定范围的行。看执行以下SQL语句:SELECT*FROMPersonWHEREId=73164执行计划如下:4、索引扫描索引扫描:整体扫描非聚集索引。下面我们来添加一个聚集索引,并执行一条查询语句:CREATENONCLUSTEREDINDEXIX_NameONPerson(Name)-创建非聚集索引SELECTNameFROMPerson查看执行计划如下:为什么此处会选择索引扫描(非聚集索引)呢?因为此非聚集索引能够覆盖所需要的数据。如果非聚集索引不能覆盖呢?例如,我们将SELECT改为SELECT*再来看看。好明显,返回结果所包括的记录太多,用非聚集索引反而不合算。因此使用了聚集索引。如果此时我们删除聚集索引,再执行SELECT*看看。DROPINDEX_Id而此时没有聚集索引,所以只有使用表扫描。5、书签查找前面关于索引的学习我们已经知道,当在非聚集索引中并非覆盖和包含所需全部的列时,SQLServer会选择,直接进行聚集索引扫描获得数据,还是先去非聚集索引找到聚集索引键,然后利用聚集索引找到数据。下面来看一个书签查找的示例:SELECT*FROMPersonWHEREName=胖胖-Name列有非聚集索引执行计划如下:上面的过程可以理解为:首先通过非聚集索引找到所求的行,但这个索引并不包含所有的列,因此还要额外去基本表中找到这些列,因此要进行键查找,如果基本表是以堆进行组织的,那么这个键查找(KeyLookup)就会变成RID查找(RIDLookup),键查找和RID查找统称为书签查找。不过有时当非聚集索引返回的行数过多时,SQLServer可能会选择直接进行聚集索引扫描了。目的-通过该培训员工可对保安行业有初步了解,并感受到安保行业的发展的巨大潜力,可提升其的专业水平,并确保其在这个行业的安全感。
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号