我将aaa,bbb,ccc
传递给@Tag
以获取存储过程SearchNewsByTag
,并且需要选择至少包含一个相似关键字的所有新闻项(比如'aaa'或' bbb'或'ccc'...)
ALTER PROCEDURE [dbo].[SearchNewsByTag]
(@PageIndex int = 0,
@PageSize int = 2147483644,
@Tag nvarchar(400) = null,
@TotalRecords int = null OUTPUT)
AS
BEGIN
DECLARE @PageLowerBound int
DECLARE @PageUpperBound int
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageLowerBound + @PageSize + 1
declare @FilteredTags as TagTable
INSERT INTO @FilteredTags (Tag)
SELECT data
FROM dbo.nop_splitstring_to_table(@Tag, ',') -- split the input Tag into separate keywords
SELECT *
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY id) AS RowNum, *
FROM News n
WHERE dbo.HasSimilarTag(@FilteredTags,n.Tags) > 0) p -- pass the table with a news tag to check
WHERE
[p].RowNum > @PageLowerBound AND [p].RowNum < @PageUpperBound
和一个标量函数,用于检查新闻是否与其中一个输入关键字(@Tag
参数)具有相似的关键字(标记)
ALTER FUNCTION [dbo].[HasSimilarTag]
(@Tags TagTable READONLY, @TagFromRecord nvarchar(10) = null)
RETURNS int
AS
BEGIN
DECLARE @name int
declare @TagsFromRecord as TagTable
INSERT INTO @TagsFromRecord (Tag)
SELECT data FROM [nop_splitstring_to_table](dbo.RemoveDauTVCLR(@TagFromRecord), ',')
declare @TagsResult as TagTable
INSERT INTO @TagsResult (Tag)
SELECT Tag FROM @Tags intersect SELECT Tag from @TagsFromRecord
select @name= COUNT(1) from @TagsResult
RETURN @name
END
一切正常但速度相当慢,通常需要5秒才能完成。新闻表只有大约110k的记录。