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 684969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:52:29+00:00 2026-05-14T01:52:29+00:00

I am re-writing the SQL which lets a user search for any other user

  • 0

I am re-writing the SQL which lets a user search for any other user on our site and also shows their roles.

An an example, roles can be “Writer”, “Editor”, “Publisher”.

Each role links a User to a Publication.

Users can take multiple roles within multiple publications.

Example table setup:

"users" : user_id, firstname, lastname
"publications" : publication_id, name  
"link_writers" : user_id, publication_id  
"link_editors" : user_id, publication_id  

Current psuedo SQL:

SELECT * FROM (
  (SELECT user_id FROM users WHERE firstname LIKE '%Jenkz%') 
  UNION 
  (SELECT user_id FROM users WHERE lastname LIKE '%Jenkz%')
) AS dt
JOIN (ROLES STATEMENT) AS roles ON roles.user_id = dt.user_id

At the moment my roles statement is:

SELECT  dt2.user_id, dt2.publication_id, dt.role FROM (
  (SELECT 'writer' AS role, link_writers.user_id, link_writers.publication_id
  FROM link_writers)
  UNION
  (SELECT 'editor' AS role, link_editors.user_id, link_editors.publication_id
  FROM link_editors)
) AS dt2

The reason for wrapping the roles statement in UNION clauses is that some roles are more complex and require a table join to find the publication_id and user_id.

As an example “publishers” might be linked accross two tables

"link_publishers": user_id, publisher_group_id
"link_publisher_groups": publisher_group_id, publication_id

So in that instance, the query forming part of my UNION would be:

SELECT 'publisher' AS role, link_publishers.user_id, link_publisher_groups.publication_id
FROM link_publishers
JOIN link_publisher_groups ON lpg.group_id = lp.group_id

I’m pretty confident that my table setup is good (I was warned off the one-table-for-all system when researching the layout). My problem is that there are now 100,000 rows in the users table and upto 70,000 rows in each of the link tables.

Initial lookup in the users table is fast, but the joining really slows things down.

How can I only join on the relevant roles?

————————– EDIT ———————————-
explain

Explain above (open in a new window to see full resolution).

The bottom bit in red, is the “WHERE firstname LIKE ‘%Jenkz%'” the third row searches WHERE CONCAT(firstname, ‘ ‘, lastname) LIKE ‘%Jenkz%’. Hence the large row count, but I think this is unavoidable, unless there is a way to put an index accross concatenated fields?

The green bit at the top just shows the total rows scanned from the ROLES STATEMENT.

You can then see each individual UNION clause (#6 – #12) which all show a large number of rows. Some of the indexes are normal, some are unique.

It seems that MySQL isn’t optimizing to use the dt.user_id as a comparison for the internal of the UNION statements. Is there any way to force this behaviour?

Please note that my real setup is not publications and writers but “webmasters”, “players”, “teams” etc.

  • 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-14T01:52:29+00:00Added an answer on May 14, 2026 at 1:52 am

    Having checked out OMG Ponies’ answer for SO – Use Of Correlated Subquery, I came up with this:

    SELECT * FROM (
      (SELECT user_id FROM users WHERE firstname LIKE '%Jenkz%') 
      UNION 
      (SELECT user_id FROM users WHERE lastname LIKE '%Jenkz%')
    ) AS dt
    JOIN ( SELECT 'writer' AS role, link_writers.user_id, link_writers.publication_id
           FROM link_writers
           UNION
           SELECT 'editor' AS role, link_editors.user_id, link_editors.publication_id
           FROM link_editors
           UNION
           SELECT 'publisher' AS role, lp.user_id, lpg.publication_id
           FROM link_publishers lp
           JOIN link_publisher_groups lpg ON lpg.publisher_group_id = lp.publisher_group_id
         ) roles on roles.user_id = dt.user_id
    

    The explain looks reasonable on my tiny data set. What does it look like on the real thing ?

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

Sidebar

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.