替代嵌套选择mysql

时间:2018-09-25 10:10:59

标签: mysql select nested-select

此刻我正在运行一个查询,并且我相信所选的嵌套会造成瓶颈,还有我可以使用的替代选项吗?

这是我的查询

    SELECT post_id,
post_order,
post_parent,
post_recycle,
post_status,
post_ps_id,
post_v_id,
post_src,
post_sn_id,
post_qpc_id,
post_date_posted,
post_scheduled_local_datetime,
post_recycle_repeats,
post_recycle_expiry_date,
post_text,
post_v_title,
link_url,
link_preview_removed,
link_name,
link_description,
link_caption,
link_url_is_bitlink,
link_bitly_destination_url,
link_expanded_url,
link_initial_is_bitlink,
link_destination,
link_picture,
link_picture_size,
link_facebook_image,
link_facebook_title,
link_facebook_description,
link_facebook_caption,
link_twitter_card,
link_twitter_image,
link_twitter_title,
link_twitter_description,
pause_m_id,
qpca_evergreen_too_frequent,
sn_network,
qpc_name,
qpc_colour,
ps_m_id,
ps_filename,
ps_via,
ps_s3,
ps_width,
ps_height,
ps_gif,
video.*,
(
    SELECT COUNT(*) FROM post post_inner 
    WHERE (post_inner.post_parent = post.post_parent) 
    AND post_inner.post_status = 'published'
) 
    AS total_repeats 
    FROM post
    JOIN social_network ON sn_id = post_sn_id AND sn_status = 'active'
    JOIN queue_post_cat ON qpc_id = post_qpc_id
    LEFT JOIN queue_post_cat_account ON qpca_qpc_id = post_qpc_id AND qpca_sn_id = post_sn_id
    LEFT JOIN link ON link_id = post_link_id
    LEFT JOIN pause ON pause_m_id = qpc_m_id AND pause_qpc_id = post_qpc_id AND pause_sn_id = post_sn_id
    LEFT JOIN photo_status ON ps_id = post_ps_id
    LEFT JOIN video ON post_v_id = v_id AND v_transcoded = 1 LEFT JOIN facebook ON fb_db_id = sn_account_id AND sn_network = 'facebook' 
    WHERE post_status != 'now' 
    AND post_m_id = 1 
    AND qpca_sn_id IS NOT NULL 
    AND qpca_qpc_id IS NOT NULL 
    AND post_status = 'queue' AND (sn_network = 'facebook' 
    OR sn_network = 'instagram' OR sn_network = 'twitter') 
    AND qpc_m_id = 1 AND (fb_type IS NULL OR fb_type != 'profile') ORDER BY post_order ASC

我相信瓶颈正在发生,

SELECT COUNT(*) FROM post post_inner 
    WHERE (post_inner.post_parent = post.post_parent) 
    AND post_inner.post_status = 'published'

这是主选择中的选择,我能做些能使运行速度更快的事情吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用子查询:

select * from 
(
SELECT post_id,
post_order,
post_parent,
post_recycle,
post_status,
post_ps_id,
post_v_id,
post_src,
post_sn_id,
post_qpc_id,
post_date_posted,
post_scheduled_local_datetime,
post_recycle_repeats,
post_recycle_expiry_date,
post_text,
post_v_title,
link_url,
link_preview_removed,
link_name,
link_description,
link_caption,
link_url_is_bitlink,
link_bitly_destination_url,
link_expanded_url,
link_initial_is_bitlink,
link_destination,
link_picture,
link_picture_size,
link_facebook_image,
link_facebook_title,
link_facebook_description,
link_facebook_caption,
link_twitter_card,
link_twitter_image,
link_twitter_title,
link_twitter_description,
pause_m_id,
qpca_evergreen_too_frequent,
sn_network,
qpc_name,
qpc_colour,
ps_m_id,
ps_filename,
ps_via,
ps_s3,
ps_width,
ps_height,
ps_gif,
video.*,

    FROM post
    JOIN social_network ON sn_id = post_sn_id AND sn_status = 'active'
    JOIN queue_post_cat ON qpc_id = post_qpc_id
    LEFT JOIN queue_post_cat_account ON qpca_qpc_id = post_qpc_id AND qpca_sn_id = post_sn_id
    LEFT JOIN link ON link_id = post_link_id
    LEFT JOIN pause ON pause_m_id = qpc_m_id AND pause_qpc_id = post_qpc_id AND pause_sn_id = post_sn_id
    LEFT JOIN photo_status ON ps_id = post_ps_id
    LEFT JOIN video ON post_v_id = v_id AND v_transcoded = 1 LEFT JOIN facebook ON fb_db_id = sn_account_id AND sn_network = 'facebook' 
    WHERE post_status != 'now' 
    AND post_m_id = 1 
    AND qpca_sn_id IS NOT NULL 
    AND qpca_qpc_id IS NOT NULL 
    AND post_status = 'queue' AND (sn_network = 'facebook' 
    OR sn_network = 'instagram' OR sn_network = 'twitter') 
    AND qpc_m_id = 1 AND (fb_type IS NULL OR fb_type != 'profile')) A
inner join 
(
    SELECT post_parent ,COUNT(*) FROM post where post_status = 'published' group by post_parent 
) B on A.post_parent = B.post_parent
 ORDER BY post_order ASC