IBM Books

Command Reference

db2advis - DB2 Index Advisor

Advises users on what indexes to create for one or more SQL statements. A group of related SQL statements is known as a workload. Users can rank the importance of each statement in a workload, and specify the frequency at which each statement in the workload is to be executed. The recommended indexes for each table, the statistics derived for them, as well as the DDL by which each can be created, are written to a user-created table, ADVISE_INDEX.

Authorization

Read access to the database. Read and write access to the explain tables.

Required Connection

None. This command establishes a database connection.

Command Syntax

>>-db2advis----d--database-name----+--------------------+------->
                                   +--w--workload-name--+
                                   +--s--"statement"----+
                                   '--f--filename-------'
 
>-----+---------------------------+---+-----------------+------->
      '--a--userid--+----------+--'   '--l--disk-limit--'
                    '-/passwd--'
 
>-----+----------------------+---+-----+-----------------------><
      '--t--max-advise-time--'   '--h--'
 

Command Parameters

-d database-name
Specifies the name of the database to which a connection is to be established.

-w workload-name
Specifies the name of the workload for which indexes are to be advised. This name is used in the ADVISE_WORKLOAD table.

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

-f filename
Specifies the name of an input file containing one or more SQL statements. The default is standard input. Identify comment text with two hyphens at the start of each line; that is, -- <comment>. Statements must be delimited by semicolons.

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 <x>

The frequency can be updated any number of times in the file.

-a userid/passwd
Name and password used to connect to the database. The slash (/) must be included if a password is specified.

-l disk-limit
Specifies the maximum number of megabytes available for all indexes in the existing schema. The default value is the database manager limit on maximum size of an index per partition (64 GB).

-t max-advise-time
Specifies the maximum allowable time, in minutes, to complete the operation. The default value is 10. Unlimited time is specified by a value of zero.

-h
Display help information. When this option is specified, all other options are ignored, and only the help information is displayed.

Examples

In the following example, the utility connects to the PROTOTYPE database, and recommends indexes for the ADDRESSES table without any constraints on the solution:

   db2advis -d prototype -s "select * from addresses a
      where a.zip in ('93213', '98567', '93412')
      and (company like 'IBM%' or company like '%otus')"

In the following example, the utility connects to the PROTOTYPE database, and recommends indexes that will not exceed 53MB for queries in the ADVISE_WORKLOAD table whose workload name is equal to "production". The maximum allowable time for finding a solution is 20 minutes.

   db2advis -d prototype -w production -l 53 -t 20

In the following example, the utility connects to the TEST database, and recommends at most five indexes of up to three columns each for each table referenced by any of the queries in the file myqueries.sql. No limits are placed on run time, or on the amount of disk space consumed.

   db2advis -d test -f myqueries.sql -c 3 -n 5

In the final example, an input file called db2advis.in contains SQL statements and a specification of the frequency at which each statement is to be executed: --#SET FREQUENCY 100 SELECT COUNT(*) FROM EMPLOYEE; SELECT * FROM EMPLOYEE WHERE LASTNAME='HAAS'; --#SET FREQUENCY 1 SELECT AVG(BONUS), AVG(SALARY) GROUP BY WORKDEPT ORDER BY WORKDEPT;

The utility connects to the SAMPLE database, and recommends indexes for each table referenced by the queries in the input file. The maximum allowable time for finding a solution is 5 minutes:

   db2advis -d sample -f db2advis.in -t 5

Usage Notes

For dynamic SQL statements, the frequency with which statements are executed can be obtained from the monitor as follows:

  1. Issue db2 reset monitor data. Wait for an appropriate interval of time.

  2. Issue db2 get snapshot for dynamic sql on <database-alias>.

  3. Issue db2 "insert into advise_workload (select 'myworkload', 0,stmt_text, cast(generate_unique() as char(254)), num_executions, 1, num_executions, 0, 0 from SYSFUN.SQLCACHE_SNAPSHOT)".

The default frequency for each SQL statement in a workload is 1, and the default importance is also 1. The generate_unique() function assigns a unique identifier to the statement, which can be updated by the user to be a more meaningful description of that SQL statement.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]