标签的数据库模式(例如,每个帖子都有一些可选的标签) [英] A database schema for Tags (eg. each Post has some optional tags)
问题描述
我有一个像 SO、Wordpress 等的网站,您可以在其中发布帖子,并且您可以为其添加(可选)标签.
I have a site like SO, Wordpress, etc, where you make a post and u can have (optional) tags against it.
处理这个问题的通用数据库模式是什么?我假设它是一个多<->多结构,有三个表.
What is a common database schema to handle this? I'm assuming it's a many<->many structure, with three tables.
有人有什么想法吗?
推荐答案
一个三表多对多的结构应该没问题.
A three table many to many structure should be fine.
例如.帖子、PostsToTags(post_id,tag_id)、标签
关键是索引.确保你的 PostsToTags 表被双向索引(post_id,tag_id
和 tag_id,post_id
),如果读取性能非常关键,你可以引入一个索引视图(这可以给你post_name, tag_name)
The key is indexing. Make sure you PostsToTags table is indexed both ways (post_id,tag_id
and tag_id,post_id
) also if read performance is ultra critical you could introduce an indexed view (which could give you post_name, tag_name)
您当然也需要帖子和标签的索引.
You will of course need indexes on Posts and Tags as well.
这篇关于标签的数据库模式(例如,每个帖子都有一些可选的标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!