126

When should a View actually be used over an actual Table? What gains should I expect this to produce?

Overall, what are the advantages of using a view over a table? Shouldn't I design the table in the way the view should look like in the first place?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bevacqua
  • 47,502
  • 56
  • 171
  • 285

8 Answers8

93

Oh there are many differences you will need to consider

Views for selection:

  1. Views provide abstraction over tables. You can add/remove fields easily in a view without modifying your underlying schema
  2. Views can model complex joins easily.
  3. Views can hide database-specific stuff from you. E.g. if you need to do some checks using Oracles SYS_CONTEXT function or many other things
  4. You can easily manage your GRANTS directly on views, rather than the actual tables. It's easier to manage if you know a certain user may only access a view.
  5. Views can help you with backwards compatibility. You can change the underlying schema, but the views can hide those facts from a certain client.

Views for insertion/updates:

  1. You can handle security issues with views by using such functionality as Oracle's "WITH CHECK OPTION" clause directly in the view

Drawbacks

  1. You lose information about relations (primary keys, foreign keys)
  2. It's not obvious whether you will be able to insert/update a view, because the view hides its underlying joins from you
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • 3
    Quick question: are views "permanent", or do they only last the life of the session? Reason I ask: we have a system that sometimes goes down in the middle of a long code run. I mitigate this by biting portions of the code into intermediate tables which save intermediate results. So if the system conks before code is complete, I only have to pick up starting from the last saved temp table. I might switch to using views if they offered the same permanence. Otherwise I'll just keep doing same, and dropping temps at the end of the run. Thx! – ouonomos Aug 30 '15 at 22:52
  • 8
    @ouonomos: A normal view doesn't contain any data. It is just a stored SQL statement, i.e. a view on the underlying data. Some databases (e.g. Oracle, PostgreSQL) support materialized views, which store the "view" temporarily in another table for faster access. This is done to speed up read access when the view is complex. But that doesn't help you in your case because a materialized view is still a view, not data on its own. Your approach is probably OK. – Lukas Eder Aug 31 '15 at 09:28
48

Views can:

  • Simplify a complex table structure
  • Simplify your security model by allowing you to filter sensitive data and assign permissions in a simpler fashion
  • Allow you to change the logic and behavior without changing the output structure (the output remains the same but the underlying SELECT could change significantly)
  • Increase performance (Sql Server Indexed Views)
  • Offer specific query optimization with the view that might be difficult to glean otherwise

And you should not design tables to match views. Your base model should concern itself with efficient storage and retrieval of the data. Views are partly a tool that mitigates the complexities that arise from an efficient, normalized model by allowing you to abstract that complexity.

Also, asking "what are the advantages of using a view over a table? " is not a great comparison. You can't go without tables, but you can do without views. They each exist for a very different reason. Tables are the concrete model and Views are an abstracted, well, View.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • 3
    +1 Views are partly a tool that mitigates the complexities that arise from an efficient, normalized model by allowing you to abstract that complexity. – metdos Jan 24 '11 at 09:52
35

Views are acceptable when you need to ensure that complex logic is followed every time. For instance, we have a view that creates the raw data needed for all financial reporting. By having all reports use this view, everyone is working from the same data set, rather than one report using one set of joins and another forgetting to use one which gives different results.

Views are acceptable when you want to restrict users to a particular subset of data. For instance, if you do not delete records but only mark the current one as active and the older versions as inactive, you want a view to use to select only the active records. This prevents people from forgetting to put the where clause in the query and getting bad results.

Views can be used to ensure that users only have access to a set of records - for instance, a view of the tables for a particular client and no security rights on the tables can mean that the users for that client can only ever see the data for that client.

Views are very helpful when refactoring databases.

Views are not acceptable when you use views to call views which can result in horrible performance (at least in SQL Server). We almost lost a multimillion dollar client because someone chose to abstract the database that way and performance was horrendous and timeouts frequent. We had to pay for the fix too, not the client, as the performance issue was completely our fault. When views call views, they have to completely generate the underlying view. I have seen this where the view called a view which called a view and so many millions of records were generated in order to see the three the user ultimately needed. I remember one of these views took 8 minutes to do a simple count(*) of the records. Views calling views are an extremely poor idea.

Views are often a bad idea to use to update records as usually you can only update fields from the same table (again this is SQL Server, other databases may vary). If that's the case, it makes more sense to directly update the tables anyway so that you know which fields are available.

Pang
  • 9,564
  • 146
  • 81
  • 122
HLGEM
  • 94,695
  • 15
  • 113
  • 186
  • 2
    Did not know there was a performance issue with view calling view. That seems strange. Isn't that correctly handled by the query optimiser ? Which version of SQL Server was used in your case ? – iDevlop Dec 07 '10 at 15:40
14

According to Wikipedia,

Views can provide many advantages over tables:

  • Views can represent a subset of the data contained in a table.
  • Views can limit the degree of exposure of the underlying tables to the outer world: a given user may have permission to query the view, while denied access to the rest of the base table.

  • Views can join and simplify multiple tables into a single virtual table.

  • Views can act as aggregated tables, where the database engine aggregates data (sum, average, etc.) and presents the calculated results as part of the data.

  • Views can hide the complexity of data. For example, a view could appear as Sales2000 or Sales2001, transparently partitioning the actual underlying table.

  • Views take very little space to store; the database contains only the definition of a view, not a copy of all the data that it presents.

  • Views can provide extra security, depending on the SQL engine used.

Loukan ElKadi
  • 2,687
  • 1
  • 16
  • 20
8

A common practice is to hide joins in a view to present the user a more denormalized data model. Other uses involve security (for example by hiding certain columns and/or rows) or performance (in case of materialized views)

TToni
  • 9,145
  • 1
  • 28
  • 42
7

Views are handy when you need to select from several tables, or just to get a subset of a table.

You should design your tables in such a way that your database is well normalized (minimum duplication). This can make querying somewhat difficult.

Views are a bit of separation, allowing you to view the data in the tables differently than they are stored.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
6

You should design your table WITHOUT considering the views.
Apart from saving joins and conditions, Views do have a performance advantage: SQL Server may calculate and save its execution plan in the view, and therefore make it faster than "on the fly" SQL statements.
View may also ease your work regarding user access at field level.

Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
iDevlop
  • 24,841
  • 11
  • 90
  • 149
5

First of all as the name suggests a view is immutable. thats because a view is nothing other than a virtual table created from a stored query in the DB. Because of this you have some characteristics of views:

  • you can show only a subset of the data
  • you can join multiple tables into a single view
  • you can aggregate data in a view (select count)
  • view dont actually hold data, they dont need any tablespace since they are virtual aggregations of underlying tables

so there are a gazillion of use cases for which views are better fitted than tables, just think about only displaying active users on a website. a view would be better because you operate only on a subset of the data which actually is in your DB (active and inactive users)

check out this article

hope this helped..

fasseg
  • 17,504
  • 8
  • 62
  • 73