SQL Optimizer Parameterization


Add a note herePlan Recompilation

Add a note hereOnce the Query Optimizer has come up with a plan, which may have taken a considerable amount of work, SQL Server does its best to ensure that you can leverage all that costly work again. It does this by caching the plan it just created, and taking steps to ensure that the plan is reused as widely as possible. It does this by using parameterization options.

Add a note here Parameterization

Parameterization is a process whereby SQL Server will take the T-SQL you entered and look for ways to replace values that may be variables with a token, so that if a similar query is processed, SQL Server can identify it as being the same underlying query, apart from some string, or integer values, and make use of the already cached plan. For example, the following is a basic T-SQL query to return data from the Adventure Works 2008 database:
Add a note here
   select *
   from person.person
   where lastname = 'fluffy'
Add a note hereThe parameterization of this query would result in the string ‘fluffy’ being replaced with a parameter such that if another user executes the following query, the same plan would be used, saving on compilation time:
Add a note here
   select *
   from person.person
   where lastname = 'jonathan'
Add a note hereNote that this is just an example, and this particular query gets a trivial plan, so it isn't a candidate for parameterization.
Add a note hereThe SQL Server Books Online topic on "Forced Parameterization" contains a lot of very specific details about what can and cannot be converted to a parameter.
Add a note hereTo determine whether a query has been parameterized, you can search for it in the DMV sys.syscacheobjects (after first executing the query to ensure it is cached). If the SQL column of this DMV shows that the query has been parameterized, you will see that any literals from the query have been replaced by variables, and those variables are declared at the beginning of the batch.

Add a note hereParameterization is controlled by the SQL Server configuration options — simple or forced:

§  Add a note hereSimple Parameterization: The default operation of SQL Server is to use simple parameterization on all queries that are suitable candidates. Books Online provides numerous details about which queries are selected and how SQL Server performs parameterization. Using simple parameterization, SQL Server is able to parameterize only a relatively small set of the queries it receives.
§  Add a note hereForced Parameterization: For more control over database performance, you can specify that SQL Server use forced parameterization. The Forced parameterization option forces SQL Server to parameterize all literal values in any select, insert, update, or delete statement queries. There are some exceptions to this, and these are well documented in SQL Server Books Online. Forced parameterization is not appropriate in all environments and scenarios. It is recommended that you use it only for a very high volume of concurrent queries, and when you are seeing high CPU from a lot of compilation/recompilation. If you are not experiencing a lot of compilation/recompilation, then forced parameterization is probably not appropriate. If you use forced in the absence of these symptoms, you may end up with a degradation in performance and/or throughput, as SQL Server takes more time to parameterize a lot of queries that are not then reused.
Add a note hereForced parameterization can also be more finely controlled through the use of plan guides. You will learn about plan guides in more detail later in this chapter.

Comments

Popular posts from this blog

Automatic Replenishment ~ Warehouse Management (WM) in SAP

Teradata - Month End Date

Cross Domain Webix Post