sql – 实现标记的方法 – 各自的优点和缺点

Related

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

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

table posts
+--------+-----------------+ 
| postId | tags            |
+--------+-----------------+
|   1    | c++,search,code |

这里的标签是逗号分隔的。

优点:使用单个选择查询一次检索标记。更新标签很简单。更新简单,便宜。

缺点:对标签检索进行额外解析,很难统计有多少帖子使用哪个标签

(或者,如果仅限于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(*),其中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更昂贵。

解决方法

这些解决方案被称为MysqLicIoUs,scuttle和toxi。

This article比较了每种方法的优缺点。

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...