SQL Server - Bài 12

53 436 0
SQL Server - Bài  12

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 MicrosoftMicrosoftOutlineIntroduction to Query OptimizerObtaining Execution Plan InformationUsing an Index to Cover a QueryIndexing StrategiesOverriding the Query OptimizerDatabase Tuning Advisor MicrosoftMicrosoftQuery Optimizer IssuesFunctionCost-Based OptimizationHow It WorksPhasesCaching the Execution PlanSetting a Cost Limit MicrosoftMicrosoftCost-Based OptimizationLimits the Number of Optimization Plans to Optimize in Reasonable Amount of TimeCost is estimated in terms of I/O and CPU costDetermines Query Processing Time Use of physical operators and sequence of operationsUse 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 PhasesQuery AnalysisIdentifies the search and join criteria of the query. By limitingthe search, the query optimizer minimizes the number of rows that areprocessedIndex SelectionDetermines 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 returnedAssesses the usefulness of the index or indexes Join SelectionEvaluates which join strategy to use by considering a number of factors: selectivity, density, memory required to process the query. MicrosoftMicrosoftCaching the Execution PlanStoring a Execution Plan in Memory One copy for all serial executionsAnother copy for all parallel executionsUsing an Execution ContextAn existing execution plan is reused, if one existsA new execution plan is generated, if one does not existRecompiling Execution PlansChanges 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 LimitSpecifying 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 queriesSpecifying Connection LimitsUse the sp_configure stored procedureExecute the SET QUERY_GOVERNOR_COST_LIMIT statement Specify 0 to turn off the query governor MicrosoftMicrosoftObtaining Execution Plan InformationViewing STATISTICS Statements OutputViewing SHOWPLAN_ALL and SHOWPLAN_TEXT OutputGraphically 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. IssuesFunctionCost-Based OptimizationHow It WorksPhasesCaching the Execution PlanSetting a Cost Limit MicrosoftMicrosoftCost-Based OptimizationLimits

Ngày đăng: 15/11/2012, 10:59

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan