简体   繁体   English

仅选择一列时,查询运行时间更长

[英]Query takes way longer to run when selecting only one column

this is my first post here.这是我在此的头一篇博文。 I'm fairly familiar with SQL queries (though it's not my main focus) but I'm stumped on a sudden issue.我对 SQL 查询相当熟悉(尽管这不是我的主要关注点),但我被一个突然的问题难住了。 I have a query that has been in use for probably at least 2 years with no issue.我有一个查询可能已经使用了至少 2 年,没有任何问题。 Today, it is suddenly taking way longer to run.今天,它突然需要更长的时间来运行。 It used to be nearly instant, now it takes about a minute to complete.它过去几乎是即时的,现在大约需要一分钟才能完成。

SELECT Carriers.[Name]
From Shipments 
INNER JOIN Carriers ON Shipments.CarrierID = Carriers.CarrierID 
INNER JOIN PriceSheets ON PriceSheets.ShipmentID = Shipments.ShipmentID 
Where PriceSheets.SettlementQueue IN ('CHECK BOL', 'CHECK ORG INV', 'CHECK BAL DUE', 'AUDIT NEEDED', 'HOLD') 
AND Shipments.CustomerID <> 10055 

Here's what is strange.这就是奇怪的地方。 If I change the SELECT Carriers.[Name] to SELECT * the query completes nearly instantly (returning about 181 rows).如果我将SELECT Carriers.[Name]更改为SELECT *查询几乎立即完成(返回大约 181 行)。 Trying to select any other individual field in the Carriers or the PriceSheets table also causes the query to take over a minute.尝试 select Carriers 或 PriceSheets 表中的任何其他单个字段也会导致查询花费一分钟以上。 However, I can select a single column from the shipments table such as SELECT Shipments.BOLNumber without causing the query to slow down.但是,我可以 select 诸如SELECT Shipments.BOLNumber等发货表中的单个列,而不会导致查询速度变慢。 But I have also noticed if I try SELECT Shipments.CustomerID in this query, that also causes it to slow down while SELECT Shipments.ProNumber for example is near instant.但我也注意到,如果我在此查询中尝试SELECT Shipments.CustomerID ,这也会导致它变慢,而SELECT Shipments.ProNumber例如几乎是即时的。

But all of those individual columns are displayed just fine if I simply do SELECT * and it happens instantly... so why is it when I specify a specific columns it's taking so much longer?但是,如果我只是简单地执行SELECT *并且它会立即发生,那么所有这些单独的列都会显示得很好......那么为什么当我指定一个特定的列时它需要更长的时间? I've yet to encounter this, and it feels like a long shot to ask since this is such a specific situation but I'm wondering if anybody else has an idea?我还没有遇到过这个问题,因为这是一个非常具体的情况,所以感觉很长一段时间,但我想知道其他人是否有想法? In case it is relevant, this is an Azure SQL Server database and I'm testing the query in SSMS.如果相关,这是一个 Azure SQL 服务器数据库,我正在 SSMS 中测试查询。 Thanks.谢谢。

Edit: Here's the execution plans.编辑:这是执行计划。 Hopefully I did this right.希望我做对了。 Admittedly, analyzing these is outside of what I have experience with.诚然,分析这些超出了我的经验。

This is for the Select * query这是针对 Select * 查询

This is for the Select [Name] query这是针对 Select [名称] 查询

Shipments_FKIndex2 and Shipments_FKIndex3 are indexes on the CustomerID and CarrierID in the Shipments table respectively. Shipments_FKIndex2 和 Shipments_FKIndex3 分别是 Shipments 表中 CustomerID 和 CarrierID 的索引。 Those are the primary keys of the Customer and Carrier table.这些是 Customer 和 Carrier 表的主键。

There are a few possibilities here.这里有几种可能性。

The first is that the selecting the individual field is referencing two indexes, FKindex2 and FKindex3.首先是选择单个字段引用了两个索引,FKindex2 和 FKindex3。 One is probably on CarrierID and the other on Name.一个可能在 CarrierID 上,另一个在 Name 上。 If each index is on one column only, consider whether these indexes can be combined.如果每个索引仅在一个列上,请考虑是否可以组合这些索引。 Create a multi-column index to be one column that is searched, such as carrierID, and then make 'name' an included column.创建一个多列索引作为搜索的一列,例如carrierID,然后将'name'设为包含列。 Of course you will have to make sure that such a change would not impact other queries.当然,您必须确保此类更改不会影响其他查询。

The second issue is that the second query rolled over into a parallel plan.第二个问题是第二个查询转入并行计划。 Check the server settings for what your parallel setting is.检查服务器设置以了解您的并行设置。 Right click the server name in SSMS -> Properties -> Advanced -> Cost Threshold for parallelism.右键单击 SSMS 中的服务器名称 -> 属性 -> 高级 -> 并行成本阈值。 If it is still set to the default of 5, consider raising that.如果它仍然设置为默认值 5,请考虑提高它。 I think most everyone agrees that setting of 5 is way too low for most use cases.我想大多数人都同意 5 的设置对于大多数用例来说太低了。

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

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