在做一个标签系统, Tag本身类似于树结构,用parent标记Tag的从属关系, Tag只有两层, 子TAG不会再有孩子。
Tag(id, name, parent), 
文章的表结构
Article(id, title)
还有Tag_Article关联表,Article和Tag是多对多关系。
Tag_Article(tag_id, article_id).现在已知tag的id, 要查出所有的子TAG对应的文章。结果形如Article(*) + Tag(id, name).
Tag表的大小,远小于Article表, TAG_Article表的大小为5*Article左右,就是每个文章大概有5个Tag.
怎么可以用一条SQL高效的完成上面的功能。
还是用多条SQL,先查出子TAG,再查文章
谢谢各位大侠.

解决方案 »

  1.   

    如果Tag表的大小10w, Article 100w, 怎么优化?
    偶几乎没有写过SQL, 大家一定要帮帮我啊
      

  2.   

    既然层数只有两层,那就left join
      

  3.   

    select * from article, tag_article join tag on tag.parent = 2 && tag_article.tag_id = tag.id where article.id = tag_article.article_id;这个是我写的,但大小表Join之类的,我把握不好.
      

  4.   

    Tag
    id   name       parent
    (1,  'SQL',     0)
    (2,  'MS',      1)
    (3,  'oracle',  1)Article
    id   title 
    (1,  'SQL how to')
    (2,  'reference')Tag_Article
    tag_id    article_id
    (2,       1)
    (3,       2)查找parent = 1
    结果
    id    title         tag_id   tag_name
    1     'SQL how to'  2        'MS'
    2     'reference'   3        'oracle'
      

  5.   


    我不太清楚SQL的执行过程, 上面的SQL会不会产生Article * tag_article这种联接
      

  6.   


    --查询的逻辑执行过程,来自技术内幕 
    (8)  SELECT (9) DISTINCT (11) <TOP_specification> <select_list> 
    (1)  FROM <left_table> 
    (3)    <join_type> JOIN <right_table> 
    (2)      ON <join_condition> 
    (4)  WHERE <where_condition> 
    (5)  GROUP BY <group_by_list> 
    (6)  WITH {CUBE | ROLLUP} 
    (7)  HAVING <having_condition> 
    (10) ORDER BY <order_by_list> 
      

  7.   

    ---------------------------------
    --  Author: Beirut(贝鲁特)
    --  Comment:小爱
    --  Date  : 2009-08-04 18:49:08
    ---------------------------------
    create table Tag (id int ,name varchar(20),   parent int)
    insert into tag values(1,  'SQL',    0) 
    insert into tag values(2,  'MS',      1) 
    insert into tag values(3,  'oracle',  1) create table Article (id int ,title varchar(20))insert into Article values (1,  'SQL how to') 
    insert into Article values (2,  'reference') create table Tag_Article (tag_id  int,   article_id int)
    insert into Tag_Article values(2,      1) 
    insert into Tag_Article values(3,      2) 
    --id    title        tag_id  tag_name 
    --连接的效率不低,你看我写的咋样,你的 tag_name 是在表Tag_Article吧?
    select a.id ,b.title,c.tag_id,c.article_id
    from tag as a
    inner join Article b on a.id=b.id
    inner join Tag_Article c on b.id=c. article_id 
     /*id          title                tag_id      article_id
    ----------- -------------------- ----------- -----------
    1           SQL how to           2           1
    2           reference            3           2(2 行受影响)
    */
      

  8.   

    --------------------------------- 
    --  Author: Beirut(贝鲁特) 
    --  Comment:小爱 
    --  Date  : 2009-08-04 18:49:08 
    --------------------------------- 
    create table Tag (id int ,name varchar(20),  parent int) 
    insert into tag values(1,  'SQL',    0) 
    insert into tag values(2,  'MS',      1) 
    insert into tag values(3,  'oracle',  1) create table Article (id int ,title varchar(20)) insert into Article values (1,  'SQL how to') 
    insert into Article values (2,  'reference') create table Tag_Article (tag_id  int,  article_id int) 
    insert into Tag_Article values(2,      1) 
    insert into Tag_Article values(3,      2) 
    --id    title        tag_id  tag_name 
    --连接的效率不低,你看我写的咋样,你的 tag_name 是在表Tag_Article吧? 
    select a.id ,b.title,c.tag_id,c.article_id 
    from tag as a 
    inner join Article b on a.id=b.id 
    inner join Tag_Article c on b.id=c. article_id 
    /*id          title                tag_id      article_id 
    ----------- -------------------- ----------- ----------- 
    1          SQL how to          2          1 
    2          reference            3          2 (2 行受影响) 
    */ 
      

  9.   


    -- 还不算太难,我都会
    select a.id ,b.title,c.tag_id,c.article_id 
    from tag a 
    join Article b on a.id=b.id 
    join Tag_Article c on b.id=c. article_id 
      

  10.   

    就inner join 吧,一般来讲这个写法效率算是高的,再就是主外键建立索引就差不多了;