实现标签的方法 - 各自的优点和缺点

bob*_*obo 27 sql tags

有关

使用SO作为示例,如果您预计它们会经常更改,那么管理标记的最明智的方法是什么?

方式1:严重非规范化(逗号分隔)

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  |       |       | 

方式2:"稍微规范化"(单独的表,没有交叉点)

table posts
+--------+-------------------+
| postId | title             |
+--------+-------------------+
|   1    | How do u tag?     |

table taggings
+--------+---------+
| postId | tagName |
+--------+---------+
|   1    | C++     |
|   1    | search  |

优点:易于查看标签计数(count(*) from taggings where tagName='C++').

缺点:tagName可能会重复很多次.

方式3:酷孩子(用交叉表标准化)

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更昂贵.

Qua*_*noi 26

这些解决方案被称为mysqlicious,scuttletoxi.

本文比较了每种方法的优缺点.