Best DB (MySQL) structure: Articles which contain favored tags

自闭症网瘾萝莉.ら 提交于 2019-11-30 10:27:58

The following is by no means exhaustive/definitive, but it should get you going in the right direction.

Tables:

news
=====
id
title
text

tag
===
id
tag

tag_map
=======
tag_id
news_id

favorite_tags
=============
user_id
tag_id

Query

SELECT * 
FROM favorite_tags
JOIN tag_map ON favorite_tags.tag_id = tag_map.tag_id
JOIN news ON tag_map.news_id = news.id
WHERE favorite_tags.user_id = $userid

The query's performance (whether in your sub-select approach, or Frank Farmer's more elegant join-based one) will chiefly depend on indices. Just remember that MySQL uses just one index per table, and the proper set of indices (depending on the query you want to optimize) invariably becomes pretty obvious...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!