Saturday, 27 July 2013

to check queries that have bad performance

Procedures to do when you need to know what are the queries that have bad     
   performance
An easier way to find queries that have bad performance:
• On Db2 V8 run this query:
db2 "select TOTAL_EXEC_TIME, NUM_EXECUTIONS, substr (STMT_TEXT, 1,500) \
as statement from table(SNAPSHOT_DYN_SQL('<database>', -1)) as \
snap_dyn_sql order by 1 desc,2 desc"
• On Db2 V7 run the snapshot command and then the query:
db2 get snapshot for dynamic sql on ICMNLSDB write to file
db2 "select TOTAL_EXEC_TIME_S, NUM_EXECUTIONS, substr (STMT_TEXT, 1,500) \
as statement from table(SYSFUN.SQLCACHE_SNAPSHOT()) as snap_dyn_sql \
order by 1 desc,2 desc"
After finding out the queries that are taking a long time or the ones that run very often, you can use db2advis to check if you need new indexes using it a query each time:
db2advis -d <db_name> [{-s "sql_statement" | -i <input_file>}]
[-l <disk-limit>] [-t <max_advise_time>] [-h] [-o <output_file>]
Where:
-d <db_name> specifies the name of the database to which a connection is to be established.

-s "sql_statement" specifies the text of a single SQL statement whose indexes are to be advised. The statement must be enclosed by double quotation marks.

-i <input_file> specifies the name of an input file containing one or more SQL statements. Statements must be delimited by semicolons. Comments should have two hyphens at the start of each line. The frequency at which each statement in the workload is to be executed can by changed by inserting the following line into the input file: --#SET FREQUENCY <value>

-l <disk-limit> specifies the maximum number of megabytes available for all indexes in the existing schema. The default value is 20% of the total database size.

-t <max-advise-time> specifies the maximum allowable time, in minutes, to complete the operation. If no value is specified for this option, the operation will continue until it is completed.

-h displays help information.

-o <output_file> saves the script to create the recommended objects in output_file.

Or to investigate one or more slow queries in a production environment:

db2advis -d <db_name> -i <input_file> -o <output_file>

Monday, 27 May 2013

Inplace/Online Reorg phases

Online or inplace table reorganization allows the user to reorganize a table while permitting full access to that table. While inplace REORG provides uninterrupted user access to the data, the performance of inplace REORG is slower than classic or offline REORG.

During an inplace table reorganization, the entire table is not reorganized at once. Instead, portions of the table are reorganized incrementally. Data is not copied out to a temporary table space: rows are moved within the existing table object to re-establish clustering, reclaim free spaces, and eliminate overflow rows.

There are four primary phases for inplace table REORG:

  1. SELECT N pages
    During this phase, DB2 selects N pages, where N is the extent size with a minimum of 32 sequential pages for REORG processing.
  2. Vacate the range
    With the N pages selected, inplace table REORG moves all rows within this range to free pages within the table. Each row that is moved leaves behind an RP (REORG pointer) record that contains the RID of the row’s new location. The row is inserted to free pages in the table as a RO (REORG overflow) record that contains the data.
    Once REORG finishes moving a set of rows, it waits for all existing data accesses that are occurring in the table (for example, by currently executing applications) to complete. These existing accesses, called old scanners, use old the RIDs when accessing the table data. Any accesses that start during this waiting period, called new scanners, use the new RIDs to access the data. Once all of the old scanners have completed, REORG cleans up the moved rows, deleting the RP records and converting the RO records to normal records.
  3. Fill the range
    After all rows have been vacated, the rows are written back, in a reorganized format, sorted according to any indexes used, and obeying any PCTFREE restrictions defined. When all the pages in the range are filled, the next N sequential pages are selected in the table, and the process begins again.
  4. Truncate the table
    When all pages in the table have been reorganized, the table will be truncated to reclaim space by default. If the NOTRUNCATE option is specified, the reorganized table is not truncated.