我需要在表格中搜索与List<string>
中包含的一组关键字匹配的行(目前有300 000行,文本长度为100到50000个字符)。关键字列表通常为2-6个单词(来自用户输入的自由文本字段)。
我现在这样做:
List<string> wordsToFind = new List<string>() { "word1", "word2" };
var matches = db.Posts.Where(x => wordsToFind.All(z => x.Text.Contains(z)));
我有两个问题。
我问的原因是,在一台拥有16 Gb ram的octacore SQL服务器上,只运行这个数据库,搜索8000条记录需要20秒才能找到两个单词。
出于好奇,我检查了这个生成的SQL,这可能和它一样有效吗?
SELECT
*
FROM [dbo].[Posts] AS [Extent1]
WHERE NOT EXISTS (SELECT
1 AS [C1]
FROM (SELECT
N'word1' AS [C1]
FROM (SELECT
1 AS X) AS [SingleRowTable1]
UNION ALL
SELECT
N'word2' AS [C1]
FROM (SELECT
1 AS X) AS [SingleRowTable2]) AS [UnionAll1]
WHERE (NOT ((CAST(CHARINDEX([UnionAll1].[C1], [Extent1].[Text]) AS int)) > 0))
OR (CASE
WHEN ((CAST(CHARINDEX([UnionAll1].[C1], [Extent1].[Text]) AS int)) > 0) THEN CAST(1 AS bit)
WHEN (NOT ((CAST(CHARINDEX([UnionAll1].[C1], [Extent1].[Text]) AS int)) > 0)) THEN CAST(0 AS bit)
END IS NULL))