代码之家  ›  专栏  ›  技术社区  ›  Carson Myers

有谁能帮我在这个笨拙的MySQL查询变得更糟之前驯服它?

  •  1
  • Carson Myers  · 技术社区  · 14 年前

    它只是检查所有的文章,看看它们是来自同一个用户,还是与显示列表的帖子属于同一个类别。当用户提交一篇文章时,它仍然由发布文章的管理员“发布”(因为他们必须首先获得批准)。因此,当用户提交一篇文章时,会出现一个单独的db条目。

    基本结构:

    select post_id, post_title, post_name, (
        //number of times this article has been
        //favorited, to help sort them
    ) as favorited, (
        //whether this article is from the same user
        select count(*) > 0 from wp_users //have to put something, it's just a flag
        where exists (
            //see if this post was authored by the the same user(s)
            and not exists (
                //make sure it's not a user submitted article. If it is,
                //we want to group it by the submitting user, not the
                //approving editor
            )
        )
        or exists (
            //see if an 'original submitter username' exists for this post,
            //and if so, see if it matches the post we're generating the list for
        )
    ) as under_users, (
        //see if this post is under the same category
    ) as under_category
    

    还有我的疑问:

    select p.ID, p.post_title, p.post_name, (
      select count(*)
      from wp_usermeta as um
      where um.meta_key = 'wpfp_favorites'
      and POSITION(CONCAT("\"",p.ID,"\"") IN um.meta_value)
    ) as favorited, (
      select count(*) > 0 from wp_users
      where exists ( 
        select *
        from wp_terms as t, wp_term_taxonomy as tt, wp_term_relationships as tr
        where tt.taxonomy = 'author'
        and tr.object_id = p.ID
        and tr.term_taxonomy_id = tt.term_taxonomy_id
        and t.term_id = tt.term_id
        and t.term_id in (
          select t2.term_id
          from wp_terms as t2, wp_term_taxonomy as tt2, wp_term_relationships as tr2
          where tt2.taxonomy = 'author'
          and tr2.object_id = 535
          and tr2.term_taxonomy_id = tt2.term_taxonomy_id
          and t2.term_id = tt2.term_id
        )
        and not exists (
          select *
          from wp_postmeta as pm
          where pm.post_id = 535
          and pm.meta_key = 'Original Submitter Username'
        )
      )
      or exists (
        select *
        from wp_postmeta as pm
        where pm.post_id = p.ID
        and pm.meta_key = 'Original Submitter Username'
        and pm.meta_value = (
          select pm2.meta_value
          from wp_postmeta as pm2
          where pm2.post_id = 535
          and pm2.meta_key = 'Original Submitter Username'
        )
      )  
    ) as under_users, (
      select count(*) > 0 from wp_users
      where exists (
        select *
        from wp_terms as t, wp_term_taxonomy as tt, wp_term_relationships as tr
        where tt.taxonomy = 'category'
        and tr.object_id = p.ID
        and tr.term_taxonomy_id = tt.term_taxonomy_id
        and t.term_id = tt.term_id
        and t.term_id in (
          select t2.term_id
          from wp_terms as t2, wp_term_taxonomy as tt2, wp_term_relationships as tr2
          where tt2.taxonomy = 'category'
          and tr2.object_id = 535
          and tr2.term_taxonomy_id = tt2.term_taxonomy_id
          and t2.term_id = tt2.term_id
          and t2.term_id not in (3, 4)
        )
      )
    ) as under_category
    from wp_posts as p
    where p.post_type = 'post' 
    and p.ID != 535
    and p.post_status = 'publish'
    having (
      under_users != 0
      or under_category != 0
    )
    order by favorited desc
    

    有什么建议可以帮我吗?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Brian Hooper    14 年前

    不要太担心你的问题太复杂。在实际应用程序中,查询是这样的。如果开始出现问题(请记住,可以在SQL语句中嵌入注释),可以创建处理某些子查询的视图。例如,您的子查询为under\ u category。你可以创建一个这样的视图。。。

    create view under_category_view as
        select tr.object_id AS p_id
            from wp_terms              as t,
                 wp_term_taxonomy      as tt,
                 wp_term_relationships as tr
            where tt.taxonomy             = 'category'
                  and tr.term_taxonomy_id = tt.term_taxonomy_id
                  and t.term_id           = tt.term_id
                  and t.term_id in (select t2.term_id
                                        from wp_terms              as t2,
                                             wp_term_taxonomy      as tt2,
                                             wp_term_relationships as tr2
                                        where tt2.taxonomy = 'category'
                                              and tr2.object_id = 535
                                              and tr2.term_taxonomy_id = tt2.term_taxonomy_id
                                              and t2.term_id = tt2.term_id
                                              and t2.term_id not in (3, 4));
    

    然后在你的大查询中你会用。。。

    select count(*) > 0 from wp_users
      where exists (select *
                        from user_category_view
                        where p_id = p.id) as under_category
    

    顺便说一句,我发现垂直拆分行和使用大缩进(正如我在这里所做的)有助于使大块查询更易于阅读。

        2
  •  3
  •   Chris Henry    14 年前

    EXISTS 从句至 EXISTS IN (ids...) .

    我发现MySQL中的嵌套子查询往往非常慢,因为一次需要检查的行数太多。在应用程序级别中断子查询允许您使用缓存,并使您能够更好地控制子查询的操作,同时使SQL更易于阅读。