Improve Your Search Algorithms

A normal practice in implementing a search is to use the “like” operator. As long as you are searching for your keyword in only one column, there will be no problem. However, what if you encounter a situation where you want to search/different keyword(s) from more than 2 or 3 columns?

Let us say you’ve got table “tblsearch” with fields fld1,fld2,fld3.

For a single column search, you can use:

  --//////////////////////////////////////SELECT * from tblsearch where fld1 like '%something%' --For multi column search : SELECT * from tblsearch where fld1 like '%something%' or     fld2 like '%something%' or     fld3 like '%something%' --//////////////////////////////////////

This will work fine if you want to search only one keyword. However, if you want to search more than one keyword, use this approach:

  --////////////////////////////////////// --Single keyword: SELECT * from tblsearch where (fld1+fld2+fld3) like '%something%' --Multiple keywords: SELECT * from tblsearch where (fld1+fld2+fld3) like '%first%'      or (fld1+fld2+fld3) like '%second%' --//////////////////////////////////////

This second query is better than the first query for two reasons has been proven to be more efficient and uses less resources.

If you use a clustering index, then it will increase the performance of first query. But only one clustered index can be created per table. It also wins out over the first query in optimization and readability.

In an extra bonus, the second method makes use of the Derived tables concept. Look at this additional query:

 select * from(select (fld1+fld2+fld3) as myfld,* from tblsearch) Awhere A.myfld like '%first%'or A.myfld like '%second%'
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a