0

我正在使用 Linq to SQl 来查询数据。当我在 SQL Server Profiler 中使用 order by 和监视编写选择查询时,它会运行选择命令 2 次

  1. 从表中选择字段
  2. 订购数据。

数据量大需要时间。有什么解决方案。

更新:

from dc in dataContext.UserTable join 
m in dataContext.MonthLookups on dc.Month equals m.Month into sr 
from x in sr.DefaultIfEmpty() 
order by dc.UserName, dc.FirstName 
select new {dc, sr.MothName};

更新:

SELECT 
 [Project1].[ UserName] AS [UserName], 
[Project1].[ FirstName] AS [FirstName], 
[Project1].[ MonthName] AS [MonthName], 
[Project1].[year] AS [year]
FROM ( SELECT 
     [Extent1].[UserName] AS [UserName], 
    [Extent1].[FirstName] AS [FirstName], 
    [Extent1].[year] AS [year], 
     [Extent2].[MonthName] AS [MonthName]
    FROM  [dbo].[DutySavingFin] AS [Extent1]
    LEFT OUTER JOIN [dbo].[MonthLookup] AS [Extent2] ON [Extent1].[Month] = [Extent2].[Month]
)  AS [Project1]
ORDER BY [Project1].[UserName] ASC, [Project1].[FirstName] ASC

更新 :

首选方式:

SELECT 
     [Extent1].[UserName] AS [UserName], 
    [Extent1].[FirstName] AS [FirstName], 
    [Extent1].[year] AS [year], 
     [Extent2].[MonthName] AS [MonthName]
    FROM  [dbo].[DutySavingFin] AS [Extent1]
    LEFT OUTER JOIN [dbo].[MonthLookup] AS [Extent2] ON [Extent1].[Month] = [Extent2].[Month]
ORDER BY [Extent1].[UserName] ASC, [Extent1].[FirstName] ASC
4

1 回答 1

1

您上面的示例显示了一个查询。如果这就是您的意思,嵌套查询不会影响性能......

如果您正在处理大量数据:
1) 确保您在 DutySavingFin.Month、MonthLookup.Month 和 DutySavingFin.UserName+DutySavingFin.FirstName 上有索引。
2)添加where子句以过滤到您需要的记录,除非您确实需要表中的所有数据

于 2012-05-08T10:04:36.773 回答