在本地开发机器上,我的查询在通过网站运行时无限期地超时(ASP.NET - ADO.NET) 相同的查询昨天正好运行。当我从本地机器执行它时,它运行正常。
我甚至使用了重启机器。它能是什么?
每个请求的解释:
默认30秒后查询超时(在ADO.NET中)。如果我将其设置为0(无限期) - 它会无限期地运行。在查询分析器中,它立即运行(1秒)
查询未更改。以下是查看代码:
public static List<Shipment> GetShipments(List<string> customerIds, DateTime dateFrom, DateTime dateTo)
{
try
{
var data = new List<Shipment>();
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
const string SQL = @"
SELECT TOP 1000 SH.ShipmentId, SH.TripId, CASE IsCancelled WHEN 1 THEN 'X' ELSE SH.Status END Status,
SH.FromMunicipality, SH.FromAdministrativeArea,
SH.ToMunicipality, SH.ToAdministrativeArea,
SH.PONumber, SH.ProCodeId, SH.ShipperReferenceNumber, SH.BOLNumber,
T.ScheduledPickupDate, T.ScheduledDeliveryDate,
CASE WHEN NOT TN.PDFBinary IS NULL THEN 1 ELSE 0 END HasPOD
FROM dbo.vPcyShipment SH
INNER JOIN dbo.vPcyTrip T ON SH.TripId = T.TripId
LEFT OUTER JOIN dbo.tTripNumber TN ON SH.TripId = TN.TripNumber
WHERE SH.CustomerId IN ({0})
AND T.ScheduledPickupDate BETWEEN @DateFrom AND @DateTo
ORDER BY T.ScheduledPickupDate DESC";
var customerParamNames = customerIds.Select((s, i) => "@customer" + i.ToString(CultureInfo.InvariantCulture)).ToArray();
var customerInClause = string.Join(",", customerParamNames);
using (var command = new SqlCommand(string.Format(SQL, customerInClause), connection))
{
command.Parameters.AddWithValue("@DateFrom", dateFrom);
command.Parameters.AddWithValue("@DateTo", dateTo);
for (var i = 0; i < customerParamNames.Length; i++)
{
command.Parameters.AddWithValue(customerParamNames[i], customerIds[i]);
}
using (var dataTable = new DataTable())
{
dataTable.Load(command.ExecuteReader());
var query = from row in dataTable.AsEnumerable()
select new Shipment
{
ShipmentId = row.Field<string>("ShipmentId"),
TripId = row.Field<string>("TripId"),
PoNo = row.Field<string>("PONumber"),
ProCodeId = row.Field<string>("ProCodeId"),
ShipperRef = row.Field<string>("ShipperReferenceNumber"),
BolNo = row.Field<string>("BOLNumber"),
ProphecyStatusCode = row.Field<string>("Status"),
FromCity = row.Field<string>("FromMunicipality"),
FromState = row.Field<string>"FromAdministrativeArea"),
ToCity = row.Field<string>("ToMunicipality"),
ToState = row.Field<string>("ToAdministrativeArea"),
ScheduledPickup = row.Field<DateTime>("ScheduledPickupDate"),
ScheduledDelivery = row.Field<DateTime>("ScheduledDeliveryDate"),
HasPOD = row.Field<int>("HasPOD")
};
data.AddRange(query.ToList());
}
}
}
return data;
}
catch (Exception ex)
{
Log(ex);
}
return null;
}
答案 0 :(得分:0)
没有足够的信息可以继续,但我首先要将您的所有来电更改为.AddWithValue()
,而不是使用.Add()
。当您致电.AddWithValue()
时,.Net必须猜测您的参数类型。如果它猜错了(它可以),突然你的查询可能不再与索引匹配,这说明了数据库性能的核心。
答案 1 :(得分:0)
查询分析器和ADO.NET之间的性能差异通常与数据库连接的不同配置有关(例如ANSI_NULLS)。
如果您确定使用完全相同的查询(相同的客户ID,相同的日期范围),您可以尝试使用ANSI_NULLS和查询分析器中的其他设置来尝试重现您在ADO中看到的行为.NET。