sql 服务器计划缓存中的即席查询和准备查询有什么区别?

What is the difference between ad hoc and prepared query in sql server plan cache?

我正在尝试了解 sql 服务器的计划缓存内容。

所以我的问题是:
1.临时计划和准备计划有什么区别?
2.What 在尝试优化 sql 服务器计划缓存时我应该知道吗?

What is the difference between ad hoc and prepared plans?

临时查询:

select * from t1

准备好的查询:
用占位符代替实际值的查询称为准备语句。

一些示例:

select * from t1 where id=@id

再举一个来自维基百科的例子:

command.CommandText = "SELECT * FROM users WHERE USERNAME = @username AND ROOM = @room";

    command.Parameters.AddWithValue("@username", username);
    command.Parameters.AddWithValue("@room", room);

What should I know about it when trying to optimize the sql server plan cache?

有关于如何优化计划的白皮书cache.so我会尽量减少..

通常在对SQL执行查询时,SQL会编译计划并将其存储在计划缓存中。此计划缓存是从缓冲池中取出的内存,不同版本对它有不同的限制how much amount of memory will be used

你知道内存是一种宝贵的资源,如果你有泄漏,再多的硬件也不够..

假设您只提交一次或两次查询,并且您倾向于像这样提交查询 lot.SQL 会将此查询的计划存储在通常会膨胀的计划缓存中 PlanCache 这很糟糕

有不同的 DMVS 可以帮助您挖掘计划缓存..

查询计划缓存中有不同类型的对象:

select 
objtype,count(*) as countt,sum(size_in_bytes)*1024.0 as memoryinkb
 from   sys.dm_exec_cached_plans a
 group by objtype

即席的、准备好的查询,这些查询正在膨胀计划缓存并且只使用一次:

select q.query_hash, 
    q.number_of_entries, 
    t.text as sample_query, 
    p.query_plan as sample_plan
from (select top 20 query_hash, 
            count(*) as number_of_entries, 
            min(sql_handle) as sample_sql_handle, 
            min(plan_handle) as sample_plan_handle
        from sys.dm_exec_query_stats
        group by query_hash
        having count(*) > 1
        order by count(*) desc) as q
    cross apply sys.dm_exec_sql_text(q.sample_sql_handle) as t
    cross apply sys.dm_exec_query_plan(q.sample_plan_handle) as p

要删除膨胀计划缓存的语句:

DECLARE @MB decimal(19,3)
        , @Count bigint
        , @StrMB nvarchar(20)


SELECT @MB = sum(cast((CASE WHEN usecounts = 1 AND objtype IN ('Adhoc', 'Prepared') THEN size_in_bytes ELSE 0 END) as decimal(12,2)))/1024/1024 
        , @Count = sum(CASE WHEN usecounts = 1 AND objtype IN ('Adhoc', 'Prepared') THEN 1 ELSE 0 END)
        , @StrMB = convert(nvarchar(20), @MB)
FROM sys.dm_exec_cached_plans


IF @MB > 10
        BEGIN
                DBCC FREESYSTEMCACHE('SQL Plans') 
                RAISERROR ('%s MB was allocated to single-use plan cache. Single-use plans have been cleared.', 10, 1, @StrMB)
        END
ELSE
        BEGIN
                RAISERROR ('Only %s MB is allocated to single-use plan cache – no need to clear cache now.', 10, 1, @StrMB)
                — Note: this is only a warning message and not an actual error.
        END
go

以上应该让您知道从哪里开始,下面是必须阅读的主题和参考资料:

1.http://www.sqlskills.com/blogs/kimberly/category/plan-cache/

2.http://sqlblog.com/blogs/kalen_delaney/archive/2007/11/04/did-you-know-sp2-does-not-limit-the-amount-of-plan-cache-you-can-have.aspx

3.https://technet.microsoft.com/en-us/library/dd672789(v=sql.100).aspx

4.Must read article By SQLCAT on issues customer faced while using Prepare Statements

在上面的参考文章中,kimberely 建议启用针对 Adhoc 工作负载的优化选项,但我建议测试它 first.here 是 DBA.SE

上的 interesting thread