sp_opt_querystats

Description

Returns a performance analysis for the selected query.

Syntax

sp_opt_querystats "query_text" | help [, "diagnostic_options" | null 
	[, database_name] [, user_name]]

Parameters

"query_text"

is the text of the query you are analyzing, enclosed in quotation marks.

help

displays syntax and usage information for sp_opt_querystats.

diagnostic_options

(Optional) the diagnostic parameters based on set options. See “Usage.”

null

sp_opt_querystats requires three parameters to specify the name of a database. If you do not require diagnostic options, enter a value of null for this parameter to specify a value for the database_name parameter.

database_name

(optional) the name of the database in which the query is executed. Use this parameter if the query you are analyzing does not have fully qualified tables.

user_name

(Optional) name of the user who executes the query within the database specified by the database_name parameter. This user must already exist in the database, and the login executing sp_opt_querystats must have permission to execute the setuser command in that database.

Examples

Example 1

Analyzes a select command on the pubs2 database:

sp_opt_querystats 'select * from pubs2.dbo.authors'

Example 2

Analyzes a select command on the pubs2 database, and includes information based on enabling these set commands: set showplan, set statistics io, set option show, set statistics plancost on:

sp_opt_querystats 'select * from pubs2.dbo.authors',
   'showplan,statio,option_show, plancost'

Usage

Permissions

Any user can execute sp_opt_querystats. Permission checks do not differ based on the granular permissions settings.

Auditing

Values in event and extrainfo columns from the sysaudits table are:

Event

Audit option

Command or access audited

Information in extrainfo

38

exec_procedure

Execution of a procedure

  • Roles – Current active roles

  • Keywords or options – NULL

  • Previous value – NULL

  • Current value – NULL

  • Other information – All input parameters

  • Proxy information – Original login name, if set proxy in effect