使用SO作为示例,如果您预计它们会经常更改,那么管理标记的最明智的方法是什么?
table posts +--------+-----------------+ | postId | tags | +--------+-----------------+ | 1 | c++,search,code |
这里的标签是逗号分隔的.
优点:使用单个select查询一次检索标记.更新标签很简单. 更新简单,便宜.
缺点:对标签检索进行额外解析,很难统计有多少帖子使用哪个标签.
(或者,如果仅限于5个标签)
table posts +--------+-------+-------+-------+-------+-------+ | postId | tag_1 | tag_2 | tag_3 | tag_4 | tag_5 | +--------+-------+-------+-------+-------+-------+ | 1 | c++ |search | code | | |
table posts +--------+-------------------+ | postId | title | +--------+-------------------+ | 1 | How do u tag? | table taggings +--------+---------+ | postId | tagName | +--------+---------+ | 1 | C++ | | 1 | search |
优点:易于查看标签计数(count(*) from taggings where tagName='C++').
缺点:tagName可能会重复很多次.
table posts +--------+---------------------------------------+ | postId | title | +--------+---------------------------------------+ | 1 | Why is a raven like a writing desk? | table tags +--------+---------+ | tagId | tagName | +--------+---------+ | 1 | C++ | | 2 | search | | 3 | foofle | table taggings +--------+---------+ | postId | tagId | +--------+---------+ | 1 | 1 | | 1 | 2 | | 1 | 3 |
优点:
缺点:改变标签比方式#1更昂贵.
| 归档时间: |
|
| 查看次数: |
7913 次 |
| 最近记录: |