2013-11-21 52 views
1

我正在使用一个工具来显示UI中某个表的某些计数。 要获得这些数字,每5秒执行一次单个查询。我无法对该工具进行任何结构更改,但是我可以更改查询以获取计数。有没有一个微软SQL查询结果缓存等效?

问题是查询的执行可能需要5秒钟,而用户无法做任何事情。查询提取的数据每隔几分钟就会改变一次。

该工具可以在Oracle和MSSQL上运行。在Oracle中,我通过使用查询结果缓存(在查询中添加了/ * + RESULT_CACHE * /提示),极大地提高了查询的速度。由于查询的结果只是不时变化,在这种特定情况下,缓存它是一个可用的解决方案。执行时间达到1毫秒,而不是之前的5秒。

我想知道在Microsoft SQL中是否有等价物。

回答

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' 
+1

谢谢,但我不认为这就是我一直在寻找。执行的查询将删除30个执行中的29个中的相同结果。因此,如果SQL服务器可以缓存查询结果,而不是执行计划,就像在oracle中可以缓存一样。 但是我会对任何改进感到满意,但是查询master.dbo.syscacheobjects根本不会给我任何行。 – ErikL

+0

@ErikL,请参阅此SO问题的答案:http://stackoverflow.com/questions/4095762/can-i-request-sql-server-to-cache-a-certain-result-set – RoadWarrior