2017-07-09 34 views
0

我有一個情況,我有一個視圖,其結構是像下述如何優化視圖具有衍生tabels

select x.*,d.* 

from x 

left join (

select x, y,z,poleid from y -- and pivot is done here 

) as d 

on x.poleid=d.poleid 

where x.country =1 




    I am calling it like so, select * from view1 where country=1 
我所看到的俯視

Q1),它過濾上表X,但它需要一定的時間在左連接,因爲它獲取整個數據,然後轉動它,

如果我可以把過濾器y.country = 1在邊, 那麼它會很快。

but that can not be done as view does not take parameter. 

所以請建議有沒有什麼辦法加快視圖。

此致

+0

能否請您發佈完整的查詢源代碼和屁股將相關執行計劃視爲XML? –

+0

你可以在國家視圖上放一個索引嗎? –

回答

0

也許考慮TVF(表值函數)

CREATE Function [dbo].[MyFuncName] (@country int) 
Returns Table 
Return (
    Select x.*,d.* 
    From x 
    Left Join (select x, y,z,poleid from y) as d on x.poleid=d.poleid 
    where x.country = @country 
) 

用法

Select * From [dbo].[MyFuncName](1) 
0

由於缺失關於這個問題(查詢的全部源代碼+估計/實際執行計劃)我最好的猜測/解決方案重要信息g是:我會提出的PIVOTJOIN predicate內這樣的:不是

select x.*,d.* 

from x 

left join (

select x, y,z,poleid from y -- and pivot is done here 

) as d 

on x.poleid=d.poleid -- <-- This predicate 
... 

我會用

select x.*,d.* 

from x 

outer apply (

select x, y,z,poleid 
from (
    select .... 
    from ... 
    where x.poleid=d.poleid -- <-- This predicate 
) AS a--lias 
PIVOT (....) -- and pivot is done here 

) as d 
... 

此外,檢查是否d.poleid列有這樣一個覆蓋索引:

CREATE /*UNIQUE*/ INDEX ... 
ON d -- replace alias with full table name 
(poleid, ... other columns ? ...) 
--INCLUDE (... covering columns ...)