SQL Server parameterized scripts and automatic parameterization (simple parameterization)

Source: Internet
Author: User
Tags server memory

If you execute an SQL statement without parameters, SQL Server internally parameterize the statement to increase the likelihood of matching it to an existing execution plan. This process, known as simple parameterization (in SQL Server 2000, called Automatic parameterization), ultimately results in the execution of plan reuse.

--Delete all cached DBCC dropcleanbuffers go--from the data buffer pool    all cached execution plans from the execution plan buffer DBCC FREEPROCCACHE        GO
--executes an SQL statement with no arguments, and SQL Server internally parameterize the statement to increase the likelihood of matching it to an existing execution plan.
sp_executesql n ' select * from WorkItem t where T.workitemid = @wid ', n ' @wid varchar ', @wid = ' b1e337b3-9b2a-4463-9692-7a 738ebba205 ' gosp_executesql n ' select * from WorkItem t where T.workitemid = @wid ', n ' @wid varchar ', @wid = ' c059be96-aea3 -42a1-8f66-b67c0dd79fa6 ' goselect    cacheobjtype, ObjType, Usecounts, refcounts, pagesused, SQL    from Sys.syscacheobjects WHERE    cacheobjtype = ' Compiled Plan ' and SQL not like '%syscacheobjects% ' and        sql like '%from WorkItem t where% '
Select    T.cacheobjtype, T.objtype, T.usecounts, T.refcounts, dc.text from    Sys.dm_exec_cached_plans t    Cross apply Sys.dm_exec_sql_text (t.plan_handle) dcwhere    t.cacheobjtype = ' Compiled plan ' and dc.text don't like '%dm_ex ec_cached_plans% ' and         dc.text like '%from workitem t where% '

Of course, as you can see from the final execution plan cache, directly executed SQL scripts will still have a corresponding record in the cache. The first SQL executed will generate two execution plans, one of which is to parameterize the obvious constants and then build your own Adhoc execution plan based on this parameterized execution plan. Subsequent SQL hits have generated an execution plan that can save a fraction of the compilation time.

As you can imagine, the execution plan for non-parameterized SQL still needs to be stored in database memory, and for the new SQL script, SQL Server also needs to follow a certain algorithm to find the corresponding parameterized execution plan, do not know how much of these need to spend resources? It is not yet clear how to view this type of information.

The following results are tracked by SQL Server Profiler:

Add:

From the Sys.dm_exec_cached_plans view, you can count the memory consumption (SQL Server memory Footprint ) of the cache execution plan.

SQL Server parameterized scripts and automatic parameterization (simple parameterization)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.