Subject Re: Strange performance on a VIEW
Author Adam
> But if I don't use the view, and just take the SQL that makes up
> V_VIEW and put the "where ID = X" into that SQL, it is FAST
>
> any idea why that would happen?
>
> V_VIEW looks like sort of like this:
>
> select ID, A, B, t2.C, sum(t2.D)
> from table1 t1
> left join table2 t2 on t1.ID = t2.ID
> group by ID, A, B, C

Which ID field do you think it is selecting; T1.ID or T2.ID? Now
which one is it actually selecting?

They may equal the same value, but if it is picking T2.ID then the
optimiser must do a full table scan on T1 which would explain the
slowness

Also because it is obviously a join across PKs, left join and join
will be identical, so using a normal inner join does not tie the
optimisers hands to choosing a bad plan.

Clearly because it runs faster outside the view a better plan does
exist for this query.

Try this:

Replace the ? with the applicable table number

select T1.ID, T?.A, T?.B, t2.C, sum(t2.D)
from table1 t1
join table2 t2 on t1.ID = t2.ID
group by T1.ID, T?.A, T?.B, T2.C

And see if it helps. Otherwise, post the entire Create View statement
so we can know what field matches what.

I also recommend you use a tool that can analyse a query plan and
show you which table records are hit. IBPlanalyzer is a nice
lightweight one, IBAdmin has one too.

Hope that helps
Adam