我有一种情况,我有一个观点,其结构如下
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 :(得分: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)
答案 1 :(得分:0)
由于缺少有关此问题的重要信息(查询的完整源代码+估计/实际执行计划),我最好的猜测/解决方案是:我会将JOIN predicate
移到PIVOT
来源内:而不是
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 ...)