Heim  >  Artikel  >  Backend-Entwicklung  >  关于数据库多表查询的问题?

关于数据库多表查询的问题?

WBOY
WBOYOriginal
2016-06-06 20:27:431366Durchsuche

目前有三张表
topic话题表 id title
tagsmap对应关系表 id tagsid topicid
tags标签表 id tagsname

现在需要查询某个标签下的前几条话题记录。

select title from topic where id in (select topicid from tagsmap where tagsid="10000")

这种查询发现查询比较慢。

这种写法有什么问题?请问如何优化查询比较好!希望能讨论。

回复内容:

目前有三张表
topic话题表 id title
tagsmap对应关系表 id tagsid topicid
tags标签表 id tagsname

现在需要查询某个标签下的前几条话题记录。

select title from topic where id in (select topicid from tagsmap where tagsid="10000")

这种查询发现查询比较慢。

这种写法有什么问题?请问如何优化查询比较好!希望能讨论。

只是一般程度的优化的话:
select title from topic t left join tagsmap m on m.topicid = t.id where m.tagsid = "10000"
数据量在几万到几十万基本上这个性能都可以接受,当然记得tagsmap里面的tagsid加上索引

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn