Ad hoc query processing

Adaptive Server performs the following steps to process ad hoc SQL statements using the statement cache:

  1. Adaptive Server parses the statement.

    If the statement should be cached (see “Caching conditions”), Adaptive Server computes a hash value from the statement. Adaptive Server uses this hash value to search for a matching statement in the statement cache (see “Statement matching criteria”).

  2. Adaptive Server caches the SQL statement text.

  3. Adaptive Server wraps the SQL statement with a lightweight stored procedure and changes any local variables into procedure parameters. The internal representation of the lightweight procedure is not yet compiled into the plan.

  4. Adaptive Server converts the SQL statement into an execute statement for the corresponding lightweight procedure.

  5. Adaptive Server then executes the procedure. Substituting the lightweight procedure increments the @@nestlevel global variable.


Statement matching criteria

Adaptive Server matches an ad hoc SQL statement to a cached statement by the SQL text and by login (particularly if both users have sa_role), user ID, database ID, and session state settings. The relevant session state consists of settings for the following set command parameters:

Settings for these parameters determine the behavior of the plan Adaptive Server produces for a cached statement. For information on the set command and its parameters, see the Adaptive Server Reference Manual.

NoteYou must configure set chained on/off in its own batch if you enable the statement cache.


Caching conditions