I pass in aaa,bbb,ccc
to @Tag
for the stored procedure SearchNewsByTag
, and it needs to select all news items that have at least one similar keywords (say like 'aaa' or 'bbb' or '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
and a scalar function to check if a news has a similar keyword(tag) with one of the input keywords (@Tag
parameter)
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
Everything works correctly but it's rather slow, usually take up to 5 seconds to finish. News table has around 110k records only.