SQL Server - Bài
Optimizing Query PerformanceVu Tuyet Trinhtrinhvt-fit@mail.hut.edu.vnHanoi University of Technology1 MicrosoftMicrosoftOverview of Query ProcessingParsing ProcessStandardization ProcessQuery OptimizationCompilationDatabase Access RoutinesTransact-SQLResultsSet MicrosoftMicrosoftOutlineIntroduction to Query OptimizerObtaining Execution Plan InformationUsing an Index to Cover a QueryIndexing StrategiesOverriding the Query OptimizerDatabase Tuning Advisor MicrosoftMicrosoftQuery Optimizer IssuesFunctionCost-Based OptimizationHow It WorksPhasesCaching the Execution PlanSetting a Cost Limit MicrosoftMicrosoftCost-Based OptimizationLimits the Number of Optimization Plans to Optimize in Reasonable Amount of TimeCost is estimated in terms of I/O and CPU costDetermines Query Processing Time Use of physical operators and sequence of operationsUse of parallel and serial processing•To reduce the number of rows returned.•To reduce the number of pages read.•To reduce the overall processing time. MicrosoftMicrosoftQuery Optimization PhasesQuery AnalysisIdentifies the search and join criteria of the query. By limitingthe search, the query optimizer minimizes the number of rows that areprocessedIndex SelectionDetermines whether an index or indexes exist. Then, there is an assessment of the usefulness of the index or indexes.Usefulness of an index is determined by how many rows will be returnedAssesses the usefulness of the index or indexes Join SelectionEvaluates which join strategy to use by considering a number of factors: selectivity, density, memory required to process the query. MicrosoftMicrosoftCaching the Execution PlanStoring a Execution Plan in Memory One copy for all serial executionsAnother copy for all parallel executionsUsing an Execution ContextAn existing execution plan is reused, if one existsA new execution plan is generated, if one does not existRecompiling Execution PlansChanges can cause execution plan to be inefficient or invalid For example, a large number of new rows added ALTER TABLE/VIEW UPDATE STATISTICS Dropping an INDEX that is used Explicit sp_recompile MicrosoftMicrosoftSetting a Cost LimitSpecifying an Upper Limit (based on Estimated Costs)Use the query governor to prevent long-running queries from executing and consuming system resources Effectively controls run-away queriesSpecifying Connection LimitsUse the sp_configure stored procedureExecute the SET QUERY_GOVERNOR_COST_LIMIT statement Specify 0 to turn off the query governor MicrosoftMicrosoftObtaining Execution Plan InformationViewing STATISTICS Statements OutputViewing SHOWPLAN_ALL and SHOWPLAN_TEXT OutputGraphically Viewing the Execution Plan MicrosoftMicrosoftSTATISTICS Statements OutputStatementStatementOutput SampleOutput SampleSTATISTICSTIMESTATISTICSPROFILESTATISTICS IOSQL Server Execution Times:CPU time = 0 ms, elapsed time = 2 ms.Rows Executes StmtText StmtId…-----------------------------------------------47 1 SELECT * FROM [charge] 16 WHERE (([charge_amt]>=@1) . . .Table 'member'. Scan count 1,logical reads 23, physical reads 0, read-ahead reads 0. [...]... Nash … Nay … Ota … Rudd … Smith Smith Smith Smith Smith … … … … … Microsoft To retrieve the information, SQL Server: 1 Traverses the index tree 2 Reads the leaf-level pages, starting with the first page, and scans through all of the leaflevel pages until it reaches the last page in the leaf-level 3 Returns the qualifying rows without accessing the data pages because the leaflevel is scanned Identifying... Smith … … … … Non-Leaf Level Leaf Level (Key Value) Akhtar Barr Barr Borm Buhl … … … … … Data Pages Microsoft Chai Con Con Cox Dale … … … … … Dunn Dunn Fine Fort Funk … … … … … Ganio Hall Hart Jones Jones … … … … … Jordan Kim Kim Koch Koch … … … … … Lang Martin Martin Martin Moris … Morgan … … Nash … … Nay … … Ota … … Rudd … Smith Smith Smith Smith Smith … … … … … Microsoft SQL Server goes through... Indexed Views Can Pre-Aggregate Data Microsoft Locating Data by Using Indexes That Cover a Query Example of Single Page Navigation Example of Partial Scan Navigation Example of Full Scan Navigation Microsoft Example of Single Page Navigation Index Pages Akhtar Lang … … Akhtar … Ganio … Sarah Eric … … Sarah … Jon … SELECT lastname, firstname FROM member WHERE lastname = 'Hall' Non-Leaf Level Lang... SQL Server goes through the following steps to retrieve the information: 1 Traverses the index tree 2 Starts reading leaf-level pages at the page that contains the first occurrence of the last name Funk Data in the leaf level is sorted in ascending order 3 Reads the range of leaf-level pages through to the last name of Lang At this time, the partial scan is completed 4 Returns the qualifying rows without... … … … Leaf Level (Key Value) Akhtar Barr Barr Borm Buhl Sarah … … … … Data Pages Microsoft … Ganio Jon Hall Don Hart Sherri Jones Amy Jones Beverly … Lang Martin Martin Martin Moris Eric … … … … … SQL Server goes through the following steps to retrieve the information: 1 Traverses the index tree comparing the last name Hall to the key values 2 Continues to traverse the index until it reaches the first... Used to Cover a Query Observing the Execution Plan Output Displays the phrase “Scanning a non-clustered index entirely or only a range” Comparing I/O Nonclustered index Total number of levels in the non–leaf level Total number of pages that make up the leaf level Total number of rows per leaf-level page Total number of rows per data page Microsoft Total number of pages that make up the... toward covering some of your more common queries • Are referenced frequently • Do not significantly add to the key size Minimize Index Key Size, avoid specifying key values that are too wide Maintain Row-to-Key Size Ratio If the size of the index key increases relative to the row size, query performance may be affected Microsoft Indexing Strategies Evaluating I/O for Queries That Access a Range of... the data pages, because the leaf level is scanned for last names between Funk and Lang Example of Full Scan Navigation Index Pages Akhtar … Martin USE credit SELECT lastname, firstname FROM member Non-Leaf Level Akhtar Ganio … Martin Smith … Leaf Level (Key Value) Akhtar Barr Barr Borm Buhl … … … … … Data Pages Microsoft Chai Con Con Cox Dale … … … … … Dunn Dunn Fine Fort Funk … … … … … Ganio Hall . IOSQL Server Execution Times:CPU time = 0 ms, elapsed time = 2 ms.Rows Executes StmtText StmtId -- -- - -- - -- - -- - -- - -- - -- - -- - -- - -- - -- - -- - -- - -- - -- - 47. IssuesFunctionCost-Based OptimizationHow It WorksPhasesCaching the Execution PlanSetting a Cost Limit MicrosoftMicrosoftCost-Based OptimizationLimits