Finding long-running stored procedures

This section describes how to identify long-running stored procedures using sysquerymetrics to track statement-level statistics for each line and statement within a procedure, including subprocedures. This may be useful for capturing the parameters that may be driving the optimization within the procedure. This section describes how to use sysquerymetrics to identify long-running stored procedures.

This feature is new in Adaptive Server 15.0, so you cannot use the steps below to compare queries from previous releases. Often a query runs slowly because of a query plan that changed during migration.

Before you use this method, consider the following:

The steps for finding long-running queries using sysquerymetrics are:

  1. Configure statement pipe active, sql text pipe active, and statement plan text pipe for the 12.5 server.

  2. Create a temporary table in tempdb for monSysStatement, monSysSQLtext and monSysPlanText by issuing something similar to:

    select * into tempdb..monSysStatement from master..monSysStatement where 1=2
    
  3. Create a monitoring process that queries the monSysStatment, monSysSQLtext, and monSysPlanText tables once every minute and inserts the result set into the temporary tables you created in step 2. For example:

    insert into tempdb..monSysStatement (select * from master..monSysStatement)
    

    To run this once every minute, you can place the query in a loop with waitfor delay "00:01:00".

  4. On the 12.5 server, execute one module of the application to be tested.

  5. Stop the application and halt the monitoring.

  6. Bulk-copy out the monitoring table information you collected data from tempdb.

  7. Repeat steps 1 – 5 for the 15.0 server.

  8. Create a set of tables in the test database named after the monitoring tables but include version information in their names. For example: monSysStmt125, monSysStmt150, and so on.

  9. Load the tables you created in step 8 with the information you collected in step 3 (use either bcp or insert...select statements).

  10. Create an index on the monSysStThree3Ducksmt125 and monSysStmt15 tables on the SPID, KPID, DBID, ProcedureID, BatchID, ContextID, LineNumber columns.