linq查询程序:
var query = from c in _CategoryRespository.Table
where !c.IsDel c.CategoryToken == token
orderby c.SeqTime descending
select c;
通过sqlserver profiler看到生成的sql语句:
exec sp_executesql N'SELECT TOP (1)
[Project1].[Id] AS [Id],
····中间字段省略···
[Project1].[DelMan] AS [DelMan]
FROM ( SELECT
[Extent1].[Id] AS [Id],
····中间字段省略···
[Extent1].[DelMan] AS [DelMan]
FROM [dbo].[Category] AS [Extent1]
WHERE ([Extent1].[IsDel] <> 1) AND (@p__linq__0 = 1 OR [Extent1].[Enabled] = 1) AND (([Extent1].[CategoryToken] = @p__linq__1) OR (([Extent1].[CategoryToken] IS NULL) AND (@p__linq__1 IS NULL)))
) AS [Project1]
ORDER BY [Project1].[SeqTime] DESC',N'@p__linq__0 bit,@p__linq__1 nvarchar(4000)',@p__linq__0=0,@p__linq__1=N'luyouqi'
总体来说就是生成了嵌套。我猜entity这么生成的意义是对于大表,先筛选,然后再排序来提高速度。可是我在本机测试用不嵌套的直接查询的语句,速度更快啊。而且我认为sqlserver等DB对于这样的筛选、排序的语句,肯定也会先(筛选,然后排序)的吧?Entity这么做是不是多余了?
速度比较的截图:
问题出在排序上吧.你那拿来对比的sql有排序吗.
是的,有排序的。这个没有忽略
@qust_青岛: 那发对比的sql和执行计划看看吧.ef生成的这个sql是没问题的.我正常写也得这样写
@吴瑞祥: OK,谢谢回答。现在手头有别的工作,下午再重现。