SQL 添加 order by 子句时服务器查询卡住

SQL Server query stuck when adding order by clause

我正在代表客户为一个非常大(且杂乱)的数据库编写一些查询。到目前为止,我一直在抓取我需要的列并确保它们包含我正在寻找的数据。在我向“order by”子句添加任何内容之前,一直没有问题。当我将 order by 子句添加到我的查询时,SQL 服务器仍然卡在“正在执行查询”。以下代码示例:

SELECT TOP 100 
   PEATR.[EffectiveDate] AS 'Effective Date', 
   CustomerRoot.[CustomerName] AS 'Name',  
   CustomerAddressRoot.[Street1] AS 'Address 1', 
   CustomerAddressRoot.[Street2] AS 'Address 2', 
   CustomerAddressRoot.[City],
   CustomerAddressRoot.[State], 
   CustomerAddressRoot.[Zip],
   CustomerAddressRoot.[Country],
   CustomerAddressRoot.[AddressDesc] AS 'Description' 
FROM PrEmployeeAccrueTierRoot AS PEATR, CustomerRoot, CustomerAddressRoot
ORDER BY CustomerRoot.[CustomerName]

我还尝试在 CustomerRoot 和 CustomerAddressRoot 上创建内部联接,因为查询返回重复数据,尤其是在“地址 1”列中。当我 运行 下面的代码时,我收到以下错误消息:

The objects "CustomerRoot" and "CustomerRoot" in the FROM clause have the same exposed names. Use correlation names to distinguish them.

代码:

SELECT TOP 100 
   PEATR.[EffectiveDate] AS 'Effective Date', 
   CustomerRoot.[CustomerName] AS 'Name',  
   CustomerAddressRoot.[Street1] AS 'Address 1', 
   CustomerAddressRoot.[Street2] AS 'Address 2', 
   CustomerAddressRoot.[City],
   CustomerAddressRoot.[State], 
   CustomerAddressRoot.[Zip],
   CustomerAddressRoot.[Country],
   CustomerAddressRoot.[AddressDesc] AS 'Description'
FROM PrEmployeeAccrueTierRoot AS PEATR, CustomerRoot, CustomerAddressRoot
INNER JOIN CustomerRoot ON CustomerAddressRoot.CustomerId=CustomerRoot.CustomerId

我之前确实为所有表分配了别名,但我仍然返回相同的错误消息。任何指导或建议将不胜感激。

您正在执行笛卡尔连接。

FROM PrEmployeeAccrueTierRoot AS PEATR, CustomerRoot, CustomerAddressRoot

...与...相同

FROM PrEmployeeAccrueTierRoot AS PEATR
  inner join CustomerRoot on 1=1
  inner join CustomerAddressRoot on 1=1

没有连接逻辑。

因此,如果 PrEmployeeAccrueTierRoot 有 1000 行,CustomerRoot 有 1000 行,CustomerAddressRoot 有 1000 行,那么您的结果将有 1,000,000,000 行。

尝试两件事:

  1. 包括连接逻辑。

您的查询应如下所示:

SELECT TOP 100 
   PEATR.[EffectiveDate] AS 'Effective Date', 
   CustomerRoot.[CustomerName] AS 'Name',  
   CustomerAddressRoot.[Street1] AS 'Address 1', 
   CustomerAddressRoot.[Street2] AS 'Address 2', 
   CustomerAddressRoot.[City],
   CustomerAddressRoot.[State], 
   CustomerAddressRoot.[Zip],
   CustomerAddressRoot.[Country],
   CustomerAddressRoot.[AddressDesc] AS 'Description' 
FROM PrEmployeeAccrueTierRoot AS PEATR
  inner join CustomerRoot on CustomerRoot.customerrootid = peatr.customerrootid
  inner join CustomerAddressRoot on CustomerAddressRoot.customerrootid = CustomerRoot.customerrootid
ORDER BY CustomerRoot.[CustomerName]

当然,我不知道您应该加入哪些专栏。了解你的数据。

那么您将剩下一个问题:如果您的查询(没有 TOP 100)将 return 数百万行,您要求数据库服务器执行所有逻辑和收集所有行,然后按 CustomerName 对它们进行排序,然后 return 前 100 行。那可能仍然很慢。你会想要...

  1. 应用过滤器。
SELECT TOP 100 
   PEATR.[EffectiveDate] AS 'Effective Date', 
   CustomerRoot.[CustomerName] AS 'Name',  
   CustomerAddressRoot.[Street1] AS 'Address 1', 
   CustomerAddressRoot.[Street2] AS 'Address 2', 
   CustomerAddressRoot.[City],
   CustomerAddressRoot.[State], 
   CustomerAddressRoot.[Zip],
   CustomerAddressRoot.[Country],
   CustomerAddressRoot.[AddressDesc] AS 'Description' 
FROM PrEmployeeAccrueTierRoot AS PEATR
  inner join CustomerRoot on CustomerRoot.customerrootid = peatr.customerrootid
  inner join CustomerAddressRoot on CustomerAddressRoot.customerrootid = CustomerRoot.customerrootid
WHERE CustomerAddressRoot.State = 'Alaska'
ORDER BY CustomerRoot.[CustomerName]

..至少在测试期间,以加快速度。