SQL 与 order by 一起使用时查询花费无限时间

SQL Query is taking infinite time when using with order by

下面是我的SQL查询

select top(10) ClientCode
FROM (((Branch INNER JOIN BusinessLocation ON 
Branch.BranchCode=BusinessLocation.BranchCode) 
INNER JOIN Center ON BusinessLocation.LocationCode = Center.LocationCode) 
INNER JOIN Groups ON Center.CenterCode = Groups.CenterCode) 
INNER JOIN Client ON Groups.GroupCode = Client.GroupCode 
WHERE
((Client.CBStatus) IS NULL) AND ((Branch.PartnerName) in 
('SVCL','Edelweiss'))
order by Client.ClientCode DESC

当我 运行 它没有 order by it 运行 没问题,但是有 order by it 没有完成执行。为什么会出现这种行为?

当您 select 使用 TOP 语句时,不一定会计算每一行的计算和连接。当您尝试排序时,至少需要计算所有行的一个单元格。这是一个很长的查询,因为您的 table 很大并且行为没有错误。不要让没有顺序的快速 运行 查询误导您关于第二个查询的复杂性。

您可以在 clientcode 列上创建索引。那会加快速度。