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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:22:10+00:00 2026-05-12T17:22:10+00:00

I am working with a legacy database schema that looks like this: product_table table

  • 0

I am working with a legacy database schema that looks like this:

product_table table has fields:

uid (int, primary key)
name (varchar 50)

category table has fields:

uid (int, primary key)

name (varchar 50)


Allright, now the product_table has 4 MM relations to the category table:

product_table__category_1__mm has fields:

uid_local (int, contains product_table.uid)
uid_foreign (int, contains category.uid)

product_table__category_2__mm has fields:

uid_local (int, contains product_table.uid)
uid_foreign (int, contains category.uid)

product_table__category_3__mm has fields:

uid_local (int, contains product_table.uid)
uid_foreign (int, contains category.uid)

product_table__category_4__mm has fields:

uid_local (int, contains product_table.uid)
uid_foreign (int, contains category.uid)

(yes, all the 4 MM tables have the same fields and all relate to the category table)


So, if I want to pull in all four joins and filter based on int values selected by the user it would look something like this:

select
product_table.*

from
product_table

inner join product_table__category_1__mm mm_1 on mm_1.uid_local = product_table.uid
inner join category cat_1 on cat_1.uid = mm_1.uid_foreign and cat_1.uid in (7, 8)

inner join product_table__category_2__mm mm_2 on mm_2.uid_local = product_table.uid
inner join category cat_2 on cat_2.uid = mm_2.uid_foreign and cat_2.uid in (63, 52)

inner join product_table__category_3__mm mm_3 on mm_3.uid_local = product_table.uid
inner join category cat_3 on cat_3.uid = mm_3.uid_foreign and cat_3.uid in (84, 12)

inner join product_table__category_4__mm mm_4 on mm_4.uid_local = product_table.uid
inner join category cat_4 on cat_4.uid = mm_4.uid_foreign and cat_4.uid in (16, 33)

group by product_table.uid ### --> in order to get unique results

Now thats a big query, but I cant change the db design as it is already widely in use.

Any idea on how to make this query faster? Where would you put indexes?

  • 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-12T17:22:11+00:00Added an answer on May 12, 2026 at 5:22 pm

    You need to get rid of GROUP BY here.

    MySQL is not good in optimizing it.

    Rewrite your query as this:

    SELECT  *
    FROM    product_table
    WHERE   EXISTS
            (
            SELECT  NULL
            FROM    product_table__category_1__mm mm_1
            JOIN    category cat_1
            ON      cat_1.uid = mm_1.uid_foreign
            WHERE   mm_1.uid_local = product_table.uid
                    AND mm_1.uid_foreign IN (7, 8)
            )
            AND
            …        
    

    Create the indexes:

    product_table__category_*__mm (uid_local, uid_foreign)
    

    or, better, declare them PRIMARY KEYs on product_table__category_*__mm:

    ALTER TABLE product_table__category_*__mm ADD CONSTRAINT pk_pc*mm_local_foreign (uid_local, uid_foreign)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a legacy database table that has a phone no. field but
I am working with a legacy database that has a table to store up
Im working on a legacy database, that cant be changed. I have a table
I am working with a legacy database that has a large number of user-defined
I'm working with a legacy SQL Server database which has a core table with
I'm working on a legacy database that is complete non-sense. I have a table
I am working with a legacy database that stores blank values as a single
The project I'm working on has a legacy database with lots of information in
So, my problem is this. I have a legacy MySQL database that I'm building
I'm working with a legacy database that uses a three column key for products.

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.