0

以下 LINQ 执行需要 90 毫秒的查询:

.Where(Function(i) (i.RequestedByUserId = MySession.ApplicationUserId) 
    And (i.RequestKey1 = searchJson) 
    And (i.RequestMethod = "ProjectPlanService.GetProjectPlanMaintenanceData"))
      .Select(Function(i) i.ResultJson).FirstOrDefault

生成的 SQL 如下:

SELECT 
[Limit1].[ResultJson] AS [ResultJson]
FROM ( SELECT TOP (1) 
    [Extent1].[ResultJson] AS [ResultJson]
    FROM [dbo].[ApplicationCache] AS [Extent1]
    WHERE ([Extent1].[RequestedByUserId] = 2) AND ([Extent1].[RequestKey1] = '{"SortProperty":"","SortOrder":0,"PageNumber":1,"RecordsPerPage":15,"CriteriaCount":"1","CriteriaString":"~=~Id"}') AND ('ProjectPlanService.GetProjectPlanMaintenanceData' = [Extent1].[RequestMethod])
)  AS [Limit1]
  1. 如何优化上述 LINQ 表达式以减少执行时间?
  2. 有没有办法获得如下的单个 Select 语句:
SELECT 
[ResultJson] 
    FROM [dbo].[ApplicationCache] AS [Extent1]
    WHERE ([Extent1].[RequestedByUserId] = 2) AND ([Extent1].[RequestKey1] = '{"SortProperty":"","SortOrder":0,"PageNumber":1,"RecordsPerPage":15,"CriteriaCount":"1","CriteriaString":"~=~Id"}') AND ('ProjectPlanService.GetProjectPlanMaintenanceData' = [Extent1].[RequestMethod]) 
4

1 回答 1

0

1:不需要优化,就这样就好了——额外的“包装”不会改变任何东西。

2:它排在前 1 是因为您要求 FirstOrDefault(),如果您想要完整的结果列表,请不要这样做。

于 2013-09-19T16:20:26.147 回答