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

  • SEARCH
  • Home
  • 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 1044333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:48:41+00:00 2026-05-16T15:48:41+00:00

This query will be done in a cached autocomplete text box, possibly by thousands

  • 0

This query will be done in a cached autocomplete text box, possibly by thousands of users at the same time. What I have below works, bit I feel there may be a better way to do what I am doing.

Any advice?

UPDATED — it can be ‘something%’:

SELECT a.`object_id`, a.`type`, 
    IF( b.`name` IS NOT NULL, b.`name`,
        IF( c.`name` IS NOT NULL, c.`name`, 
            IF( d.`name` IS NOT NULL, d.`name`,
                IF ( e.`name` IS NOT NULL, e.`name`, f.`name` )
            )
        )
    ) AS name
FROM `user_permissions` AS a 
   LEFT JOIN `divisions` AS b 
       ON ( a.`object_id` = b.`division_id` 
           AND a.`type` = 'division' 
           AND b.`status` = 1 ) 
   LEFT JOIN `departments` AS c 
       ON ( a.`object_id` = c.`department_id` 
           AND a.`type` = 'department'  
           AND c.`status` = 1 ) 
   LEFT JOIN `sections` AS d 
       ON ( a.`object_id` = d.`section_id` 
           AND a.`type` = 'section'  
           AND d.`status` = 1 ) 
   LEFT JOIN `units` AS e 
       ON ( a.`object_id` = e.`unit_id` 
           AND a.`type` = 'unit'  
           AND e.`status` = 1 ) 
   LEFT JOIN `positions` AS f 
       ON ( a.`object_id` = f.`position_id` 
           AND a.`type` = 'position'  
           AND f.`status` = 1 ) 
WHERE a.`user_id` = 1 AND ( 
    b.`name` LIKE '?%' OR
    c.`name` LIKE '?%' OR
    d.`name` LIKE '?%' OR
    e.`name` LIKE '?%' OR
    f.`name` LIKE '?%'
)
  • 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-16T15:48:41+00:00Added an answer on May 16, 2026 at 3:48 pm

    Other than changing the nested Ifs to use a Coalesce() function (MySql has Coalesce() doesn’t it)? There is not much you can do as long as you need to filter on that input parameter with a like expresion. Putting a filter on a column using a Like expression, where the Like parameter has a wildcard at the begining, as you do, makes the query argument non-SARG-able, which means that the query processor must do a complete table scan of all the rows in the table to evaluate the filter predicate.

    It cannot use an index, because an index is based on the column values, and with your Like parameter, it doesn’t know which index entries to read from (since the parameter starts with a wild card)

    if MySql has Coalesce, you can replace your Select with:

    SELECT a.`object_id`, a.`type`,
    Coalesce(n.name, c.name, d.Name, e.Name) name
    

    If you can replace the search argument parameter so that it does not start with a wildcard, then just ensure that there is an index on the name column in each of the tables, and (if there are not indices on that column now), the query performance will increase enormously.

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

Sidebar

Related Questions

I have a form where when it was created it will this query public
in my self-defined module I have this method which will query the db and
I will make this quick and simple I have a query and I want
I have this query: SELECT p.text,se.name,s.sub_name,SUM((p.volume / (SELECT SUM(p.volume) FROM phrase p WHERE p.volume
I have a query like this (from u in DataContext.Users where u.Division == strUserDiv
Is there a reason why this query doesn't work? The following query will work
Also what will be the scenarios where this query is used select * from
I am trying to build a query which will do this: Lets say for
this code will not pass the query string to default3.aspx on Image Button ..
Consider the following criteria query: var x = SomeCriteria.AddOrder(new Order(Name, true)).List(); This will order

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.