我有一个有 100 亿行的表。当我使用 where clausule 和一个值进行硬编码查询时(tabulka = tabulka.Where(x => x.Value.Contains("value")
sql 查询作为批处理发送到 sql。大约需要 5 秒。当值未硬编码(tabulka = tabulka.Where(x => x.Value.Contains(value)
)时,查询发送一个 RPC,需要 15 秒。
这是 EF 发送到 SQL 的真实示例(来自 Profiler):
exec sp_executesql N'SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[Tabulka] AS [Extent1]
WHERE ([Extent1].[Column] LIKE @p__linq__0 ESCAPE N''~'') AND ([Extent1].[Column] LIKE @p__linq__1 ESCAPE N''~'')
) AS [GroupBy1]',N'@p__linq__0 nvarchar(4000),@p__linq__1 nvarchar(4000)',@p__linq__0=N'%text1%',@p__linq__1=N'%text2%'
这需要 15 秒。
当我添加 OPTION (RECOMPILE) 需要 5 秒:
exec sp_executesql N'SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[Tabulka] AS [Extent1]
WHERE ([Extent1].[Column] LIKE @p__linq__0 ESCAPE N''~'') AND ([Extent1].[Column] LIKE @p__linq__1 ESCAPE N''~'')
) AS [GroupBy1] OPTION (RECOMPILE)',N'@p__linq__0 nvarchar(4000),@p__linq__1 nvarchar(4000)',@p__linq__0=N'%text1%',@p__linq__1=N'%text2%'
如果我将其重写为简单查询,也需要 5 秒:
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[Tabulka] AS [Extent1]
WHERE ([Extent1].[Column] LIKE '%text1%' AND [Extent1].[Column] LIKE '%text2%')
) AS [GroupBy1]
问题是如何强制 EF 将其作为 Batch 发送或执行某些操作以花费 5 秒而不是 15 秒。
这些查询仅作为演示,真正的查询可能要复杂得多,我不知道要重建它而不使用 IQueryable。
任何帮助都会得到帮助。以下是查询的执行计划