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

WBOY
Release: 2016-06-06 20:27:43
Original
1365 people have browsed it

目前有三张表
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加上索引

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!