Mysql in 优化

表结构如下,文章只有690篇。

文章表article(id,title,content)

标签表tag(tid,tag_name)

标签文章中间表article_tag(id,tag_id,article_id)

 

其中有个标签的tid是135,查询标签tid是135的文章列表。

690篇文章,用以下的语句查询,奇慢:

select id,title from article where id in(select article_id from article_tag where tag_id=135)

其中这条速度很快:

select article_id from article_tag where tag_id=135

查询结果是五篇文章,id为428,429,430,431,432

用下面sql来查文章也很快:

select id,title from article where id in(428,429,430,431,432)

解决方法:

select id,title from article where id in(select article_id from (select article_id from article_tag where tag_id=135) as tbt)

 

相关文章