在 SQL Server 中,什么时候应该使用索引视图而不是真实表?

发布于 2024-09-26 14:00:33 字数 81 浏览 3 评论 0原文

我知道在 SQL Server 中,您可以在视图上创建索引,然后视图保存基础表中的数据。然后就可以查询视图了。但是,为什么我需要使用视图而不是表呢?

I know in SQL Server you can create indexes on a view, then the view saves the data from the underlying table. Then you can query the view. But, why I need to use view instead of table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡笑忘祈一世凡恋 2024-10-03 14:00:33

您可能希望使用视图来简化查询。在我们的项目中,共识是使用界面视图,尤其是“报告界面”。

想象一下,您有一张客户表,经理希望每天早上收到一份包含客户姓名及其帐户余额(或其他内容)的报告。如果您根据表格对报告进行编码,则会在报告和表格之间创建强链接,从而使以后的更改变得困难。

另一方面,如果您的报告命中了视图,您可以自由地扭曲数据库;只要视图相同,报告就可以工作,经理就会很高兴,并且您可以自由地试验数据库。您想将客户端元数据与主客户端表分开吗?继续吧,并将视图中的两个表连接起来。您想对客户的购物车信息进行非规范化吗?没问题,视图可以适应...

说实话,这是我作为程序员的观点,但数据库大师肯定会发现其他用途:)

You may want to use a view to simplify on queries. In our projects, the consensus is on using views for interfaces, and especially "report interfaces".

Imagine you've got a client table, and the manager wants a report every morning with the client's name, and their account balance (or whatever). If you code your report against the table, you're creating a strong link between your report and your table, making later changes difficult.

On the other hand if your report hits a view, you can twist the database freely; as long as the view is the same the report works, the manager is happy and you're free to experiment with the database. You want to separate client metadata from the main client table? go for it, and join the two tables in the view. You want to denormalize the cart info for the client? no problem, the view can adapt...

To be honest, it's my view as a programmer but other uses will certainly be found by db gurus :)

无风消散 2024-10-03 14:00:33

使用索引视图的优点之一是可以对 2 个或更多列的结果进行排序,其中这些列位于不同的表中。即,有一个视图,它是 table1 和 table2 按 table1.column1、table2.column2 排序的结果。然后,您可以在 column1、column2 上创建索引来优化该查询

One advantage of using an indexed view is for ordering results of 2 or more columns, where the columns are in different tables. ie, have a view which is the result of table1 and table2 sorted by table1.column1, table2.column2. You could then create an index on column1, column2 to optimise that query

若无相欠,怎会相见 2024-10-03 14:00:33

表是数据物理存储的地方。

视图是对表格进行汇总或分组以使表格组更易于使用的地方。

索引视图允许查询使用视图,而不需要从基础表获取数据,因为视图已经拥有数据,从而提高性能。

如果不对数据库进行非规范化,则仅使用表就无法获得相同的结果,从而可能会产生其他问题。

A table is where the data is physically stored.

A view is where tables are summarized or grouped to make groups of tables easier to use.

An indexed view allows a query to use a view, and not need to get data from the underlying table, as the view already has the data, thus increasing performance.

You could not achieve the same result with just tables, without denormalizing your database, and thus potentially creating other issues.

海风掠过北极光 2024-10-03 14:00:33

基本上,使用视图:

  1. 当您在许多表上多次使用相同的复杂查询时。
  2. 当新系统需要读取旧表数据,但不注意更改其感知模式时。

索引视图可以通过创建更具体的索引来提高性能,而无需增加冗余。

Basically, use a view:

  1. When you use the same complex query on many tables, multiple times.
  2. When new system need to read old table data, but doesn't watch to change their perceived schema.

Indexed Views can improve performance by creating more specific index without increasing redundancy.

我们只是彼此的过ke 2024-10-03 14:00:33

视图只是一个已指定名称并存储在数据库中的 SELECT 语句。视图的主要优点是,一旦创建,它就像您要编写的任何其他 SELECT 语句的表一样。

视图的 select 语句可以引用表、其他视图和函数。

您可以在视图(索引视图)上创建索引来提高性能。索引视图会自动更新,立即反映基础表的更改。

如果您的索引视图仅从一个表中选择列,您也可以将索引放在该表上并直接查询该表,该视图只会给您的数据库带来开销。但是,如果您的 SELECT 语句通过联接等覆盖多个表,那么您可以通过在视图上放置索引来提高性能。

A view is simply a SELECT statement that has been given a name and stored in a database. The main advantage of a view is that once it's created, it acts like a table for any other SELECT statements that you want to write.

The select statement for the view can reference tables, other views and functions.

You can create an index on the view (indexed view) to improve performance. An indexed view is self-updating, immediately reflecting changes to the underlying tables.

If your indexed view only selects columns from one table, you could just as well place the index on that table and query that table directly, the view would only cause overhead for your database. However, if your SELECT statement covers multiple tables with joins etc. than you could gain a performance boost by placing an index on the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文