When to use database views and when not?

2019-01-22 12:16发布

This question is about database views, not materialized-views.

Pros:

  • Query simplification.
  • Avoid repeat the same joins on multiples queries.
  • Avoid magic numbers.

Cons:

  • Hiding real queries (may be you are repeating joins).

What else?

7条回答
在下西门庆
2楼-- · 2019-01-22 12:20

I've had to use views a few times for doing strange joins and grouping by aliases.

By strange joins, I mean selecting a list of distinct dates and then outer joining them back to the table they came from to get null entries for empty days. I couldn't figure out any other way of doing it.

As for grouping by aliases, it seemed to depend on the complexity of the formula inside the alias. If the alias didn't reference any actual columns, or columns that were already being grouped, everything was ok, but aliases on columns that weren't included in the grouping were throwing errors.

I seem to recall reading or hearing somewhere during my university days that selecting from a view was faster than selecting from a bunch of joined tables, but I don't know if that's true.

One last advantage of using a view: pivot tables in Excel. I don't think there is a way of joining tables, or at least not in the wizard interface. It might be possible to do joins in Microsoft Query, but I haven't tried yet because the thought just occurred to me now.

查看更多
太酷不给撩
3楼-- · 2019-01-22 12:23

Although views can hide complexity and multiple joins, these is complexity that would have been in the SP anyway.

If the SP could have been optimized, then the view should be optimized, which would lead to increased performance on all SPs that hit that view.

Views are incredibly powerful and useful for one reason that stands out above all the other very good reasons. They reduce code duplication. That is, in most cases, the bottom line. If a query will be used in three or more places, then a view will drastically simplify your changes if the schema or query parameters change.

I once had to edit 22 stored procedures to change some query logic. If the original architecture had utilized views, then I would have had only three changes.

查看更多
萌系小妹纸
4楼-- · 2019-01-22 12:23

Now that SQL Server has common table expressions I find myself creating fewer views. When I do create a view it's usually for a normalized hierarchy that can be used in many queries not something that replaces one query.

For example Region, Market, and City may be three normalized tables (snowflake). 90% of my queries need this data, so I'll create a view. The view never replaces a single query but makes all the other queries simple and DRY.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-22 12:25

Views are pretty awesome when you don't entirely trust the party sending queries to your database. A good example might be you would create a view on tables for a contractor so that all they can see is the rows pertaining to their project.

查看更多
神经病院院长
6楼-- · 2019-01-22 12:28

Security. Grant access on a view to the users who should be able to see the columns returned from it.

查看更多
我只想做你的唯一
7楼-- · 2019-01-22 12:30

Pros: Allows you to change the underlying data structures without affecting the queries applications are using (as long as your view can hide the data structures)

查看更多
登录 后发表回答