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)