MySQL - can I avoid these correlated / dependant subqueries?

岁酱吖の 提交于 2019-12-11 20:39:17

问题


I have a MySQL query that I have been optimising, and currently it has 2 dependent / correlated subqueries.

I was wondering if it was possible to re write to avoid these?

SELECT *
FROM `pp_slides` 
JOIN `pp_slide_content` 
    ON `pp_slides`.`id` = `pp_slide_content`.`slide_id` 
    AND `pp_slide_content`.`version` = (
        SELECT max(`version`) FROM `pp_slide_content` WHERE `slide_id` = `pp_slides`.`id`
    )

LEFT JOIN `pp_published_slides` 
    ON `pp_published_slides`.`slide_id` = `pp_slides`.`id` 
    AND `pp_published_slides`.`slide_version` = `pp_slide_content`.`version` 
    AND `pp_published_slides`.`publish_id` = (
        SELECT max(`publish_id`) FROM `pp_published_slides` WHERE `pp_published_slides`.`slide_id` = `pp_slides`.`id` AND `pp_published_slides`.`slide_version` = `pp_slide_content`.`version`
    ) 


LEFT JOIN `pp_publish` ON `pp_publish`.`id` = `publish_id`

WHERE `pp_slides`.`product_id` =  '2'
AND `pp_slides`.`country_code` =  'gb'

A quick overview: A slide is created, and supports versioned changes. A slide (and other entities) are then published. The slide and the version that is published is set in the pp_published_slides tables. And the overall publish object is saved in pp_publish.

The above SQL will load up a slide object, and include extra data about the latest version, when it was published etc.

Here is a sqlfiddle http://sqlfiddle.com/#!2/902fb4/1

Any help would be greatly appreciated, kinda at the limits of my SQL knowledge....


回答1:


Here's an example showing part of your query rewritten without the correlated subquery...

SELECT s.*
     , c.*
  FROM slides s  
  JOIN slide_content c
    ON c.slide_id = s.id
  JOIN ( SELECT slide_id, MAX(version) max_version FROM slide_content GROUP BY slide_id ) x
    ON x.slide_id = c.slide_id
   AND x.max_version = c.version 
 WHERE s.product_id = 2
   AND s.country_code = 'gb';


来源:https://stackoverflow.com/questions/26278054/mysql-can-i-avoid-these-correlated-dependant-subqueries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!