嵌套选择mysql的替代方法

hgc7kmma  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(338)

我现在正在运行一个查询,我相信所选的嵌套正在创建一个瓶颈,是否有其他选项可以使用?
这是我的问题,

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'

哪一个是主选择中的选择,我能做些什么让它跑得更快吗?

tjvv9vkg

tjvv9vkg1#

您可以尝试使用子查询:

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

相关问题