1

我想要一个可以传递文本字符串的 SQL 过程,它将从关键字表中识别文本中的特定关键字(标签)。

到目前为止,我有以下内容非常适合单个单词;

INSERT INTO #tags SELECT Word 
FROM dbo.SplitWords('some colours are blue, green, red and light blue')

SELECT Word
FROM    #tags
INTERSECT
SELECT  Tag
FROM    dbo.Tags

DROP TABLE #tags

如果我的标签表有 'green'、'red' 和 'blue' 的条目,它们会按照您的预期返回。

我想知道的是我如何才能最好地获得类似的结果,但对于包含多个单词的标签......例如,“浅蓝色”

我意识到上面代码的问题是我将源文本拆分为单个单词,因此“浅蓝色”永远不会匹配,但是我可以采取不同的路线而不涉及光标等吗?

谢谢你的帮助


刚刚意识到以下将实现我所需要的

DECLARE @Text as nvarchar(max)

SELECT @Text = 'some colours are blue, green, red and light blue'

SELECT  TagID, 
    Tag 
FROM    Tags 
WHERE   @Text LIKE '% ' + Tag + ' %' 
ORDER BY Tag

但是我担心如果我的表有几千行并且我正在搜索的文本字符串很长,它可能会效率低下。

有人对我如何更有效地执行相同的过程有任何想法吗?

4

2 回答 2

0

查看我在TSQL 上的回答 - 使用全文 CONTAINS 的连接

它利用了@Conrad 的想法,但让你超越了 CONTAINS 的可变限制

于 2012-05-09T18:10:53.013 回答
0

OK, I settled for my previous solution until I had time to have a re-think / wait for it to become a problem. The old method is now taking too long to execute forcing me to find an alternative solution.

After some playing the best solution I have found (in terms of length of execution time) is to cursor it....

I therefore set up a cursor for the phrase I want to search the text for and blitz through it populating a temp table with the phrase IDs which match as I go. Key thing to use FAST_FORWARD and FORWARD_ONLY settings to maximize performance.

Once done, I simply join my temp table back to my db tables to return whatever details of the phrases I need.

Example code below:

DECLARE @PageText nvarchar(max) -- parameter of page text

CREATE TABLE #Matches (PhraseID int)

DECLARE @PhraseID int DECLARE @PhraseText nvarchar(100)

DECLARE curMatchingPhrases CURSOR FAST_FORWARD FORWARD_ONLY FOR SELECT p.PhraseID, p.PhraseText FROM Phrases p

OPEN curMatchingPhrases

FETCH NEXT FROM curMatchingPhrases INTO @PhraseID, @PhraseText

WHILE @@FETCH_STATUS = 0 BEGIN

IF EXISTS (SELECT 'match' WHERE @PageText LIKE '% ' + @PhraseText + ' %')
BEGIN
    INSERT #Matches SELECT  @PhraseID
    WHERE   @PhraseID NOT IN (SELECT PhraseID FROM #Matches)
END

FETCH NEXT FROM curMatchingPhrases INTO @PhraseID, @PhraseText 

END

CLOSE curMatchingPhrases DEALLOCATE curMatchingPhrases

SELECT * FROM #Matches

DROP TABLE #Matches

I'm sure others on here will be able to find more elegant solutions, but the cursor has reduced a 6+sec SP down to 0 - 1sec for me, so I'm happy for now.

Mojo

于 2012-11-26T23:37:57.577 回答