Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View or Temporary Table - which to use in MS SQL Server?

I have a problem to decide whether to use a view or a temp table.

I have a stored procedure that i call from program. In that SP i store the result of a long query in a temp table, name the columns and make another queries on that table store the results in labels or a gridview or whatever and drop the Temp Table. I could also store the query-result in a view and make queries on that view. So what is better or in what case do i HAVE to use a VIEW/ Temp Table.

According to my research a view has the benefit of: Security, Simplicity and Column Name Specification. My temporary table fulfills all that too (according to my opinion).

like image 858
ruedi Avatar asked Jun 03 '13 12:06

ruedi


People also ask

Which is better view or temp table?

If you are going to use data only once during a database session, then a view will actually perform better than a temporary table because you don't need to create a structure for it.

Can we use temp tables in views in SQL Server?

As with Table Variables, Local Temporary tables are private to the process that created it. They cannot therefore be used in views and you cannot associate triggers with them.

Which is better temp table or table variable in SQL Server?

As far as performance is concerned table variables are useful with small amounts of data (like only a few rows). Otherwise a SQL Server temp table is useful when sifting through large amounts of data. So for most scripts you will most likely see the use of a SQL Server temp table as opposed to a table variable.

What is the advantage of using a temporary table instead of a table?

Temporary tables behave just like normal ones; you can sort, filter and join them as if they were permanent tables. Because SQL Server has less logging and locking overheads for temporary tables (after all, you're the only person who can see or use the temporary table you've created), they execute more quickly.


1 Answers

If the query is "long" and you are accessing the results from multiple queries, then a temporary table is the better choice.

A view, in general, is just a short-cut for a select statement. If does not imply that the results are ever run and processed. If you use a view, the results will need to be regenerated each time it is used. Although subsequent runs of the view may be more efficient (say because the pages used by the view query are in cache), a temporary table actually stores the results.

In SQL Server, you can also use table variables (declare @t table . . .).

Using a temporary table (or table variable) within a single stored procedure would seem to have few implications in terms of security, simplicity, and column names. Security would be handled by access to the stored procedure. Column names are needed for either solution. Simplicity is hard to judge without more information, but nothing sticks out as being particularly complicated.

like image 80
Gordon Linoff Avatar answered Sep 28 '22 00:09

Gordon Linoff