简体   繁体   English

视图上的SQL Server聚集索引

[英]sql server clustered index on a view

Im trying to create a view out of quite a complex select query and it wont 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 u meet a stupid amount of criteria. 显然,如果您满足愚蠢的条件,sql server将仅存储结果集。

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?? 有谁知道以任何方式使sql server缓存视图结果,以便以后可以查询? I dont really want to create another table cos that would snowball into a bunch of changes all over the place. 我真的不想创建另一个表cos,该表cosball会在整个地方滚雪球般变成一堆变化。

Thanks in advance. 提前致谢。

I think the answer you are looking for is: Don't use a view to do this. 我认为您正在寻找的答案是:不要使用视图来执行此操作。 Use a table with the fields corresonding to the returned fields form the sql query . 使用具有与sql查询返回的字段相对应的字段的表。 Automate the query to populate this table 自动查询以填充此表

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. 当您请求一种方法来缓存复杂查询的结果时,SQL Server提供的唯一另一个对象(将解决您的问题)是一个表。

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. 如果使用SSIS(SQL Server集成服务),这是一件相对琐碎的事情。

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

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. 然后,您可以根据需要在这些表的顶部构建视图。

when using aggregates inside a indexed view you need to use COUNT_BIG() instead of COUNT() otherwise the view won't be created 在索引视图中使用聚合时,您需要使用COUNT_BIG()而不是COUNT(),否则将不会创建该视图

Also if you are not on Enterprise Edition you need to provide the NOEXPAND hint otherwise the optimizer will not use the view 另外,如果您不在企业版上,则需要提供NOEXPAND提示,否则优化器将不会使用该视图

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) 也许您不需要视图,但是在表上没有正确的索引,是否可以发布表的DDL(包括索引和约束)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM