简体   繁体   中英

Converting subquery into Join

I would like to convert this query into a series on joins as its currently killing the db

SELECT releases.* FROM releases WHERE label_no_country IN (SELECT releases.label_no_country
                    FROM releases
                    INNER JOIN charts_extended
                    ON charts_extended.release_id=releases.id AND charts_extended.artist='Quickinho'
                    GROUP BY releases.label_no_country
                    ORDER BY count(releases.label_no_country) DESC
                    )
                    AND releases.id NOT IN (SELECT release_id FROM charts_extended WHERE artist='Quickinho')
                    AND releases.all_artists!='Various Artists'
                    AND releases.label_no_country!='Unknown Label'
                    ORDER BY date DESC
                    LIMIT 0,12
SELECT r.* 
  FROM releases r
  JOIN charts_extended x
    ON x.release_id = r.id 
   AND x.artist = 'Quickinho'
   AND x.label_no_country = r.label_no_country
  LEFT
  JOIN charts_extended y 
    ON y.release_id = r.id
 WHERE r.all_artists! = 'Various Artists'
   AND r.label_no_country != 'Unknown Label'
   AND y.release_id IS NULL
 ORDER 
    BY date DESC
 LIMIT 0,12

or something like that. Beyond that, we'd need to see an EXPLAIN and proper DDLs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM