Sql Server 2008 Clear Select语句缓存?

时间:2013-06-18 07:05:48

标签: sql sql-server-2008 caching

执行select语句后是否有可能清除缓存?

我试着用我的一个sql,第一次执行它,它返回3.15分钟,第二次(dint更改任何东西)它返回2.55分钟。

对我来说测试sql上的实际性能有点乏味。

我发现在this帖子上的Sql Server 2005上有缓存。

我对sql server 2008上的缓存是否正确?

1 个答案:

答案 0 :(得分:1)

这就是我为同一目的所做的事情:

DBCC FREEPROCCACHE
DBCC DROPCLEANBUFFERS
GO
DROP TABLE [WHATEVER I NEED TO DROP]
GO
TRUNCATE TABLE [WHATEVER I NEED TO EMPTY]
GO
UPDATE [WHATEVER HAS TO BE "RESET"]
GO
[DO REST OF "RESET ACTIONS" HERE]
USE [YOUR DATABASE NAME]
GO

DECLARE @start_time datetime, @end_time datetime, @miliseconds int

DECLARE @ALL_YOUR_VARS -- _YOU'LL_USE_WHILE_YOU_MEASURE /* BECAUSE YOU DON'T WANT TO MEASURE THE TIME SPENT ON TEMP VARS YOU CREATE FOR MEASURING EXEC TIME ONLY */

-- TODO: SET VARS' VALUES HERE, SAME REASON AS ABOVE

SET @start_time = GETDATE()

-- [EXECUTE ALL YOUR STATEMENTS HERE] -- [YOU WANT TO MEASURE]

SET @end_time = GETDATE() SET @miliseconds = DATEDIFF(ms, @start_time, @end_time) SELECT @start_time, @end_time, CAST(@miliseconds AS VARCHAR(max)) + ' ms' GO



编辑:差点忘了!您也可以在开始测量之前删除所有活动连接到您的数据库(不要忘记在执行下面的代码后创建一个新的连接作为实际的即时窗口,从中运行查询也会断开连接!) />
USE master
GO

SET NOCOUNT ON DECLARE @DBName varchar(50) DECLARE @spidstr varchar(8000) DECLARE @ConnKilled smallint SET @ConnKilled=0 SET @spidstr = ''

Set @DBName = 'YOURDBNAMEHERE' IF db_id(@DBName) < 4 BEGIN PRINT 'Connections to system databases cannot be killed' RETURN END SELECT @spidstr=coalesce(@spidstr,',' )+'kill '+convert(varchar, spid)+ '; ' FROM master..sysprocesses WHERE dbid=db_id(@DBName)

IF LEN(@spidstr) > 0 BEGIN EXEC(@spidstr) SELECT @ConnKilled = COUNT(1) FROM master..sysprocesses WHERE dbid=db_id(@DBName) END