DB2性能优化.ppt
《DB2性能优化.ppt》由会员分享,可在线阅读,更多相关《DB2性能优化.ppt(58页珍藏版)》请在三一办公上搜索。
1、1,DB2性能优化,2,The DB2 Optimizer,Determines database navigationParses SQL statements for tables and columns which must be accessedQueries statistics from DB2 Catalog(populated by RUNSTATS utility)Determines least expensive access path Since it is a Cost-Based Optimizer-it chooses the lease expensive ac
2、cess path,3,The DB2 Optimizer,SQL,Optimized Access Path,DB2 OptimizerCost-Based,Query Cost Formulas,DB2Catalog,4,Optimizer Access Path Selection,1.Gets the current statistics from DB2 catalog for the columns and tables identified in the SQL statements.These statistics are populated by the Runstats u
3、tility.2.Computes the estimated percentage of qualified rows for each predicate-which becomes the filter factor for the predicate.,5,Optimizer Access Path Selection,3.Chooses a set of reasonable access paths.4.Computes each potential access paths estimated cost based on:CPU CostI/O Cost,6,Access Pat
4、h Cost Based On:,CPU CostApplying predicates(Stage 1 or Stage 2)Traversing pages(index and tablespace)SortingI/O CostDB2 Catalog statisticsSize of the bufferpoolsCost of work files used(sorts,intermediate results,and so on),7,Will a Scan or an Index Be Used?,A tablespace Scan sequentially reads all
5、of the tablespace pages for the table being accessed.Most of the time,the fastest way to access DB2 data is with an Index.For DB2 to consider using an index-the following criteria must be met:At least one of the predicates for the SQL statement must be indexable.One of the columns(in any indexable p
6、redicate)must exist as a column in an available index.,8,Will a Scan or an Index Be Used?,An index will not be used in these circumstances:When no indexes exist for the table and columns being accessedWhen the optimizer determines that the query can be executed more efficiently without using an inde
7、x-the table has a small number of rows or using the existing indexes might require additional I/O-based on the cardinality of the index and the cluster ratio of the index.,9,Types of Indexed Access,Direct Index Lookupvalues must be provided for each column in the indexMatching Index Scan(absolute po
8、sitioning)can be used if the high order column(first column)of an index key is provided Nonmatching Index Scan(relative positioning)can be used if the first column of the index is not provided can be used for non-clustered indexescan be used to maintain data in a particular order to satisfy the ORDE
9、R BY or GROUP BYIndex Only Access can be used with if a value is supplied for all index columns-avoids reading data pages completely,10,Sequential Prefetch,A read-ahead mechanism invoked to prefill DB2s buffers so that data is already in memory before it is requested.Can be requested by DB2 under an
10、y of these circumstances:A tablespace scan of more than one pageAn index scan in which the data is clustered and DB2 determines that eight or more pages must be accessed.An index-only scan in which DB2 estimates that eight or more leaf pages must be accessed.,11,Database Services Address Space,The D
11、BAS,or Database Services Address Space,provides the facility for the manipulation of DB2 data structures.The DBAS consists of three components:Relational Data System(RDS)Set-Level Orientation Stage 2 predicatesSQL statement checkingSortingOptimizer,12,Database Services Address Space,Data Manager(DM)
12、Row-Level OrientationStage 1 predicatesIndexable predicatesLockingVarious data manipulationsBuffer Manager(BM)Physical Data AccessData movement to and from DASD,Bufferpools,13,Database Services Address Space,RelationalData Manager,DataManager,BufferManager,SQL,OptimizedSQL,Read BufferorRequest data,
13、Results,Apply stage 2predicatesand sort data,Apply stage 1predicates,Data,14,Database Services Address Space,When an SQL statement requesting a set of columns and rows is passed to the RDS,the RDS determines the best mechanism for satisfying the request.The RDS can parse an SQL statement and determi
14、ne its needs.When the RDS receives an SQL statement,it performs these steps:1.Checks authorization2.Resolves data element names into internal identifiers3.Checks the syntax of the SQL statement4.Optimizes the SQL statement and generates an access path,15,Database Services Address Space,The RDS then
15、passes the optimized SQL statement to the Data Manager for further processing.The function of the DM is to lower the level of data that is being operated on.The DM analyzes the request for table rows or index rows of data and then calls the Buffer Manager to satisfy the request.The Buffer Manager ac
16、cesses data for other DB2 components.It uses pools of memory set aside for the storage of frequently accessed data to create an efficient data access environment.,16,Database Services Address Space,The BM determines if the data is in the bufferpool already.If so-the BM accesses the data and send it
17、to the DM.If not-it calls the VSAM Media Manager to read and send back the data to the BM,so it can be sent to the DM.The DM receives the data and applies as many predicates as possible to reduce the answer set.Only Stage 1 predicates are applied in the DM.,17,Database Services Address Space,Finally
18、,the RDS receives the data from the DM.All Stage 2 predicates are applied,the necessary sorting is performed,and the results are returned to the requestor.Considering these steps,realize that Stage 1 predicates are more efficient because they are evaluated earlier in the process,by the DM instead of
19、 the RDS,and thereby reduce overhead during the processing steps.,18,SQL Coding Strategies and Guidelines,Understand Stage 1 and Stage 2 PredicatesTune the queries that are executed more frequently first!It Depends!Know Your Data!Static vs.Dynamic SQLBatch vs.Interactive(CICS vs.web),19,Unnecessary
20、SQL,Avoid unnecessary execution of SQLConsider accomplishing as much as possible with a single call,rather than multiple calls,20,Rows Returned,Minimize the number of rows searched and/or returned Code predicates to limit the result to only the rows neededAvoid generic queries that do not have a WHE
21、RE clause,21,Column Selection,Minimize the number of columns retrieved and/or updated Specify only the columns neededAvoid SELECT*Extra columns increases row size of the result set Retrieving very few columns can encourage index-only access,22,Singleton SELECT vs.Cursor,If a single row is returnedSi
22、ngleton SELECT.INTOoutperforms a Cursorerror when more than 1 row is returnedIf multiple rows are returnedCursorrequires overhead of OPEN,FETCH,and CLOSEWhat is an example of a singleton select and a select requiring a cursor?,23,Singleton SELECT vs.Cursor,For Row Update:When the selected row must b
23、e retrieved first:Use FOR UPDATE OF clause with a CURSORUsing a Singleton SELECTthe row can be updated by another program after the singleton SELECT but before the subsequent UPDATE,causing a possible data integrity issue,24,Use For Fetch Only,When a SELECT statement is used only for data retrieval-
24、use FOR FETCH ONLYFOR READ ONLY clause provides the same function-and is ODBC compliantEnables DB2 to use block fetchMonitor the performance to decide which is best for each situation,25,Avoid Sorting,DISTINCT-always results in a sortUNION-always results in a sortUNION ALL-does not sort,but retains
25、any duplicates,26,Avoid Sorting,ORDER BY may be faster if columns are indexeduse it to guarantee the sequence of the dataGROUP BYspecify only columns that need to be groupedmay be faster if the columns are indexeddo not include extra columns in SELECT list or GROUP BY because DB2 must sort the rows,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DB2 性能 优化
链接地址:https://www.31ppt.com/p-2728519.html