1

我有这个查询:

SELECT
            DATE(
                wp_posts.post_date
            ) post_date,
            wp_posts.id,
            wp_posts.post_title,
            wp_posts.post_status,
            wp_posts.guid,
            COUNT(
                wp_plugin_most_popular_posts.post_id
            ) as count,
            (SELECT
                COUNT(
                    wp_plugin_most_popular_posts.post_id
                )
            FROM
                wp_plugin_most_popular_posts
            WHERE wp_plugin_most_popular_posts.post_id = wp_posts.ID 

            GROUP BY
                post_id) AS view_count
        FROM
            wp_posts

        INNER JOIN wp_term_relationships rel ON wp_posts.ID = rel.object_id
        INNER JOIN wp_term_taxonomy ttax ON rel.term_taxonomy_id = ttax.term_taxonomy_id
        INNER JOIN wp_terms terms ON ttax.term_id = terms.term_id

        JOIN wp_plugin_most_popular_posts ON wp_plugin_most_popular_posts.post_id = wp_posts.id
        WHERE
            wp_posts.post_status = "publish"

            AND taxonomy = "category"

        GROUP BY
            wp_plugin_most_popular_posts.post_id
        ORDER BY
            view_count DESC,
            post_date DESC
        LIMIT 10;

并解释退货

id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
1       PRIMARY ttax    ref     PRIMARY,term_id_taxonomy,taxonomy       taxonomy        98    const    28      Using where; Using temporary; Using filesort
1       PRIMARY terms   eq_ref  PRIMARY PRIMARY 8       peiraxtiri_db.ttax.term_id      1     Using index
1       PRIMARY rel     ref     PRIMARY,term_taxonomy_id        term_taxonomy_id        8     peiraxtiri_db.ttax.term_taxonomy_id      1       Using index
1       PRIMARY wp_posts        eq_ref  PRIMARY PRIMARY 8       peiraxtiri_db.rel.object_id   Using where
1       PRIMARY wp_plugin_most_popular_posts    ref     idx2    idx2    4       peiraxtiri_db.wp_posts.ID      96      Using where; Using index
2       DEPENDENT SUBQUERY      wp_plugin_most_popular_posts    ref     idx2    idx2    4     func     96      Using where; Using index

你能帮我优化一下吗?这个查询现在大约需要 7-9 秒 :( 我不是 mysql 优化专家,我不知道该怎么做......

4

0 回答 0