Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone

I wanted to know that what may be the best practice to search for keywords or sentence in sql server.

Suppose I have a table say products with name, description and keywords. I have a search function in my website where the user may type in anything (sentence or single word).
I query the product table columns using like clause and shortlist the list to rows containing the search criteria.
To reduce the response time in case if the volume of data in product table is large, I add a pagination query of 10.
Is the above explained scenario a good practice?

Thanks in advance

What I have tried:

My current query

select * from (select srno as productid, name +' '+ descriptions +' '+ keywords as search
from ProductDetails) PD
where PD.search LIKE '%bed%' OR PD.search LIKE '%sheets%'

Here I have combined the three columns namely name,descriptions and keywords and I am searching for "bed sheets" in my product table

Table Structure

ProductDetails example
srno|Name|Description|keywords
1|Floral Bed Sheets|..some text..|bed sheets
2|Square Pillow Covers|.. some text..|pillow covers square

Is it proper to place keywords in a separate column or should I create a new table for keywords.
If I have to create a new table for keywords, how to modify the above query?
Posted
Updated 7-Sep-16 10:37am
v2
Comments
an0ther1 28-Aug-16 19:50pm    
Hi JPais,

Pagination will not help - the full result set needs to be identified and then the first 'x' number of records (10 in your case) is returned. It reduces the result set passed to the client but it doesn't speed up your query.
Additionally indexing will not help because you are using %search term%. SQL can use text prior to the first wildcard to identify a result set but that is all.
There is a few options though;
a) Implement categories in your product table - this will help limit your results.
b) Implement Full Text Search - this is probably the best option
c) Create a search table that contains the search words - this is basically re-creating full text search manually & I wouldn't recommend it.

Kind Regards
JPais 4-Sep-16 5:30am    
Thank u for your reply.. can u pls help me with your third option. Actually I am using a shared server and iy does not allow me to set up full text server. So can you pls guide me through your third option.
Also will "like query" as the one in my query will slow the search process?

I feel you show go for full text indexing.

Read more from Understanding Full-Text Indexing in SQL Server - Simple Talk[^].

Above to that you can implement fuzzy search algorithm for approximate string matching.
 
Share this answer
 
I used your second solution for a similar problems.
You can add a index to the fields containing the word searched and do this :

select srno as productid, name +' '+ descriptions +' '+ keywords as search
from ProductDetails as PD
where PD.search LIKE '%bed%' OR PD.search LIKE '%sheets%'

but I suggest you if it is possible to use only like in this way

select srno as productid, name +' '+ descriptions +' '+ keywords as search
from ProductDetails
where PD.search LIKE 'bed%' OR PD.search LIKE 'sheets%'

in this way you don't lost your index
 
Share this answer
 
Comments
JPais 6-Sep-16 3:05am    
Is it possible to achieve full text search capability by manual method. i.e. creating a manual indexing system using tables and querying the same. Can you guide me with my example? Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900