SQL Server 视图上的聚集索引

发布于 2024-07-25 07:43:44 字数 297 浏览 12 评论 0原文

我正在尝试从相当复杂的选择查询中创建一个视图,但它不允许我在其上放置聚集索引,因为我必须使用子查询和一些聚合函数。

我必须在其上获取聚集索引,否则使用该视图的查询将永远持续下去。 显然,如果满足特定条件,SQL Server 只会存储结果集。

视图的基表是只读的,并且每天只能通过批量导入更新一次。 我不明白为什么结果不能被缓存。

有谁知道有什么方法可以让 SQL Server 缓存视图的结果,以便稍后可以查询它们吗? 我真的不想创建另一个表,因为这会像滚雪球一样导致到处都是变化。

提前致谢。

I'm trying to create a view out of quite a complex select query and it won't let me put a clustered index on it because I have to use subqueries and some aggregate functions.

I have to get a clustered index on it otherwise the queries that use the view will take forever. Apparently SQL Server will only store the result set if you meet a specific criteria.

The base tables for the view are read-only and and only get updated by a bulk import once a day. I can't see why the results can't be cached.

Does anyone know of any way to get SQL Server to cache the results of a view so they can in turn be queried later? I don't really want to create another table because that would snowball into a bunch of changes all over the place.

Thanks in advance.

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

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

发布评论

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

评论(6

此岸叶落 2024-08-01 07:43:44

我认为您正在寻找的答案是:

  • 不要使用视图来执行此操作。
  • 使用一个表,该表的字段与 SQL 查询返回的字段相对应。
  • 自动执行查询以填充此表

I think the answer you are looking for is:

  • Don't use a view to do this.
  • Use a table with the fields corresponding to the returned fields form the SQL query.
  • Automate the query to populate this table
薄荷梦 2024-08-01 07:43:44

简而言之,由于您提到的原因,无法创建聚集索引。

当您寻求一种方法来缓存复杂查询的结果时,SQL Server 提供的唯一其他对象(并将解决您的问题)是表。

如果自动化是一个问题,您应该考虑创建视图,但仅将其用作插入表的方式,以便您可以在批量插入后立即对表进行截断/插入(从视图中选择)。

如果您使用 SSIS(SQL Server 集成服务),那么添加这一点相对来说是微不足道的。

The short answer is that a clustered index cannot be created, due to the reasons you mention.

When you ask for a way to cache the results of the complicated query, the only other object that SQL Server provides (and will solve your issue) is a table.

If automation is a problem, you should consider creating the view, but only using it as a way to insert into a table, such that you can do a truncate/insert into the table (selecting from the view) immediately after the bulk insert.

If you use SSIS (SQL Server Integration Services) this is a relatively trivial thing to add.

世界如花海般美丽 2024-08-01 07:43:44

据我所知,在编译执行计划时,SQL Server 本质上是将视图的定义复制并粘贴到其编译的查询中 - 只要您能够向基础表添加索引,就应该可以获得良好的性能从查询中。

As far as I'm aware, when compiling execution plans SQL Server essentially copies and pastes the definition of the view into the query its compiling - as long as you are able to add indexes to the underlying tables it should be possible to get good performance from the query.

许一世地老天荒 2024-08-01 07:43:44

您正在构建的听起来像一个数据仓库,因此您最好的选择是在数据进入系统后对其进行操作。 您可以构建新的非规范化表(或者无论如何修改它)并对它们建立索引以允许快速查询。

如果需要,您可以在这些表之上构建视图。

What you are building sounds like a data warehouse, therefore your best option is to manipulate the data once it is in the system. You can build new tables of denormalised (or however else you are modifying it) and index them to allow quick querying.

You can then build views on top of these tables if you need to.

遗忘曾经 2024-08-01 07:43:44

在索引视图中使用聚合时,您需要使用 COUNT_BIG() 而不是 COUNT(),否则将不会创建视图

另外,如果您不是使用企业版,则需要提供 NOEXPAND 提示,否则优化器将不会使用该视图

SELECT *
FROM YourView WITH(NOEXPAND)
WHERE ....

也许您不需要视图,但您只是在表上没有正确的索引,您可以发布表的 DDL(包括索引和约束)

when using aggregates inside a indexed view you need to use COUNT_BIG() instead of COUNT() otherwise the view won't be created

Also if you are not on Enterprise Edition you need to provide the NOEXPAND hint otherwise the optimizer will not use the view

SELECT *
FROM YourView WITH(NOEXPAND)
WHERE ....

Maybe you don't need a view but you just don't have th correct indexes on the tables, can you post the DDL of the tables (including indexes and constraints)

一梦等七年七年为一梦 2024-08-01 07:43:44

我遇到了同样的问题,最终将子查询本身放入聚集索引视图中。

I had the same problem and ended up putting the sub queries in clustered index views themselves.

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