是否有Microsoft SQL查询结果缓存等效?

时间:2013-11-21 15:31:26

标签: sql sql-server caching

我正在使用一个工具,它可以显示UI中表格的一些计数。 为了获得这些数字,每5秒执行一次查询。我无法在工具中进行任何结构更改,但我可以更改查询以获取计数。

问题是查询的执行最多可能需要5秒钟,用户无法执行任何操作。查询提取的数据每隔几分钟就会发生变化。

该工具可以在Oracle和MSSQL上运行。在Oracle中,我能够通过使用查询结果缓存(在查询中添加/ * + RESULT_CACHE * / hint)来大大提高查询速度。由于查询结果只是偶尔会发生变化,因此在这种特定情况下缓存它是一种可用的解决方案。执行时间大约是1ms而不是之前的5秒。

我想知道Microsoft SQL中是否存在等效内容。

1 个答案:

答案 0 :(得分:0)

SQL Server Management Studio能够显示查询执行计划,该计划显示在查询的每个部分上花费的时间百分比。从“查询”菜单中,调查“显示估计的执行计划”和“包括实际执行计划”菜单项。

还有一个SQL Server查询计划缓存:

-- First, clear the cache.
-- WARNING: Do not execute this statement anywhere near production!
DBCC FREEPROCCACHE

-- Look at what executable plans are in cache
SELECT sc.*
FROM master.dbo.syscacheobjects AS sc
WHERE sc.cacheobjtype = 'Executable Plan'

-- Execute the following statement
SELECT t.*
FROM pubs.dbo.titles AS t
WHERE t.price = 19.99

-- Look at what executable plans are in cache and you'll 
-- find that there's a plan for a NUMERIC(4,2)
SELECT sc.*
FROM master.dbo.syscacheobjects AS sc
WHERE sc.cacheobjtype = 'Executable Plan'

-- If you execute the EXACT same statement with a 4,2 
-- then you will get THAT plan. But if you execute with a 5,2
-- then you'll get a new plan. Try this:
SELECT t.*
FROM pubs.dbo.titles AS t
WHERE price = 199.99

-- Look again at the cached executable plans, and you'll see a NEW one...
SELECT sc.*
FROM master.dbo.syscacheobjects AS sc
WHERE sc.cacheobjtype = 'Executable Plan'
相关问题