我觉得我很慢,我有一个疑问:
SELECT K.RANK, physicalName, DocText, FileType
FROM Documents AS Docs
INNER JOIN CONTAINSTABLE(
Documents,DocText, 'ISABOUT (pages Weight(0.7))'
) AS K
ON Docs.DocumentID = K.[KEY]
ORDER BY K.RANK;
它适用于MSSQL,如果我这样做,转发器就会被填充:
SqlCommand objCommand = new SqlCommand("SELECT K.RANK, physicalName, DocText, FileType FROM Documents AS Docs INNER JOIN CONTAINSTABLE(Documents,DocText, 'ISABOUT ( pages Weight(0.7) )') AS K ON Docs.DocumentID = K.[KEY] ORDER BY K.RANK", objConn);
但是当我尝试用参数替换搜索文本时,它没有给我任何结果:
SqlCommand objCommand = new SqlCommand("SELECT K.RANK, physicalName, DocText, FileType FROM Documents AS Docs INNER JOIN CONTAINSTABLE(Documents,DocText, 'ISABOUT ( @SearchParams )') AS K ON Docs.DocumentID = K.[KEY] ORDER BY K.RANK", objConn);
objCommand.Parameters.AddWithValue("@SearchParams", "pages Weight(0.7)");
我做错了什么?
答案 0 :(得分:2)
搜索条件整体可以“参数化”:
SqlCommand objCommand =
new SqlCommand("SELECT K.RANK, physicalName, DocText, FileType FROM Documents AS Docs
INNER JOIN
CONTAINSTABLE(Documents,DocText, @SearchCondition) AS K
ON Docs.DocumentID = K.[KEY] ORDER BY K.RANK", objConn);
objCommand.Parameters.AddWithValue("@SearchParams", "ISABOUT (pages Weight(0.7))");
编辑#1: AddWithValue
应该避免,因为它会产生计划缓存污染。请阅读本文to better understand this aspect的SQL Server性能。相反,我会使用Add
方法(cmd.Parameters.Add
)。
+
Query performance and plan cache issues when parameter length not specified correctly