Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 554309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:41:46+00:00 2026-05-13T11:41:46+00:00

I am worried this is a bit expensive. Plus I will soon implement a

  • 0

I am worried this is a bit expensive.

Plus I will soon implement a normalized system for the tags so there will be additional joins.

On top of that I have 4 tables (tbl_videos, tbl_articles, tbl_galleries and tbl_users) of which I want to display three results of each and thus will have to run the query four times on one press of ‘search’.

SELECT *, 
(
(CASE WHEN `description` LIKE '%hotel%' THEN 1 ELSE 0 END) + 
(CASE WHEN `description` LIKE '%london%' THEN 1 ELSE 0 END) + 
(CASE WHEN `description` LIKE '%lazy%' THEN 1 ELSE 0 END) + 
(CASE WHEN `description` LIKE '%dog%' THEN 1 ELSE 0 END) +

(CASE WHEN `title` LIKE '%hotel%' THEN 1 ELSE 0 END) + 
(CASE WHEN `title` LIKE '%london%' THEN 1 ELSE 0 END) + 
(CASE WHEN `title` LIKE '%lazy%' THEN 1 ELSE 0 END) + 
(CASE WHEN `title` LIKE '%dog%' THEN 1 ELSE 0 END) +

(CASE WHEN `tags` LIKE '%hotel%' THEN 1 ELSE 0 END) + 
(CASE WHEN `tags` LIKE '%london%' THEN 1 ELSE 0 END) + 
(CASE WHEN `tags` LIKE '%lazy%' THEN 1 ELSE 0 END) + 
(CASE WHEN `tags` LIKE '%dog%' THEN 1 ELSE 0 END)

) AS relevance
FROM `table`
WHERE `description` LIKE '%hotel%'
  OR `description` LIKE '%london%'
  OR `description` LIKE '%lazy%'
  OR `description` LIKE '%dog%' 
  OR `title` LIKE '%hotel%'
  OR `title` LIKE '%london%'
  OR `title` LIKE '%lazy%'
  OR `title` LIKE '%dog%'
  OR `tags` LIKE '%hotel%'
  OR `tags` LIKE '%london%'
  OR `tags` LIKE '%lazy%'
  OR `tags` LIKE '%dog%'
ORDER BY relevance DESC
LIMIT 0 , 3;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:41:46+00:00Added an answer on May 13, 2026 at 11:41 am

    Yes, this is probably going to be quite resource-hungry, but it sounds like you have a business layer in front of it before you submit the query.

    Think about what you want to achieve, and what you could do yourself, in terms of parsing out search terms, or even giving the user separate fields for description, title and tags, and construct the query appropriately, to more direct the query, rather than effectively saying, “I’ve got some data quite like this search term, somewhere in the columns this table”.

    Once you have settled on your business logic (and if the above query is how it ends up, although I doubt it, then sobeit), then you should definitely run the query through the explain plan and see where you might pick up some indexes to help the database along.

    EDIT:

    Okay, so how’s this for a suggestion

    select matched_val, relevance from (
        select description as matched_val, count(*) as relevance
        from table 
        where description like '%hotel%'
        or description like '%london%'
        or description like '%lazy%'
        or description like '%dog%'
        group by description
    
        union all
    
        select title as matched_val, count(*) as relevance
        from table 
        where title like '%hotel%'
        or title like '%london%'
        or title like '%lazy%'
        or title like '%dog%'
        group by title
    
        union all
    
        select tags as matched_val, count(*) as relevance
        from table 
        where tags like '%hotel%'
        or tags like '%london%'
        or tags like '%lazy%'
        or tags like '%dog%'
        group by tags
    ) as a
    order by a.relevance desc
    LIMIT 0 , 3
    

    it at least means you only have to do your like matching once, rather than in the predicate and in the switch statement, plus the optimiser would be able to use an index on description, title and tags (which you will need to add yourself) and you should be away.

    have a go and see how your query optimiser likes it…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 301k
  • Answers 301k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you run through the process with a network trace… May 13, 2026 at 8:10 pm
  • Editorial Team
    Editorial Team added an answer Surely you've realized that putting a finite measurement on the… May 13, 2026 at 8:10 pm
  • Editorial Team
    Editorial Team added an answer You'll need to add a position field to your options… May 13, 2026 at 8:10 pm

Related Questions

I have an application with many NSToolbarItems. Only 5 or so are needed at
So for a bit of background, I am creating a website with the Zend
I have fixtures with initial data that needs to reside in my database (countries,
I ran into the problem that two drives connected at nearly the same time
I am trying to implement some unit testing for an old framework. I am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.