简体   繁体   中英

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. I have a query that has been in use for probably at least 2 years with no issue. 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). Trying to select any other individual field in the Carriers or the PriceSheets table also causes the query to take over a minute. However, I can select a single column from the shipments table such as SELECT Shipments.BOLNumber without causing the query to slow down. 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.

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? 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. 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

This is for the Select [Name] query

Shipments_FKIndex2 and Shipments_FKIndex3 are indexes on the CustomerID and CarrierID in the Shipments table respectively. Those are the primary keys of the Customer and Carrier table.

There are a few possibilities here.

The first is that the selecting the individual field is referencing two indexes, FKindex2 and FKindex3. One is probably on CarrierID and the other on 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. 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. If it is still set to the default of 5, consider raising that. I think most everyone agrees that setting of 5 is way too low for most use cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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