我的linq查询给了我以下sql代码:
SELECT
"GroupBy1"."A1" AS "C1"
FROM ( SELECT
COUNT(1) AS "A1"
FROM ( SELECT "Extent1"."DIRECTACQUISITIONID" AS "DIRECTACQUISITIONID"
FROM "SICAP_PROD_EX"."DIRECTACQUISITION" "Extent1"
WHERE ((1 <> "Extent1"."SYSDIRECTACQUISITIONSTATEID") AND ("Extent1"."UNIQUEIDENTIFICATIONCODE" = :p__linq__0))
FETCH FIRST 2001 ROWS ONLY
) "Limit1"
) "GroupBy1"
它在sql开发人员中以0.01秒的速度运行但在执行
时超时var count = query.Count();
我的表有大约1600万行,抛出的错误是
"ORA-01013: user requested cancel of current operation"
修改
EF代码是:
private IQueryable<DA> GetAll(CustomDbContext ctx) {
return ctx.Linguistic(true)
.DirectAcquisitions
.AsNoTracking()
.Where(a => a.SysDirectAcquisitionStateID != (int)Values.SysDirectAcquisitionState.InDefinition);
}
和
query = query.Where(x => x.UniqueIdentificationCode == filter.UniqueIdentificationCode);
我需要一个自定义的DbContext,因为在执行Take(x)oracle时使用比FETCH慢的ROWNUM所以我覆盖了IDbCommandInterceptor中的ReaderExecuting方法来执行以下操作:
if (command.CommandText.Contains($"AND (ROWNUM <= ({context.CacheCount})"))
command.CommandText = command.CommandText.Replace($"AND (ROWNUM <= ({context.CacheCount}) )", $" FETCH FIRST {context.CacheCount} ROWS ONLY");
此外,在CustomDbContext构造函数中,我执行了以下操作,因为EF生成了额外的空检查,即使在sql开发人员中,我的查询也很慢:
this.Configuration.UseDatabaseNullSemantics = true;