我有一个计算的查询row_Number()
。该表在与行号的分区和排序相同的列(和顺序)上有一个聚集索引。
使用合并连接(多对一)时,需要进行排序,即使聚集索引的顺序正确。删除连接也会删除排序操作。
应该支持 row_number 计算的聚集索引:
create clustered index [ClusteredIndex_e060df3fbf464a8eb9b6ea5d46a9a5f5] on [dbo].[log1]
(
[client] asc,
[orderId] asc,
[campaign] asc,
[id] asc,
[DateStamp] asc
)
create clustered index [ClusteredIndex_dd0ee53e050d436cba2cab7c678a39e5] on [dbo].[LiveReference]
(
[client] asc,
[orderId] asc,
[campaign] asc
)
查询:
with cr as
(
select distinct client, orderId,campaign
from LiveReference
)
select e.[DateStamp]
,e.[campaign]
,e.[client]
,e.[orderId]
,e.[ad]
,e.[id]
,e.[source]
,row_number() over (partition by e.[client] ,
e.[orderId] ,
e.[campaign] ,
e.[id]
order by e.[DateStamp]) as num
from [dbo].[log1] e
inner join cr on
e.client = cr.client
and e.campaign = cr.campaign
and e.orderId = cr.orderId
删除连接也会删除排序:
select e.[DateStamp]
,e.[campaign]
,e.[client]
,e.[orderId]
,e.[ad]
,e.[id]
,e.[source]
,row_number() over (partition by e.[client] ,
e.[orderId] ,
e.[campaign] ,
e.[id]
order by e.[DateStamp]) as num
from [dbo].[log1] e
(我知道这也会删除由连接执行的过滤,但这并不能解释为什么排除这些行会更改顺序)
为什么排序连接的结果会不按正确的顺序排列?
一般来说,merge join(包括merge join串联)只保留join键的排序顺序。
合并连接键是
client, campaign, orderId
. 窗口函数所需的输入排序顺序是client, orderId, campaign, id , datestamp
.因此,合并连接无法提供您的窗口函数所需的排序顺序。您可以避免使用嵌套循环连接进行排序(例如使用提示)。
我在使用 Merge Join Concatenation 避免排序中写了有关详细信息。
查询可以重写为,
所以不需要
CTE
和distinct