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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:05:49+00:00 2026-05-11T13:05:49+00:00

I have a very complex MySQL query that includes use of the same subquery

  • 0

I have a very complex MySQL query that includes use of the same subquery three times. Will MySQL actually run the subquery three times? (It’s an expensive one.) If so, is there a way for me to tell MySQL to save or cache the results so it won’t do that? I could save the data in a large array then re-feed it to MySQL, but I’d rather not move it out and back into the database like that.

This is the subquery that appears three times:

SELECT id FROM programs  WHERE submitter_id=32 AND id in (     SELECT id FROM programs      WHERE feed_id=2478 AND id in (         SELECT program_id FROM playlist_program_map          WHERE playlist_id=181))) 

And here’s an example of the full query in which the query appears:

SELECT object_id, programs.created AS created,  MATCH(text) AGAINST ('excellent ' IN BOOLEAN MODE) AS relevance  FROM comments_programs USE INDEX (text)  LEFT JOIN programs ON programs.id=object_id  WHERE object_id IN (     SELECT id FROM programs      WHERE 1 AND id IN (         SELECT id FROM programs          WHERE submitter_id=32 AND id in (             SELECT id FROM programs              WHERE feed_id=2478 AND id in (                 SELECT program_id FROM playlist_program_map                  WHERE playlist_id=181))))  AND MATCH(text) AGAINST ('excellent ' IN BOOLEAN MODE)>0)  UNION (  SELECT object_id, programs.created AS created,  MATCH(text) AGAINST ('excellent ' IN BOOLEAN MODE) AS relevance  FROM descriptions_programs USE INDEX (text)  LEFT JOIN programs ON programs.id=object_id  WHERE object_id IN (     SELECT id FROM programs      WHERE 1 AND id IN (         SELECT id FROM programs          WHERE submitter_id=32 AND id in (             SELECT id FROM programs              WHERE feed_id=2478 AND id in (                 SELECT program_id FROM playlist_program_map                  WHERE playlist_id=181))))  AND MATCH(text) AGAINST ('excellent ' IN BOOLEAN MODE)>0 AND current=1 )   UNION (  SELECT object_id, programs.created AS created,  MATCH(text) AGAINST ('excellent ' IN BOOLEAN MODE) AS relevance  FROM titles_programs USE INDEX (text)  LEFT JOIN programs ON programs.id=object_id  WHERE object_id IN (     SELECT id FROM programs      WHERE 1 AND id IN (         SELECT id FROM programs          WHERE submitter_id=32 AND id in (             SELECT id FROM programs              WHERE feed_id=2478 AND id in (                 SELECT program_id FROM playlist_program_map                  WHERE playlist_id=181))))  AND MATCH(text) AGAINST ('excellent ' IN BOOLEAN MODE)>0 AND current=1; 
  • 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. 2026-05-11T13:05:49+00:00Added an answer on May 11, 2026 at 1:05 pm

    See what EXPLAIN EXTENDED says.

    If it says DEPENDENT SUBQUERY or UNCACHEABLE SUBQUERY, then it will be reevaluated each time it’s used.

    This happens if the subquery uses session variables or is a correlated subquery.

    If it doesn’t, it most probably will be cached.

    If your case the subquery will not be cached, it will be reevaluated in each UNION‘ed set.

    You subquery, though, seems to be too complicated. Why don’t you just use:

    SELECT id FROM   playlist_program_map ppm, programs p WHERE  ppm.playlist_id = 181        AND p.id = ppm.program_id        AND submitter_id = 32        AND feed_id = 2478 

    If you have an index on playlist_program_map (playlist_id), this query should work like a charm.

    Could you please tell me two more things:

    1. How many rows are there in playlist_program_map and how many DISTINCT playlist_id values are there?
      • How many rows are there in programs and how many DISTINCT submitter_id, feed_id pairs are there?

    From your comment I can conclude that there are 10 programs per playlist in average, and 200 programs per (submitter, feed) pair. This means your index on playlist_program_map is more selective than the one on (submitter, feed), and playlist_program_map must be leading in the join.

    The fulltext index in your case also doesn’t seem to be very selective, given that you need to join 10 programs out of 2,000,000.

    You may better try the following:

    SELECT object_id, programs.created AS created FROM   playlist_program_map ppm, programs p, comments_programs cp WHERE  ppm.playlist_id = 181        AND p.id = ppm.program_id        AND p.submitter_id = 32        AND p.feed_id = 2478        AND cp.object_id = p.id        AND cp.text REGEXP 'excellent' 

    , and repeat this for all three tables.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can leverage apache's mod_negotiation module. This way you can… May 12, 2026 at 12:10 am
  • Editorial Team
    Editorial Team added an answer Create a pivot table. Create a new column call 'Day'… May 12, 2026 at 12:10 am
  • Editorial Team
    Editorial Team added an answer Do not save plain MD5 hashes in your database. Plain… May 12, 2026 at 12:10 am

Related Questions

I've alluded to this project before, in this question, but the scope of redesign
I have three tables in MySQL that are related, yet not technically linked to
I have a big doubt. Let's take as example a database for a whatever
The expressiveness of the query languages (QL) provided with ORMs can be very powerful.

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.