Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between select from table directly and view

What is the difference between SELECT data from table directly or from view?
And What is the best use for each one?

like image 531
ecleel Avatar asked Feb 22 '09 21:02

ecleel


People also ask

What is difference between view and SELECT in SQL?

You can select data from multiple tables, or you can select specific data based on certain criteria in views. It does not hold the actual data; it holds only the definition of the view in the data dictionary. The view is a query stored in the data dictionary, on which the user can query just like they do on tables.

What's the difference between a view and a table in SQL?

A view is a virtual table. A view consists of rows and columns just like a table. The difference between a view and a table is that views are definitions built on top of other tables (or views), and do not hold data themselves. If data is changing in the underlying table, the same change is reflected in the view.

What's the difference between a view and a table?

A table is structured with columns and rows, while a view is a virtual table extracted from a database. The table is an independent data object while views are usually depending on the table. The table is an actual or real table that exists in physical locations.

Which is faster SELECT from table or view?

there is no difference. A view is just a stored query which can be referred to in sql queries as though they are tables. Note that this does not apply to materialized views. A view is only a query stored in the data dictionary: it is not going to make your query run faster or slower.


2 Answers

According to Microsoft there is a performance benifit if you use indexed views in sql server 2000/2005/2008.

Indexed views can increase query performance in the following ways
1. Aggregations can be precomputed and stored in the index to minimize expensive computations during query execution.
2. Tables can be prejoined and the resulting data set stored.
3. Combinations of joins or aggregations can be stored

But just like indexes on tables, indexes on views experience modification overhead. So only add an index to a view if the benefit of its speed increase when running exceeds the time it takes to update the view's index.

The below links give more information on this (on when to use what).

  1. SQL Server 2000/2005 Indexed View Performance Tuning and Optimization Tips.
  2. Improving Performance with SQL Server 2000 Indexed View.
  3. See performance gains by using indexed views in SQL.
like image 181
Binoj Antony Avatar answered Sep 22 '22 18:09

Binoj Antony


In most databases, they are functionally interchangeable (disregarding materialized views, which are something entirely different anyway.) There are two common reasons for creating views. 1. An abstraction (and column-aliasing) mechanism, and 2. For permissions and access control. But as for efficiency, it's not an issue.

like image 43
dkretz Avatar answered Sep 20 '22 18:09

dkretz