2013-11-21 53 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