Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

How to improve query with multiple LIKEs with leading wildcards Expand / Collapse
Author
Message
Posted Tuesday, December 4, 2012 10:23 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, May 12, 2014 11:14 AM
Points: 78, Visits: 187
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!
Post #1392610
Posted Tuesday, December 4, 2012 12:11 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 1,945, Visits: 2,922
I have an app to which I'm adding a generic search function.


Do not do this. There are some nice cheap or free text search tools you can get. This is never going to work. Would you try to cut wood with a screw driver?


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1392651
Posted Tuesday, December 4, 2012 12:19 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, May 12, 2014 11:14 AM
Points: 78, Visits: 187
Thanks! Can you suggest an app or two that would be appropriate?
Post #1392654
Posted Tuesday, December 4, 2012 12:44 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 13,302, Visits: 12,168

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)
Post #1392666
Posted Tuesday, December 4, 2012 3:03 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, May 12, 2014 11:14 AM
Points: 78, Visits: 187
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?
Post #1392721
Posted Tuesday, December 4, 2012 3:15 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 13,302, Visits: 12,168
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)
Post #1392725
Posted Tuesday, December 4, 2012 3:15 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, May 12, 2014 11:14 AM
Points: 78, Visits: 187
Thanks again.
Post #1392726
Posted Tuesday, December 4, 2012 5:48 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 36,995, Visits: 31,516
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!


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1392754
Posted Tuesday, December 4, 2012 9:05 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 1,945, Visits: 2,922
Can you suggest an app or two that would be appropriate?


I do not know what the application is, but it looks like searching email at a law office. ZyIndex was the Gold Standard for law offices in my day. Its commands look like Lexis and WestLaw, so the lawyers had no trouble using it.

Today, you would have to do research. Or ask the lawyer's support staff.


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1392772
Posted Thursday, December 6, 2012 8:22 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, May 12, 2014 11:14 AM
Points: 78, Visits: 187
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.
Post #1393555
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse