如何将CROSS APPLY重写为INNER JOIN以使视图编入索引

问题描述 投票:4回答:2

separate thread上,我得到了一个关于如何将我的存储过程转换为视图的实例,它将客户名称保存到订单映射,其中订单是以逗号分隔的订单列表,包括无订单的NULL。因此,对于下表,我需要在视图中显示以下内容:

Name     Orders
'John'   New Hat, New Book, New Phone
'Marry'  NULL

我需要索引视图,但如果视图中的SELECT查询具有APPLY和/或子查询,则无法执行此操作。是否可以将此视图转换为索引视图?

create table Customers (CustomerId int, CustomerName VARCHAR(100))
create table Orders    (CustomerId int, OrderName VARCHAR(100))

insert into Customers  (CustomerId, CustomerName) select 1, 'John' union all select 2, 'Marry'
insert into Orders     (CustomerId, OrderName)    select 1, 'New Hat' union all select 1, 'New Book' union all select 1, 'New Phone'
go

create view OrderView as 
select c.CustomerName, x.OrderNames        
from Customers c            
cross apply (select stuff((select ',' + OrderName from Orders o 
      where o.CustomerId = c.CustomerId for xml path('')),1,1,'') 
      as OrderNames) x
go
sql-server tsql sql-view cross-apply
2个回答
11
投票

您无法将此视图编入索引。

基本上,你在这里有一个聚合函数(伪装成CROSS APPLY)。

在索引视图中允许的唯一聚合函数是COUNT_BIGSUM,因为它们分布在集合加法和减法上,即SUM(a UNION ALL b) = SUM(a) + SUM(b)SUM(a EXCEPT ALL b) = SUM(a) - SUM(b)

索引可维护时需要此属性。

当从基础表插入,更新或删除新记录时,不需要重新评估整个视图:新记录的值只是从聚合值中添加或减去。

此外,COUNT_BIG也应该是视图的一部分,以跟踪记录的删除(当它变为0时,应从视图索引中删除记录)。


-1
投票

如果使用1 = 1的内连接,它将满足条件,并允许连接。

在1 = 1上选择*来自x内连接y

© www.soinside.com 2019 - 2024. All rights reserved.