Contains performs MUCH slower with variable vs constant string SQL Server

Posted by Greg R on Stack Overflow See other posts from Stack Overflow or by Greg R
Published on 2010-03-16T18:14:59Z Indexed on 2010/03/16 18:31 UTC
Read the original article Hit count: 351

For some unknown reason I'm running into a problem when passing a variable to a full text search stored procedure performs many times slower than executing the same statement with a constant value. Any idea why and how can that be avoided?

This executes very fast:

SELECT * FROM table
WHERE CONTAINS (comments, '123')

This executes very slowly and times out:

DECLARE @SearchTerm nvarchar(30)
SET @SearchTerm = '123'
SET @SearchTerm = '"' + @SearchTerm + '"'

SELECT * FROM table
WHERE CONTAINS (comments, @SearchTerm)

Does this make any sense???

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server