Search code examples
sql-server-2008t-sqlfull-text-searchshort-circuiting

SQL 2008: Prevent Full-text lookup in query when not needed


From working on this specific situation, it was news to me that the logic operators are not short circuited in SQL.

I routinely do something along these lines in the where clause (usually when dealing with search queries):

WHERE
   (@Description IS NULL OR @Description = myTable.Description)

Which, even if it's not short-circuited in this example, doesn't really matter. However, when dealing with the fulltext search functions, it does matter.. If the second part of that query was CONTAINS(myTable.Description, @Description), it wouldn't work because the variable is not allowed to be null or empty for these functions.

I found out the WHEN statements of CASE are executed in order, so I can change my query like so to ensure the fulltext lookup is only called when needed, along with changing the variable from null to '""' when it is null to allow the query to execute:

WHERE
   (CASE WHEN @Description = '""' THEN 1 WHEN CONTAINS(myTable.Description, @Description) THEN 1 ELSE 0 END = 1)

The above code should prevent the full-text query piece from executing unless there is actually a value to search with.

My question is, if I run this query where @Description is '""', there is still quite a bit of time in the execution plan spent dealing with clustered index seeks and fulltextmatch, even though that table and search does not end up being used at all: is there any way to avoid this?

I'm trying to get this out of a hardcoded dynamic query and into a stored procedure, but if the procedure ends up being slower, I'm not sure I can justify it.


Solution

  • In case anyone else runs into a scenario like this, this is what I ended up doing, which is pretty close to what M_M was getting at; I broke away the full-text pieces and placed them behind branches:

    DECLARE @TableBfullSearch TABLE (TableAId int)
    IF(@TableBSearchInfo IS NOT NULL)
       INSERT INTO @TableBfullSearch
       SELECT
          TableAId
       FROM
          TableB
       WHERE
       ...(fulltext search)...
    
    DECLARE @TableCfullSearch TABLE (TableAId int)
    IF(@TableCSearchInfo IS NOT NULL)
       INSERT INTO @TableCfullSearch
       SELECT
          TableAId
       FROM
          TableC
       WHERE
       ...(fulltext search)...
    
    --main query with this addition in the where clause
    SELECT
      ...
    FROM
       TableA
    WHERE
       ...
       AND (@TableBSearchInfo IS NULL OR TableAId IN (SELECT TableAId FROM @TableBfullSearch))
       AND (@TableCSearchInfo IS NULL OR TableAId IN (SELECT TableAId FROM @TableCfullSearch))
    

    I think that's probably about as good as it'll get without some sort of dynamic query