-1

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

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

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

我在这篇文章中发现 Sql Server 2005 上有缓存。

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

4

1 回答 1

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

于 2013-06-18T11:20:24.847 回答