views:

119

answers:

2

I want to know the performance of using Views, Temp Tables and Direct Queries Usage in a Stored Procedure.

I have a table that gets created every time when a trigger gets fired. I know this trigger will be fired very rare and only once at the time of setup.

Now I have to use that created table from triggers at many places for fetching data and I confirms it that no one make any changes in that table. i.e ReadOnly Table.

I have to use this tables data along with multiple tables to join and fetch result for further queries say

select * from triggertable

By Using temp table

select ... into #tx from triggertable join t2 join t3 and so  on

select a,b, c from #tx --do something
select d,e,f from #tx ---do somethign
--and so on 
--around 6-7 queries in a row in a stored procedure.

By Using Views

create view viewname
(
select ... from triggertable join t2 join t3 and so  on
)

select a,b, c from viewname --do something
select d,e,f from viewname ---do somethign
--and so on 
--around 6-7 queries in a row in a stored procedure.

This View can be used in other places as well. So I will be creating at database rather than at sp

By Using Direct Query

select a,b, c from select ... into #tx from triggertable join t2 join t3 join ... --do something
select a,b, c from select ... into #tx from triggertable join t2 join t3 join ... --do something
.
.
--and so on 
--around 6-7 queries in a row in a stored procedure.

Now I can create a view/temporary table/ directly query usage in all upcoming queries.

What would be the best to use in this case.

+1  A: 

If triggertable is only created once at the time of setup then simply query the table directly. If you wrap your SQL queries in a Transaction that should prevent other users from updating triggertable while you are querying it.

There is no benefit of using a view in this situation.

You could copy triggertable in to a temp table but I don't see any real benefit in this situation.

Barry
A: 

Is it always data from the same source that are joined in? If that is the case an Index on the view could improve performance.

The only reason I can see for a temp table is if you have a WHERE in there that selects a small subset that all the subsequent 6-7 queries can use, but you don't state one way or the other in your question.

Both options can be used in conjunction but there are more factors that you are not mentioning such as the size of the total data etc.

Otherwise I wouldn't bother and simply query the tables directly as so select triggertable.a, t2.b, t3.c from triggertable join t2 join t3 ...

Don
@Don: Yes, Source of the triggerable table will always be same. It will always have same no of columns and with same name. Only what i need is to use subset of that table at multiple places in different different stored procedure. Even this view that I talked about might be used in multiple procedures as well.
Shantanu Gupta