query是一个 IQueryable ,其中包含许多联接和条件。
var total = await query.Select(p=>p.Id).Distinct().CountAsync();
EF Core 2.1生成的sql:
SELECT COUNT(*)
FROM ( SELECT DISTINCT [Id]
FROM (many join and condition)
) as [t]
查询大约需要 8秒才能返回总数。
我手动将SQL查询更改为此:
SELECT COUNT(distinct Id)
From (many join and condition)
并且查询需要 3秒
如何重新编写我的linq查询以生成 count(唯一标识) ,而不是从( select count() ...)*