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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:05:57+00:00 2026-05-28T01:05:57+00:00

This is the whole query… SELECT s.*, (SELECT url FROM show_medias WHERE show_id =

  • 0

This is the whole query…

SELECT s.*, (SELECT url FROM show_medias WHERE show_id = s.id AND is_primary = 1) AS media_url
FROM (shows As s)
WHERE `s`.`id` IN (
 SELECT DISTINCT st.show_id
 FROM show_time_schedules AS sts
 LEFT JOIN show_times AS st ON st.id = sts.show_time_id
 WHERE sts.schedule_date BETWEEN CAST('2012-01-10' AS date) AND CAST('2012-01-14' AS date)
 )
AND `s`.`is_active` = 1
ORDER BY s.name asc 

If…

SELECT url FROM show_medias WHERE show_id = s.id AND is_primary = 1
(0.0004 sec)

And…

 SELECT DISTINCT st.show_id
 FROM show_time_schedules AS sts
 LEFT JOIN show_times AS st ON st.id = sts.show_time_id
 WHERE sts.schedule_date BETWEEN CAST('2012-01-10' AS date) AND CAST('2012-01-14' AS date)
(0.0061 sec)

Is there an obvious reason….

SELECT s.*, (inner query 1) AS media_url
FROM (shows As s)
WHERE `s`.`id` IN ( inner query 2 )
AND `s`.`is_active` = 1
ORDER BY s.name asc

is taking 5.7245 sec?

EXPLAIN EXTENDED

id  select_type         table       type    possible_keys   key     key_len ref                     rows    filtered    Extra
1   PRIMARY             s           ALL     NULL            NULL    NULL    NULL                    151     100.00      Using where; Using filesort
3   DEPENDENT SUBQUERY  sts         ALL     NULL            NULL    NULL    NULL                    26290   100.00      Using where; Using temporary
3   DEPENDENT SUBQUERY  st          eq_ref  PRIMARY         PRIMARY 4       bvcdb.sts.show_time_id  1       100.00      Using where
2   DEPENDENT SUBQUERY  show_medias ALL     NULL            NULL    NULL    NULL                    159     100.00      Using where
  • 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-28T01:05:57+00:00Added an answer on May 28, 2026 at 1:05 am

    You can always use EXPLAIN or EXPLAIN EXTENDED to see what MySql is doing with a query

    You could also write your query a slightly different way, have you tried the following?

    SELECT        s.*, 
                  sm.url AS media_url 
    FROM          shows AS s
    INNER JOIN    show_medias AS sm ON s.id = SM.show_id
    WHERE `s`.`id` IN ( 
                            SELECT DISTINCT st.show_id 
                            FROM show_time_schedules AS sts 
                            LEFT JOIN show_times AS st ON st.id = sts.show_time_id 
                            WHERE sts.schedule_date BETWEEN CAST('2012-01-10' AS date) AND CAST('2012-01-14' AS date) 
                            ) 
    AND            `s`.`is_active` = 1 
    AND            sm.is_primary = 1
    ORDER BY       s.name asc 
    

    It would be interesting to see what the effect of that is. I would expect it to be faster as, at the moment, I think MySql will be running inner query 1 for each show you have (so that one query will be run many times. A join should be more efficient.)

    Replace the INNER JOIN with a LEFT JOIN if you want all shows that don’t have a row in show_medias.

    EDIT:

    I’ll take a look at your EXPLAIN EXTENDED shortly, I also wonder if you want to try the following; it removes all of the subqueries:

    SELECT        DISTINCT s.*,  
                           sm.url AS media_url  
    FROM                   shows AS s 
    INNER JOIN             show_medias AS sm ON s.id = SM.show_id
    INNER JOIN             show_times AS st ON (s.id = st.show_id)
    RIGHT JOIN             show_time_schedules AS sts ON (st.id = sts.show_time_id)
    
    WHERE                  `s`.`is_active` = 1  
    AND                    sm.is_primary = 1 
    AND                    sts.schedule_date BETWEEN CAST('2012-01-10' AS date) AND CAST('2012-01-14' AS date)  
    ORDER BY               s.name asc 
    

    (It would also be good to see the EXPLAIN EXTENDED on these – you could add it to the comments for this one).

    Further EDIT:

    On your EXPLAIN EXTENDED (a good start on how to read these is here)

    The USING FILESORT and USING TEMPORARY are both key indicators. Hopefully, the second query I recommend should remove any TEMPORARY tables (in the subquery). Try then leaving the ORDER BY off to see if that makes a difference (and we can add that to the findings so far 🙂

    I can also see that the query is potentially missing out on a lot of index lookups; all of your id columns are prime candidates for index matches (with the usual index caveats). I’d also try adding those indexes and then running EXPLAIN EXTENDED again to see what the difference is now (EDIT as we already know from your comment above!)

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

Sidebar

Related Questions

Let's say I have something like this: $db=new PDO($dsn); $statement=$db->query('Select * from foo'); while
I have this MySql query : SELECT forum_categories.title, forum_messages.author, forum_messages.date AS last_message FROM forum_categories
I want to replace a query string like this: SELECT abc,def,ghi,jhk FROM table.... with
i have a problem to get the whole query from the url using the
It turns out this whole misunderstanding of the open() versus fopen() stems from a
This query gives an error: select ep, case when ob is null and b2b_ob
I made a package which I can use like this: select * from table(my_package.my_function(99,
I've been trying for the this whole a query who is officially giving me
I have this query: $events = db_query(SELECT e.name, e.event_date, e.time, e.data, e.picture, e.event_type, v.latitude,
I have this LINQ query: XNamespace ns = NAMESPACE; var items = (from c

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.