Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SQLServer2012:SQLServerArchitectureTHELIFECYCLEOFAQUERY(part2)PlanCacheMicrosoftCertificationExamples,exercises,pr
GameTrailer
NaturalMiscarriage
Windows
Office
Database
ApplicationServer
Graphics
Mobile
SQLServer
Search
SQLServer2012:SQLServerArchitectureTHELIFECYCLEOFAQUERY(part2)PlanCache
HowTakeScreenshotsInMSWord2013
NOWPLAYING
Videotutorials
How To Install Windows 8 On VMware Workstation
HowToInstallWindows8
0:37/0:37
HowToInstallWindowsServer2012
6/26/20138:54:45PM
HowToDisableWindows8MetroUI
Video:HowtocreateyourfirstSwimlaneDiagramorCrossFunctionalFlowchartDiagrambyusingMicrosoftVisio
2010
HowToChangeAccountPictureInWindows8
Video:HowToInstallWindowsServer2012OnVirtualBox
Video:HowToInstallWindowsStoreAppsFromWindows8ClassicDesktop
HowToRestart,LogOffAndShutdownWindows8
1.PlanCache
HowToLoginToSkypeUsingAMicrosoftAccount
Creatingexecutionplanscanbetimeconsumingandresourceintensive,soitmakessensethatifSQLServerhas
alreadyfoundagoodwaytoexecuteapieceofcodethatitshouldtrytoreuseitforsubsequentrequests.
HowToEnableAeroGlassEffectInWindows8
HowToDisableWindowsUpdateinWindows8
Ifnocachedplanisfound,thentheCommandParsergeneratesaquerytreebasedontheTSQL.Aquerytreeisan
internal structure whereby each node in the tree represents an operation in the query that needs to be performed.
ThistreeisthenpassedtotheQueryOptimizertoprocess.Ourbasicquerydidnthaveanexistingplansoaquery
treewascreatedandpassedtotheQueryOptimizer.
Figure 3 shows the plan cache added to the diagram, which is checked by the Command Parser for an existing
query plan. Also added is the query tree output from the Command Parser being passed to the optimizer because
nothingwasfoundincacheforourquery.
FIGURE3
HowToDisableWindows8MetroUI
HowToAddWidgetsToWindows8LockScreen
Top10
Windows10:SystemMaintenanceand
PerformanceUsingWindows
Troubleshooters
Windows10:SystemMaintenanceand
PerformanceCheckingDrivesforErrors
withThisPC
Windows10:SystemMaintenanceand
PerformanceCheckingDrivesforErrors
WhenConnected
Windows10:SystemMaintenanceand
PerformanceViewingDiskInformation
Windows10:SystemMaintenanceand
PerformanceSelectingaPowerScheme
Windows10:SystemMaintenanceand
PerformanceCheckingChargeLevel
Windows10:HowtoCustomizetheRight
Side(part3)Eliminatealltiles
Windows10:HowtoCustomizetheRight
Side(part2)Groupyourtiles
Windows10:HowtoCustomizetheRight
Side(part1)Resizeatile
ImplementingEdgeServicesforan
ExchangeServer2007Environment:
UtilizingtheBasicSenderandRecipient
ConnectionFilters(part3)Configuring
RecipientFiltering
http://mscerts.wmlcloud.com/sql_server/SQL%20Server%202012%20%20%20SQL%20Server%20Architecture%20%20THE%20LIFE%20CYCLE%20OF%2
1/5
12/31/2015
SQLServer2012:SQLServerArchitectureTHELIFECYCLEOFAQUERY(part2)PlanCacheMicrosoftCertificationExamples,exercises,pr
Populartags
QueryOptimizer
The Query Optimizer is the most prized possession of the SQL Server team and one of the most complex and
secretive parts of the product. Fortunately, its only the lowlevel algorithms and source code that are so well
protected(evenwithinMicrosoft),andresearchandobservationcanrevealhowtheOptimizerworks.
Itiswhatsknownasacostbasedoptimizer,whichmeansthatitevaluatesmultiplewaystoexecuteaqueryand
thenpicksthemethodthatitdeemswillhavethelowestcosttoexecute.Thismethodofexecutingisimplemented
asaqueryplanandistheoutputfromtheQueryOptimizer.
Basedonthatdescription,youwouldbeforgivenforthinkingthattheOptimizersjobistofindthebest query plan
becausethatwouldseemlikeanobviousassumption.Itsactualjob,however,istofindagoodplaninareasonable
amount of time, rather than the best plan. The optimizers goal is most commonly described as finding the most
efficientplan.
If the Optimizer tried to find the best plan every time, it might take longer to find the plan than it would to just
executeaslowerplan(somebuiltinheuristicsactuallyensurethatitnevertakeslongertofindagoodplanthanit
doestojustfindaplanandexecuteit).
As well as being cost based, the Optimizer also performs multistage optimization, increasing the number of
decisionsavailabletofindagoodplanateachstage.Whenagoodplanisfound,optimizationstopsatthatstage.
Thefirststageisknownaspreoptimization,andqueriesdropoutoftheprocessatthisstagewhenthestatementis
simpleenoughthattherecanonlybeoneoptimalplan,removingtheneedforadditionalcosting.Basicquerieswith
nojoinsareregardedassimple,andplansproducedassuchhavezerocost(becausetheyhavent been costed)
andarereferredtoastrivialplans.
Thenextstageiswhereoptimizationactuallybegins,anditconsistsofthreesearchphases:
Phase0Duringthisphasetheoptimizerlooksatnestedloopjoinsandwontconsiderparalleloperators.
Theoptimizerwillstophereifthecostoftheplanithasfoundis<0.2.Aplangeneratedatthisphaseisknownasa
transactionprocessing,orTP,plan.
Phase 1 Phase 1 uses a subset of the possible optimization rules and looks for common patterns for
whichitalreadyhasaplan.
The optimizer will stop here if the cost of the plan it has found is < 1.0. Plans generated in this phase are called
quickplans.
Phase 2 This final phase is where the optimizer pulls out all the stops and is able to use all of its
optimizationrules.Italsolooksatparallelismandindexedviews(ifyourerunningEnterpriseEdition).
Completion of Phase 2 is a balance between the cost of the plan found versus the time spent optimizing. Plans
createdinthisphasehaveanoptimizationlevelofFull.
HOWMUCHDOESITCOST?
Thetermcostdoesnttranslateintosecondsoranythingmeaningfulitisjustanarbitrarynumberusedtoassigna
value representing the resource cost for a plan. However, its origin was a benchmark on a desktop computer at
MicrosoftearlyinSQLServerslife.
In a plan, each operator has a baseline cost, which is then multiplied by the size of the row and the estimated
numberofrowstogetthecostofthatoperatorandthecostoftheplanisthetotalcostofalltheoperators.
Becausecostiscreatedfromabaselinevalueandisntrelatedtothespeedofyourhardware,anyplancreatedwill
havethesamecostoneverySQLServerinstallation(likeforlikeversion).
Because our SELECT query is very simple, it drops out of the process in the preoptimization phase because the
http://mscerts.wmlcloud.com/sql_server/SQL%20Server%202012%20%20%20SQL%20Server%20Architecture%20%20THE%20LIFE%20CYCLE%20OF%2
2/5
12/31/2015
SQLServer2012:SQLServerArchitectureTHELIFECYCLEOFAQUERY(part2)PlanCacheMicrosoftCertificationExamples,exercises,pr
plan is obvious to the optimizer (a trivial plan). Now that there is a query plan, its on to the Query Executor for
execution.
QueryExecutor
TheQueryExecutorsjobisselfexplanatoryitexecutesthequery.Tobemorespecific,itexecutesthequeryplan
byworkingthrougheachstepitcontainsandinteractingwiththeStorageEnginetoretrieveormodifydata.
NOTE
The interface to the Storage Engine is actually OLE DB, which is a legacy from a design decision made in SQL
Servers history. The development teams original idea was to interface through OLE DB to allow different Storage
Enginestobepluggedin.However,thestrategychangedsoonafterthat.
The idea of a pluggable Storage Engine was dropped and the developers started writing extensions to OLE DB to
improveperformance.Thesecustomizationsarenow core to the product and while theres now no reason to have
OLEDB,theexistinginvestmentandperformanceprecludesanyjustificationtochangeit.
The SELECT query needs to retrieve data, so the request is passed to the Storage Engine through an OLE DB
interfacetotheAccessMethods.
Figure4showstheadditionofthequeryplanastheoutputfromtheOptimizerbeingpassedtotheQueryExecutor.
Also introduced is the Storage Engine, which is interfaced by the Query Executor via OLE DB to the Access
Methods(comingupnext).
FIGURE4
AccessMethods
Access Methods is a collection of code that provides the storage structures for your data and indexes, as well as
the interface through which data is retrieved and modified. It contains all the code to retrieve data but it doesnt
actuallyperformtheoperationitselfitpassestherequesttotheBufferManager.
SupposeourSELECTstatementneedstoreadjustafewrowsthatareallonasinglepage.The Access Methods
codewillasktheBufferManagertoretrievethepagesothatitcanprepareanOLEDBrowsettopassbacktothe
RelationalEngine.
BufferManager
TheBufferManager,asitsnamesuggests,managesthebufferpool,whichrepresentsthemajorityofSQLServers
memory usage. If you need to read some rows from a page (youll look at writes when we look at an UPDATE
query), the Buffer Manager checks the data cache in the buffer pool to see if it already has the page cached in
memory.Ifthepageisalreadycached,thentheresultsarepassedbacktotheAccessMethods.
Ifthepageisntalreadyincache,thentheBuffer Manager gets the page from the database on disk, puts it in the
datacache,andpassestheresultstotheAccessMethods.
Thekeypointtotakeawayfromthisisthatyouonlyeverworkwithdatainmemory.Everynewdatareadthatyou
requestisfirstreadfromdiskandthenwrittentomemory(thedatacache)beforebeingreturnedasaresultset.
ThisiswhySQLServerneedstomaintainaminimumleveloffreepagesinmemoryyouwouldntbeabletoread
anynewdataiftherewerenospaceincachetoputitfirst.
The Access Methods code determined that the SELECT query needed a single page, so it asked the Buffer
Managertogetit.TheBufferManagercheckedwhetheritalreadyhaditinthedatacache,and then loaded it from
diskintothecachewhenitcouldntfindit.
http://mscerts.wmlcloud.com/sql_server/SQL%20Server%202012%20%20%20SQL%20Server%20Architecture%20%20THE%20LIFE%20CYCLE%20OF%2
3/5
12/31/2015
SQLServer2012:SQLServerArchitectureTHELIFECYCLEOFAQUERY(part2)PlanCacheMicrosoftCertificationExamples,exercises,pr
DataCache
Thedatacacheisusuallythelargestpartofthebufferpooltherefore,itsthelargestmemoryconsumerwithin SQL
Server.Itisherethateverydatapagethatisreadfromdiskiswrittentobeforebeingused.
Thesys.dm_os_buffer_descriptorsDMVcontainsonerowforeverydatapagecurrentlyheldincache.Youcan use
thisscripttoseehowmuchspaceeachdatabaseisusinginthedatacache:
SELECTcount(*)*8/1024AS'CachedSize(MB)'
,CASEdatabase_id
WHEN32767THEN'ResourceDb'
ELSEdb_name(database_id)
ENDAS'Database'
FROMsys.dm_os_buffer_descriptors
GROUPBYdb_name(database_id),database_id
ORDERBY'CachedSize(MB)'DESC
Theoutputwilllooksomethinglikethis(withyourowndatabases,obviously):
CachedSize(MB)Database
3287People
34tempdb
12ResourceDb
4msdb
Inthisexample,thePeopledatabasehas3,287MBofdatapagesinthedatacache.
Theamountoftimethatpagesstayincacheisdeterminedbyaleastrecentlyused(LRU)policy.
The header of each page in cache stores details about the last two times it was accessed, and a periodic scan
through the cache examines these values. A counter is maintained that is decremented if the page hasnt been
accessed for a while and when SQL Server needs to free up some cache, the pages with the lowest counter are
flushedfirst.
The process of aging out pages from cache and maintaining an available amount of free cache pages for
subsequent use can be done by any worker thread after scheduling its own I/O or by the lazy writer process,
coveredlaterinthesectionLazyWriter.
You can view how long SQL Server expects to be able to keep a page in cache by looking at the
MSSQL$<instance>:Buffer Manager\Page Life Expectancy counter in Performance Monitor. Page life expectancy
(PLE)istheamountoftime,inseconds,thatSQLServerexpectstobeabletokeepapageincache.
Under memory pressure, data pages are flushed from cache far more frequently. Microsoft has a long standing
recommendation for a minimum of 300 seconds for PLE but a good value is generally considered to be 1000s of
seconds these days. Exactly what your acceptable threshold should be is variable depending on your data usage,
butmoreoftenthannot,youllfindserverswitheither1000sofsecondsPLEoralotlessthan300,soitsusually
easytospotaproblem.
ThedatabasepagereadtoservetheresultsetforourSELECTqueryisnowinthedatacacheinthebufferpooland
will have an entry in the sys.dm_os_buffer_descriptors DMV. Now that the Buffer Manager has the result set, its
passedbacktotheAccessMethodstomakeitswaytotheclient.
ABasicSELECTStatementLifeCycleSummary
Figure5showsthewholelifecycleofaSELECTquery,describedhere:
FIGURE5
http://mscerts.wmlcloud.com/sql_server/SQL%20Server%202012%20%20%20SQL%20Server%20Architecture%20%20THE%20LIFE%20CYCLE%20OF%2
4/5
12/31/2015
SQLServer2012:SQLServerArchitectureTHELIFECYCLEOFAQUERY(part2)PlanCacheMicrosoftCertificationExamples,exercises,pr
1. The SQL Server Network Interface (SNI) on the client established a connection to the SNI on the SQL Server
using a network protocol such as TCP/IP. It then created a connection to a TDS endpoint over the TCP/IP
connectionandsenttheSELECTstatementtoSQLServerasaTDSmessage.
2. The SNI on the SQL Server unpacked the TDS message, read the SELECT statement, and passed a SQL
CommandtotheCommandParser.
3.TheCommandParsercheckedtheplancacheinthebufferpoolforanexisting,usablequery plan that matched
thestatementreceived.Whenitdidntfindone,itcreatedaquerytreebasedontheSELECTstatementandpassed
ittotheOptimizertogenerateaqueryplan.
4.TheOptimizergeneratedazerocostortrivialplaninthepreoptimizationphasebecausethestatementwasso
simple.ThequeryplancreatedwasthenpassedtotheQueryExecutorforexecution.
5. At execution time, the Query Executor determined that data needed to be read to complete the query plan so it
passedtherequesttotheAccessMethodsintheStorageEngineviaanOLEDBinterface.
6.TheAccessMethodsneededtoreadapagefromthedatabasetocompletetherequestfromtheQueryExecutor
andaskedtheBufferManagertoprovisionthedatapage.
7. The Buffer Manager checked the data cache to see if it already had the page in cache. It wasnt in cache so it
pulledthepagefromdisk,putitincache,andpasseditbacktotheAccessMethods.
8.Finally,theAccessMethodspassedtheresultsetbacktotheRelationalEnginetosendtotheclient.
Related
SQLServer2012:SQLServerArchitectureTHELIFECYCLEOFAQUERY(part3)ASimpleUpdate
Query
SQLServer2012:SQLServerArchitectureTHELIFECYCLEOFAQUERY(part1)
Other
ProtectingSQLServerData:CELLLEVELENCRYPTIONViewsandStoredProcedures(part2)
CreatingtheStoredProcedures
ProtectingSQLServerData:CELLLEVELENCRYPTIONViewsandStoredProcedures(part1)
CreatingtheView
ProtectingSQLServerData:ImplementingCellLevelEncryption
ProtectingSQLServerData:PreparingforCellLevelEncryption
MicrosoftSQLServer2008R2:MonitoringReplication(part2)NewandImprovedPeertoPeer
Replication
MicrosoftSQLServer2008R2:MonitoringReplication(part1)ReplicationMonitoringSQL
Statements
MicrosoftSQLServer2008R2:ScriptingReplication
ProcessingandStoringDatainSQLServer2005:DataMigrationfromOneDataStoretoAnotherData
Store
ProcessingandStoringDatainSQLServer2005:ImplementingtheRecordFailureCode
ProcessingandStoringDatainSQLServer2005:DataTrackingValidation
ProcessingandStoringDatainSQLServer2005:UpdatingtheFileWorkerClass
MicrosoftSQLServer2008R2:SettingUpReplication(part4)CreatingSubscriptions
MicrosoftSQLServer2008R2:SettingUpReplication(part3)HorizontalandVerticalFiltering
MicrosoftSQLServer2008R2:SettingUpReplication(part2)CreatingaPublication
MicrosoftSQLServer2008R2:SettingUpReplication(part1)CreatingaDistributorandEnabling
Publishing
SQLServer2008R2:BasingtheReplicationDesignonUserRequirements
SQLServer2008R2:PlanningforSQLServerDataReplication&SQLServerReplicationTypes
SQLServer2008R2:ReplicationAgents
SQLServer2008:ReplicationSubscriptions
SQLServer2008:ReplicationScenarios
VideoChannel
Windows
Office
Database
ApplicationServer
http://mscerts.wmlcloud.com/sql_server/SQL%20Server%202012%20%20%20SQL%20Server%20Architecture%20%20THE%20LIFE%20CYCLE%20OF%2
5/5