EF 6 查找生成派生自语句
EF 6 Find Generates Derived From Statement
我对 EF 6 的 Find 方法生成派生查询的原因感到困惑。我在LinqPad中运行下面两条语句:
using (var ctx = new ApplicationDbContext())
{
var z = ctx.TicketTracker.Find(11277);
var x = ctx.TicketTracker.Where(w => w.TicketTrackerId == 11277).FirstOrDefault();
}
这是输出:
SELECT
[Limit1].[TicketTrackerId] AS [TicketTrackerId],
[Limit1].[BusinessId] AS [BusinessId],
[Limit1].[DeviceId] AS [DeviceId],
[Limit1].[ETicketStorageId] AS [ETicketStorageId],
[Limit1].[TicketJSON] AS [TicketJSON],
[Limit1].[DriverUpdateId] AS [DriverUpdateId],
[Limit1].[OrderNumber] AS [OrderNumber],
[Limit1].[OrderKey] AS [OrderKey],
[Limit1].[TicketNumber] AS [TicketNumber],
[Limit1].[CustomerId] AS [CustomerId],
[Limit1].[VehicleId] AS [VehicleId],
[Limit1].[Plant] AS [Plant],
[Limit1].[Notes] AS [Notes],
[Limit1].[PostedToKeystone] AS [PostedToKeystone],
[Limit1].[EmailSuccess] AS [EmailSuccess],
[Limit1].[EmailRecipients] AS [EmailRecipients],
[Limit1].[FormType] AS [FormType],
[Limit1].[Usage] AS [Usage],
[Limit1].[JobId] AS [JobId],
[Limit1].[JobLot] AS [JobLot],
[Limit1].[CustomerPo] AS [CustomerPo],
[Limit1].[CreateDate] AS [CreateDate],
[Limit1].[PostedToKeystoneDate] AS [PostedToKeystoneDate],
[Limit1].[Devices_DeviceId] AS [Devices_DeviceId]
FROM ( SELECT TOP (2)
[Extent1].[TicketTrackerId] AS [TicketTrackerId],
[Extent1].[BusinessId] AS [BusinessId],
[Extent1].[DeviceId] AS [DeviceId],
[Extent1].[ETicketStorageId] AS [ETicketStorageId],
[Extent1].[TicketJSON] AS [TicketJSON],
[Extent1].[DriverUpdateId] AS [DriverUpdateId],
[Extent1].[OrderNumber] AS [OrderNumber],
[Extent1].[OrderKey] AS [OrderKey],
[Extent1].[TicketNumber] AS [TicketNumber],
[Extent1].[CustomerId] AS [CustomerId],
[Extent1].[VehicleId] AS [VehicleId],
[Extent1].[Plant] AS [Plant],
[Extent1].[Notes] AS [Notes],
[Extent1].[PostedToKeystone] AS [PostedToKeystone],
[Extent1].[EmailSuccess] AS [EmailSuccess],
[Extent1].[EmailRecipients] AS [EmailRecipients],
[Extent1].[FormType] AS [FormType],
[Extent1].[Usage] AS [Usage],
[Extent1].[JobId] AS [JobId],
[Extent1].[JobLot] AS [JobLot],
[Extent1].[CustomerPo] AS [CustomerPo],
[Extent1].[CreateDate] AS [CreateDate],
[Extent1].[PostedToKeystoneDate] AS [PostedToKeystoneDate],
[Extent1].[Devices_DeviceId] AS [Devices_DeviceId]
FROM [ETicket].[TicketTracker] AS [Extent1]
WHERE [Extent1].[TicketTrackerId] = @p0
) AS [Limit1]
GO
SELECT TOP (1)
[Extent1].[TicketTrackerId] AS [TicketTrackerId],
[Extent1].[BusinessId] AS [BusinessId],
[Extent1].[DeviceId] AS [DeviceId],
[Extent1].[ETicketStorageId] AS [ETicketStorageId],
[Extent1].[TicketJSON] AS [TicketJSON],
[Extent1].[DriverUpdateId] AS [DriverUpdateId],
[Extent1].[OrderNumber] AS [OrderNumber],
[Extent1].[OrderKey] AS [OrderKey],
[Extent1].[TicketNumber] AS [TicketNumber],
[Extent1].[CustomerId] AS [CustomerId],
[Extent1].[VehicleId] AS [VehicleId],
[Extent1].[Plant] AS [Plant],
[Extent1].[Notes] AS [Notes],
[Extent1].[PostedToKeystone] AS [PostedToKeystone],
[Extent1].[EmailSuccess] AS [EmailSuccess],
[Extent1].[EmailRecipients] AS [EmailRecipients],
[Extent1].[FormType] AS [FormType],
[Extent1].[Usage] AS [Usage],
[Extent1].[JobId] AS [JobId],
[Extent1].[JobLot] AS [JobLot],
[Extent1].[CustomerPo] AS [CustomerPo],
[Extent1].[CreateDate] AS [CreateDate],
[Extent1].[PostedToKeystoneDate] AS [PostedToKeystoneDate],
[Extent1].[Devices_DeviceId] AS [Devices_DeviceId]
FROM [ETicket].[TicketTracker] AS [Extent1]
WHERE 11277 = [Extent1].[TicketTrackerId]
我希望 Find 能够简单地生成一个 select 语句,但在我开始切换代码之前,EF 使用简单方法添加这个复杂查询是否有原因?我读过 Find 使用内部缓存而 Where 没有,但除此之外我还缺少什么?
这些查询是正常的,不会导致任何性能问题。对于一些更复杂的查询,您可能会发现编写自定义的 Raw SQL Query 是值得的,但在本例中不是。
this query is always either taking up too many resources or is the slowest.
打开 Query Store 以了解它正在占用哪些资源以及速度缓慢的原因。
当您看到随机查询 运行 似乎无缘无故地变慢,或者资源消耗非常高时,这通常是索引问题的迹象。
-- 将此答案放在这里,因为这实际上是 OP 查询的问题。
我对 EF 6 的 Find 方法生成派生查询的原因感到困惑。我在LinqPad中运行下面两条语句:
using (var ctx = new ApplicationDbContext())
{
var z = ctx.TicketTracker.Find(11277);
var x = ctx.TicketTracker.Where(w => w.TicketTrackerId == 11277).FirstOrDefault();
}
这是输出:
SELECT
[Limit1].[TicketTrackerId] AS [TicketTrackerId],
[Limit1].[BusinessId] AS [BusinessId],
[Limit1].[DeviceId] AS [DeviceId],
[Limit1].[ETicketStorageId] AS [ETicketStorageId],
[Limit1].[TicketJSON] AS [TicketJSON],
[Limit1].[DriverUpdateId] AS [DriverUpdateId],
[Limit1].[OrderNumber] AS [OrderNumber],
[Limit1].[OrderKey] AS [OrderKey],
[Limit1].[TicketNumber] AS [TicketNumber],
[Limit1].[CustomerId] AS [CustomerId],
[Limit1].[VehicleId] AS [VehicleId],
[Limit1].[Plant] AS [Plant],
[Limit1].[Notes] AS [Notes],
[Limit1].[PostedToKeystone] AS [PostedToKeystone],
[Limit1].[EmailSuccess] AS [EmailSuccess],
[Limit1].[EmailRecipients] AS [EmailRecipients],
[Limit1].[FormType] AS [FormType],
[Limit1].[Usage] AS [Usage],
[Limit1].[JobId] AS [JobId],
[Limit1].[JobLot] AS [JobLot],
[Limit1].[CustomerPo] AS [CustomerPo],
[Limit1].[CreateDate] AS [CreateDate],
[Limit1].[PostedToKeystoneDate] AS [PostedToKeystoneDate],
[Limit1].[Devices_DeviceId] AS [Devices_DeviceId]
FROM ( SELECT TOP (2)
[Extent1].[TicketTrackerId] AS [TicketTrackerId],
[Extent1].[BusinessId] AS [BusinessId],
[Extent1].[DeviceId] AS [DeviceId],
[Extent1].[ETicketStorageId] AS [ETicketStorageId],
[Extent1].[TicketJSON] AS [TicketJSON],
[Extent1].[DriverUpdateId] AS [DriverUpdateId],
[Extent1].[OrderNumber] AS [OrderNumber],
[Extent1].[OrderKey] AS [OrderKey],
[Extent1].[TicketNumber] AS [TicketNumber],
[Extent1].[CustomerId] AS [CustomerId],
[Extent1].[VehicleId] AS [VehicleId],
[Extent1].[Plant] AS [Plant],
[Extent1].[Notes] AS [Notes],
[Extent1].[PostedToKeystone] AS [PostedToKeystone],
[Extent1].[EmailSuccess] AS [EmailSuccess],
[Extent1].[EmailRecipients] AS [EmailRecipients],
[Extent1].[FormType] AS [FormType],
[Extent1].[Usage] AS [Usage],
[Extent1].[JobId] AS [JobId],
[Extent1].[JobLot] AS [JobLot],
[Extent1].[CustomerPo] AS [CustomerPo],
[Extent1].[CreateDate] AS [CreateDate],
[Extent1].[PostedToKeystoneDate] AS [PostedToKeystoneDate],
[Extent1].[Devices_DeviceId] AS [Devices_DeviceId]
FROM [ETicket].[TicketTracker] AS [Extent1]
WHERE [Extent1].[TicketTrackerId] = @p0
) AS [Limit1]
GO
SELECT TOP (1)
[Extent1].[TicketTrackerId] AS [TicketTrackerId],
[Extent1].[BusinessId] AS [BusinessId],
[Extent1].[DeviceId] AS [DeviceId],
[Extent1].[ETicketStorageId] AS [ETicketStorageId],
[Extent1].[TicketJSON] AS [TicketJSON],
[Extent1].[DriverUpdateId] AS [DriverUpdateId],
[Extent1].[OrderNumber] AS [OrderNumber],
[Extent1].[OrderKey] AS [OrderKey],
[Extent1].[TicketNumber] AS [TicketNumber],
[Extent1].[CustomerId] AS [CustomerId],
[Extent1].[VehicleId] AS [VehicleId],
[Extent1].[Plant] AS [Plant],
[Extent1].[Notes] AS [Notes],
[Extent1].[PostedToKeystone] AS [PostedToKeystone],
[Extent1].[EmailSuccess] AS [EmailSuccess],
[Extent1].[EmailRecipients] AS [EmailRecipients],
[Extent1].[FormType] AS [FormType],
[Extent1].[Usage] AS [Usage],
[Extent1].[JobId] AS [JobId],
[Extent1].[JobLot] AS [JobLot],
[Extent1].[CustomerPo] AS [CustomerPo],
[Extent1].[CreateDate] AS [CreateDate],
[Extent1].[PostedToKeystoneDate] AS [PostedToKeystoneDate],
[Extent1].[Devices_DeviceId] AS [Devices_DeviceId]
FROM [ETicket].[TicketTracker] AS [Extent1]
WHERE 11277 = [Extent1].[TicketTrackerId]
我希望 Find 能够简单地生成一个 select 语句,但在我开始切换代码之前,EF 使用简单方法添加这个复杂查询是否有原因?我读过 Find 使用内部缓存而 Where 没有,但除此之外我还缺少什么?
这些查询是正常的,不会导致任何性能问题。对于一些更复杂的查询,您可能会发现编写自定义的 Raw SQL Query 是值得的,但在本例中不是。
this query is always either taking up too many resources or is the slowest.
打开 Query Store 以了解它正在占用哪些资源以及速度缓慢的原因。
当您看到随机查询 运行 似乎无缘无故地变慢,或者资源消耗非常高时,这通常是索引问题的迹象。
-- 将此答案放在这里,因为这实际上是 OP 查询的问题。