2

我试图通过在一次访问数据库中包含子属性来提高我的 Linq-to-SQL 查询的效率。我首先尝试了各种 linq 查询来实现这一点。查询变得越来越复杂,所以我尝试了 LoadWith() 选项:

我的 DAL 类的构造函数设置了 LoadWith() 设置:

public TrackerJobData()
{
    dataLoadOptions = new DataLoadOptions();

    dataLoadOptions.LoadWith<TrackerJobRecord>(x => x.SpecBarcodeRecords);
    dataLoadOptions.LoadWith<TrackerJobRecord>(x => x.TrackerJobEquipmentTriggerRecords);
    dataLoadOptions.LoadWith<TrackerJobRecord>(x => x.EtaRecord);

    this.Database.LoadOptions = dataLoadOptions;
}

这是我正在使用的查询:

public TrackerJob GetItem(int trackerJobId)
{
    TrackerJobRecord record =
        (from trackerJob in this.Database.TrackerJobRecords
         where trackerJob.TrackerJobId == trackerJobId
         select trackerJob).FirstOrDefault();

    return record.Map();
}

当我仅在 linq 查询(而不是返回)上调试和 F10时,我在 SQL Profiler 中得到以下输出:

在此处输入图像描述

请原谅我对 SQL Profiler 的无知,但突出显示的三行是否意味着从客户端(我的代码)到服务器的三个往返行程?如果是这样,为什么?SQL Server 会一次执行多个 sp_executesql 调用吗?

而且由于我认为 LoadWith() 会消除多次调用,所以我做错了什么?

编辑

以下是 SQL Profiler 中的三个语句:

exec sp_executesql N'SELECT TOP (1) [t0].[TrackerJobId], [t0].[Name], [t0].[EtaId], [t0].[SamplingProcessorTypeId], [t0].[Description], [t0].[LastModifiedUser], [t0].[LastModifiedTime], [t0].[VersionNumber], [t0].[Active], [t0].[Archived], [t1].[EtaId] AS [EtaId2], [t1].[EtaNumber], [t1].[Title], [t1].[State], [t1].[DateInitialized], [t1].[EtaOriginatorId], [t1].[Quantity], [t1].[Ehs], [t1].[Ship], [t1].[InternalUse], [t1].[DateClosed], [t1].[ExperimentId], [t1].[Disposition], [t1].[TestType], [t1].[LastModifiedUser] AS [LastModifiedUser2], [t1].[LastModifiedTime] AS [LastModifiedTime2], [t1].[VersionNumber] AS [VersionNumber2]
FROM [AutoTracker].[TrackerJob] AS [t0]
INNER JOIN [Global].[Eta] AS [t1] ON [t1].[EtaId] = [t0].[EtaId]
WHERE [t0].[TrackerJobId] = @p0',N'@p0 int',@p0=17

exec sp_executesql N'SELECT [t0].[SpecBarcodeId], [t0].[TrackerJobId], [t0].[EquipmentId], [t0].[StartTime], [t0].[EndTime], [t0].[LastModifiedUser], [t0].[LastModifiedTime], [t0].[VersionNumber]
FROM [AutoTracker].[SpecBarcode] AS [t0]
WHERE [t0].[TrackerJobId] = @x1',N'@x1 int',@x1=17

exec sp_executesql N'SELECT [t0].[TrackerJobId], [t0].[EquipmentId], [t0].[LastModifiedUser], [t0].[LastModifiedTime], [t0].[VersionNumber]
FROM [AutoTracker].[TrackerJobEquipmentTrigger] AS [t0]
WHERE [t0].[TrackerJobId] = @x1',N'@x1 int',@x1=17
4

2 回答 2

1

Each of those SQL Profiler calls represents a single roundtrip from client to DB server instance. SQL Server does support returning data sets in a single roundtrip, but I'm not sure how you would do that with LINQ to SQL.

于 2012-08-08T18:12:51.940 回答