Like operator - Query performance issue

  • Dear All

    Have a table with 15 million rows. And Almost 15 columns. I am using following query. which is very slow. This table is having cluster index

    select top 100

    from TABLE

    where COLUMN1 like '%xxx%'

    OR Column2 like '%xxx%'

    .

    .

    .

    OR Column12 like '%xxx%'

    Is there a better way to get the same result.

  • The % at the start means your search needs to read every row and look for a match. If you really need that kind of search you should look into using a full text index.

    There is also a bug related with LIKE Statement. you can look to that as well.

    http://support.microsoft.com/kb/2847467

    Hope it helps.

  • Partitions would be helpful.Then you could also restrict conditions in where clause

  • create full text index fro all the 15 columns?

  • sorry did not understand about partitionong

  • Partitioning is unlikely to help at all.

    Unless you can add some SARGable predicates (predicates where SQL can use indexes to reduce the rows it has to read), there's probably not much that can be done. LIKE with a leading wildcard requires SQL to read every row, so that query will read every row of the table.

    Full text may or may not help. Try it out and see.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Partitioning is not primarily a performance tuning mechanism. It is primarily a data management mechanism. There are cases where you can get performance improvements, but you need to remember that it's not designed to do that. It's designed to help with large scale data management.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • As an aside, it seems odd that you are searching so many of those columns. Can you show us what the data looks like?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • gsaini 95591 (3/31/2014)


    Partitions would be helpful.Then you could also restrict conditions in where clause

    We're talking about a leading wildcard in 12 different columns. As the others have suggested, parititioning wouldn't help that at all.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Krishna1 (3/31/2014)


    create full text index fro all the 15 columns?

    Sounds like a good option, have used this in similar situations before with good results.

  • Dear All

    Thanks for your replies. I created full text index but when see the execution plan it does not show the full text index but shows cluster index.

    And the query is still slow.

    Regards

    Krishna

  • You have to change your code to use the full text index. It's not like a regular index where the optimizer just picks it up. You need to read up on this. Here's an introduction.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply