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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:50:19+00:00 2026-06-09T18:50:19+00:00

Is there any way to optimize the query below? It requires looking through multiple

  • 0

Is there any way to optimize the query below? It requires looking through multiple tables. Thank you in advance for your help it is appreciated. Relevant schema:

CREATE TABLE `variant_bikes` (
  `variant_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
  `bike_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`variant_id`,`bike_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `product_bikes` (
  `product_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
  `bike_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`product_id`,`bike_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `cart_products` (
  `product_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`product_id`),
) ENGINE=MyISAM AUTO_INCREMENT=95 DEFAULT CHARSET=utf8;

CREATE TABLE `cart_product_options` (
  `option_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `product_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`option_id`),
) ENGINE=MyISAM AUTO_INCREMENT=9040 DEFAULT CHARSET=utf8;

CREATE TABLE `cart_product_option_variants` (
  `variant_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `option_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`variant_id`),
) ENGINE=MyISAM AUTO_INCREMENT=9040 DEFAULT CHARSET=utf8;

And SQL is:

select distinct p.product_id 
from cart_products p 
     left join product_bikes b on p.product_id = b.product_id where bike_id = $bike_id
or
p.product_id in (
    select product_id from cart_product_options where option_id in (
        select option_id from cart_product_option_variants where variant_id in (
            select variant_id from variant_bikes where bike_id=$bike_id
        )
    )
)

Edit: Short is a customer selects a bike and this selects the products which fit, either the product itself or the product option variants. Note this code works I just want to know if there is a way to optimize it.

  • 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-06-09T18:50:21+00:00Added an answer on June 9, 2026 at 6:50 pm

    Though the nature to my question changed a bit the join strategy works, but is it the best answer? i.e. does it resolve my original question of it being optimized as both work:

    select distinct p.product_id from cscart_products p 
        left join product_bikes pb on p.product_id = pb.product_id and pb.bike_id = 111
        left join cscart_product_options po on po.product_id = p.product_id
        left join cscart_product_option_variants pov on pov.option_id = po.option_id
        left join variant_bikes vb on vb.variant_id = pov.variant_id and vb.bike_id = 111
        where pb.bike_id = 111 or vb.bike_id = 111
    

    This resulted in no noticeable speed boost however. (sometimes it would return quicker, other times slower probably due to server load at the time).

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

Sidebar

Related Questions

Is there any way to optimize the query below? SELECT DATE_FORMAT(a.duedate,'%d-%b-%y') AS dte, duedate,
I'm wondering if there's any way to optimize the following SELECT query. (Note: I
Is there any way to optimize this query? It takes more than 2.5 secs.
is there any way how to optimize next query: EXPLAIN EXTENDED SELECT keyword_id, ck.keyword,
Is there any way I can avoid using array_flip to optimize performance. I am
I wonder if there is any wise way to rewrite the following query so
Is there any way to optimize the following line of C code (to avoid
Is there any way in Notepad++ (or even with another tool) to change the
Is there any way I can set a formatter on models that will convert
Is there any way to use Google's API to retrieve a user's current zipcode

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.