Compile a Like Query in SQL Server 2012

Hello

I need to search a Database Table of 100,000 Records on Multiple Columns with Like.

The way I am doing it is Creating a SQL String and Executing it.

User Searches For "Word1 Word2 Word3"
Columns that Need to be Searched "Col1, Col2, Col3"

select name from test
where (col1 like '%word1%' or col2 like '%word1%' or col3 like '%word1%')
	and
		(col1 like '%word2%' or col2 like '%word2%' or col3 like '%word2%')
	and
		(col1 like '%word3%' or col2 like '%word3%' or col3 like '%word3%')

Open in new window



Is there a New Function / Feature / Way that can do this Better/Faster in SQL 2012?
p-platerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
No.  Even full text search won't really help with that.

If you put all those columns in an index -- name, col1, col2, and col3 -- SQL could scan just the index rather than the whole table.

If the searches are consistent and/or often repeated, you could store the search results in a different table and speed it up that way, but I suspect that's not the situation you have.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
p-platerAuthor Commented:
I have experimented with combining the data in all the columns to be searched into another column and breaking the words by characters in this column then setting a full text search on this new column.

This worked out around 4 times quicker than the previous like query (As I could use like 'word1%').

Wasn't sure if there was a new way though.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Not really a good or correct way to do this. Isn't usual to find a request to query a value for all columns in a table.
Your test already showed you the quickest way to perform the search.
0
p-platerAuthor Commented:
The Request Only needs to Query 10 out of 130 Columns.
i.e. Name, Delivery Address, Postal Address, Postcode, Email, Phone, Mobile etc.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Ok but that doesn't change my last comment.
You can try to index those 10 columns to see how much faster the query runs for LIKE solution and compare it with FULL TEXT SEARCH.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.