Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to improve query with multiple LIKEs with leading wildcards


How to improve query with multiple LIKEs with leading wildcards

Author
Message
Melanie Peterson
Melanie Peterson
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 249
I have an app that I'm adding a generic search function to. The user types in a word, or phrase, or portion of a word, and all items matching this search string anywhere in the database is returned. No surprise, it's running like a dog. Indexes may help, but not for the leading wildcards. Here's the WHERE clause:

WHERE @SearchTerm = A.EmpLogin
OR @SearchTerm = CM.EmpLogin
OR @SearchTerm = EC.ClientNum
OR EC.ClientName LIKE '%' + @SearchTerm + '%'
OR @SearchTerm = EM.MatterName
OR @SearchTerm = EM.MatterDesc
OR ET.[From] LIKE '%' + @SearchTerm + '%'
OR ET.[To] LIKE '%' + @SearchTerm + '%'
OR ET.CC LIKE '%' + @SearchTerm + '%'
OR ET.BCC LIKE '%' + @SearchTerm + '%'
OR ET.[Subject] LIKE '%' + @SearchTerm + '%'
OR ET.MessageBody LIKE '%' + @SearchTerm + '%'
OR @SearchTerm = E.EmpLogin
OR E.FirstName LIKE @SearchTerm + '%'
OR E.LastName LIKE @SearchTerm + '%'
OR E.Title LIKE '%' + @SearchTerm + '%'
OR @SearchTerm = LE.EmpLogin
OR D.[Description] LIKE '%' + @SearchTerm + '%'
OR @SearchTerm = D.ResAttorneyID
OR D.Comments LIKE '%' + @SearchTerm + '%'



I need the leading wildcards for fields like Comments and Description, basically anywhere you see wildcards they're needed. I have two questions: where would you recommend I put indexes (and of what type) and is there a better way to construct this query so that I can search for my search term within the text of fields, but it runs faster than it is now?

As a side note, only a few very trusted employees will have access to the app that this is used in, so I don't think I need to use dynamic sql here.

Thanks much!
Melanie Peterson
Melanie Peterson
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 249
Thanks! Can you suggest an app or two that would be appropriate?
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16550 Visits: 17004

I have an app that I'm adding a generic search function to. The user types in a word, or phrase, or portion of a word, and all items matching this search string anywhere in the database is returned. No surprise, it's running like a dog. Indexes may help, but not for the leading wildcards. Here's the WHERE clause:


I would take a slightly different approach than Celko. Just don't do this at all. There is no such thing as a third party app that can return a value that it finds in any column or row in the database in anything resembling a reasonable amount of time. At the very least you need to make the user identify what to search. This type of catch all search will never be anything but horribly slow.

You might want to read this article on Gail's blog about catch-all queries. I think you need to convert to something along the lines of what she explains. http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Melanie Peterson
Melanie Peterson
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 249
Thank you. How about FULL TEXT searching? Microsoft says it's faster than the equivalent LIKE statements. I've never used it. Is it worth looking into in this case?
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16550 Visits: 17004
Melanie Peterson (12/4/2012)
Thank you. How about FULL TEXT searching? Microsoft says it's faster than the equivalent LIKE statements. I've never used it. Is it worth looking into in this case?


It would certainly have a lot of potential of improving what you are trying to do. It is pretty simple to implement. Try it out on a dev server and see if you can get it to do what you want.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Melanie Peterson
Melanie Peterson
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 249
Thanks again.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45024 Visits: 39889
CELKO (12/4/2012)
[quote]Would you try to cut wood with a screw driver?



BWAAA-HAAA!!!! ONLY if I couldn't find my hammer! :-D:-P

--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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Melanie Peterson
Melanie Peterson
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 249
:-D Are law firms always so ridiculous? Never mind, I've worked at them since 1987, I know the answer. Email is only part of the data we need to trawl through, but not a bad guess! Thanks for the info.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search