Sunday, February 19, 2012

Index and view

I have two very big tables t1 and t2, and a view:
alter view v1 as
select ... t1.c, ...
from t1 full outer join t2 on t1.a=t2.a and t1.b=t2.b
-- index exists for (a, b) and c and c is unique key of t1.
and i found it takes much longer time to execute
select ...
from v1
where c = 'xxxx'
than
select ... t1.c, ...
from t1 full outer join t2 on t1.a=t2.a and t1.b=t2.b
where c= 'xxxx'
Executive plan also shows SQL always do the joining, which takes a lot of
time, in the view first.
Anyway to force SQL Server to consider the where clause in the executive
plan of the one using the view? (I cannot write the "where c='xxxx'" into
view since it will be variable.)sorry the definition of the view is
alter view v1 as
select ... isnull(t1.c, t2.c) as c, ...
from t1 full outer join t2 on t1.a=t2.a and t1.b=t2.b
"nick" wrote:

> I have two very big tables t1 and t2, and a view:
> alter view v1 as
> select ... t1.c, ...
> from t1 full outer join t2 on t1.a=t2.a and t1.b=t2.b
> -- index exists for (a, b) and c and c is unique key of t1.
> and i found it takes much longer time to execute
> select ...
> from v1
> where c = 'xxxx'
> than
> select ... t1.c, ...
> from t1 full outer join t2 on t1.a=t2.a and t1.b=t2.b
> where c= 'xxxx'
> Executive plan also shows SQL always do the joining, which takes a lot of
> time, in the view first.
> Anyway to force SQL Server to consider the where clause in the executive
> plan of the one using the view? (I cannot write the "where c='xxxx'" into
> view since it will be variable.)|||nick
Do you have any indexes defined on the tables?
"nick" <nick@.discussions.microsoft.com> wrote in message
news:5141C943-B03F-4C95-87D3-4F278BBDF0AE@.microsoft.com...
>I have two very big tables t1 and t2, and a view:
> alter view v1 as
> select ... t1.c, ...
> from t1 full outer join t2 on t1.a=t2.a and t1.b=t2.b
> -- index exists for (a, b) and c and c is unique key of t1.
> and i found it takes much longer time to execute
> select ...
> from v1
> where c = 'xxxx'
> than
> select ... t1.c, ...
> from t1 full outer join t2 on t1.a=t2.a and t1.b=t2.b
> where c= 'xxxx'
> Executive plan also shows SQL always do the joining, which takes a lot of
> time, in the view first.
> Anyway to force SQL Server to consider the where clause in the executive
> plan of the one using the view? (I cannot write the "where c='xxxx'" into
> view since it will be variable.)

No comments:

Post a Comment