在性能调优时,需要比较Sql 语句的性能,由于Sql Server 会缓存一些数据,所以在测试性能之前,需要清空缓存信息,常用的两个命令
CHECKPOINT DBCC DROPCLEANBUFFERS DBCC FREEPROCCACHE
Buffer Pool包含了Sql Server的所有缓存(Cache),如plan cache,data cache。
1,Removes all clean buffers from the buffer pool.
DBCC DROPCLEANBUFFERS [ WITH NO_INFOMSGS ]
Use DBCC DROPCLEANBUFFERS to test queries with a cold buffer cache without shutting down and restarting the server.
To drop clean buffers from the buffer pool, first use CHECKPOINT to produce a cold buffer cache. This forces all dirty pages for the current database to be written to disk and cleans the buffers. After you do this, you can issue DBCC DROPCLEANBUFFERS command to remove all buffers from the buffer pool.
2,Removes all elements from the plan cache, removes a specific plan from the plan cache by specifying a plan handle or SQL handle, or removes all cache entries associated with a specified resource pool.
DBCC FREEPROCCACHE [ ( { plan_handle | sql_handle | pool_name } ) ] [ WITH NO_INFOMSGS ]
3,清空指定DB的stored procedure的缓存
DBCC FLUSHPROCINDB(@DBID) [WITH NO_INFOMSGS],
其中@DBID参数表示需要清除的sp所在的数据库的ID号,可通过查询sys.databases视图或者使用DB_ID()函数来获取
不能清除某一个sp的缓存,通过该脚本,能够清除指定DB中所有sp的计划缓存。
原文:http://www.cnblogs.com/ljhdo/p/4862190.html