执行 LINQ 语句时,要连接的列的位置对性能有影响吗?
例如,以下哪个查询运行最快,为什么?
一种)
var query = from o in entities.orders
join i in entities.order_items
on o.OrderId equals i.OrderId
where o.AddedSalesOrder == 0
select new
{
i.ShippingFirstName,
i.ShippingLastName,
i.Sku,
i.Quantity,
i.ItemPrice,
o.TotalShippingCost,
o.OrderId,
o.OrderCreateDate
};
二)
var query = from o in entities.orders
join i in entities.order_items
on o.OrderId equals i.OrderId
where o.AddedSalesOrder == 0
select new
{
o.TotalShippingCost,
o.OrderId,
o.OrderCreateDate,
i.ShippingFirstName,
i.ShippingLastName,
i.Sku,
i.Quantity,
i.ItemPrice
};
C)
var query = from o in entities.orders
join i in entities.order_items
on o.OrderId equals i.OrderId
where o.AddedSalesOrder == 0
select new
{
o.OrderCreateDate,
i.ShippingFirstName,
i.ShippingLastName,
o.TotalShippingCost,
o.OrderId,
i.Sku,
i.Quantity,
i.ItemPrice
};
我期望查询 B 是最有效的,因为连接列的放置导致生成更清晰的 SQL 代码,但我可能错了。
如果重要,查询将在 SQL Server 2008r2 数据库上运行。
- 编辑 -
对于它的价值,我通过 C# 运行了一个快速(并且肯定是非绝对的)基准测试,以查看每个场景下的性能表现。我的发现如下:
a) 297.61 millisecond avg over 100000 iterations
b) 245.90 millisecond avg over 100000 iterations
c) 304.16 millisecond avg over 100000 iterations
我用来测试的代码如下:
var sw = new Stopwatch();
List<long> totalTime = new List<long>();
for (int u = 0; u < 100000; u++)
{
sw.Start();
var entities = new Entities();
var query = from o in entities.orders
join i in entities.order_items
on o.OrderId equals i.OrderId
where o.AddedSalesOrder == 1
select new
{
i.ShippingFirstName,
i.ShippingLastName,
i.Sku,
i.Quantity,
i.ItemPrice,
o.TotalShippingCost,
o.OrderId,
o.OrderCreateDate
};
var qc = query.Count();
sw.Stop();
totalTime.Add(sw.ElapsedMilliseconds);
sw.Reset();
}
Console.WriteLine("Average time in Milliseconds: {0}", totalTime.Average());
似乎连接列的顺序可能会影响执行速度 - 或者正如所指出的,我的数据库可能效率低下:)
无论如何,我想将发现发布给任何觉得这很有趣的人。