Database 在数据库中存储标记最有效的方法是什么?

Database 在数据库中存储标记最有效的方法是什么?,database,database-design,tags,tagging,Database,Database Design,Tags,Tagging,我在我的网站上实现了一个类似stackoverflow使用的标签系统,我的问题是-什么是存储标签以便搜索和过滤的最有效的方法 我的想法是: Table: Items Columns: Item_ID, Title, Content Table: Tags Columns: Title, Item_ID 这太慢了吗?有更好的方法吗?根据你在问题中提供的数据,你不能真正谈论缓慢。我认为在开发的这个阶段,你甚至不应该过分担心性能。它叫 但是,我建议您在Tags表中包含Tag_ID列。通常,每个表都

我在我的网站上实现了一个类似stackoverflow使用的标签系统,我的问题是-什么是存储标签以便搜索和过滤的最有效的方法

我的想法是:

Table: Items
Columns: Item_ID, Title, Content

Table: Tags
Columns: Title, Item_ID

这太慢了吗?有更好的方法吗?

根据你在问题中提供的数据,你不能真正谈论缓慢。我认为在开发的这个阶段,你甚至不应该过分担心性能。它叫


但是,我建议您在Tags表中包含Tag_ID列。通常,每个表都有一个ID列是一个好的做法。

我建议使用中间第三个表来存储标记站点关联,因为标记和项目之间存在多对多关系,即一个项目可以与多个标记关联,一个标记可以与多个项目关联。 嗯,,
阀门。

如果空间将成为一个问题,请使用第三个表标签(标签Id,标题)来存储标签的文本,然后将标签表更改为(标签Id,项目Id)。这两个值也应该提供一个唯一的复合主键。

一个项将有许多标记。一个标签将属于多个项目。这对我意味着您很可能需要一个中间表来克服多对多障碍

比如:

表:项目
列:项目ID、项目标题、内容

表:标签
列:标记ID、标记标题

表:项目标签
列:项目ID、标记ID

可能是因为你的web应用非常流行,需要一路去规范化,但过早地搅乱局面是毫无意义的。

项目应该有一个“ID”字段,标签应该有一个“ID”字段(主键,集群)


然后制作一个ItemID/TagID的中间表,并将“”放在那里。

事实上,我认为根据比例,将tags表反规范化可能是一种更好的方法

这样,tags表只包含tagid、itemid和tagname

您将获得重复的标记名,但它使添加/删除/编辑特定项目的标记变得更加简单。您不必创建新标记、删除旧标记的分配并重新分配新标记,只需编辑标记名即可


为了显示标记列表,您只需使用DISTINCT或GROUP BY,当然您也可以轻松地计算标记的使用次数。

如果您不介意使用一些非标准内容,Postgres 9.4版及更高版本可以选择存储JSON text array类型的记录

您的模式将是:

Table: Items
Columns: Item_ID:int, Title:text, Content:text

Table: Tags
Columns: Item_ID:int, Tag_Title:text[]
有关更多信息,请参阅Josh Berkus的这篇优秀文章:


在性能方面,有更多不同的选项可供全面比较,上面建议的选项总体上是最好的。

标记模式:标记表和属性:

表:

tags (each row only keeps information about a particular tag)
taggings (each row keeps information about trigger and who will receive the trigger )
activitietis_tags (each row keeps information about tag with particular activity)
tag_status (each row keeps track of a tag status)
表:标记表的标记属性:

id(PK)
userId(FK users)(not null)(A tag only belongs to one user, but  a user can create multiple tags. So it is one to many relationships.)
genreId(FK activity_geners)(not null)
name (string) (not null)
description (string)
status (int) (0=inactive, 1=pending, 2=active, there could be more flag)
rank(int)  (rank is the popularity of a particular tag), this field can be use for sorting among similar tags.)
type (int) (0=tag, 1=challenge, 2=mission)
photo(string)
visibility (int) (0=public, 2=protected, 3 = private)(private means the tag only  visible to assigned users of a activity, protected means a tag only visible to all friends and followers of the creator of the tag, public means search by public, such as all admin created tag)
createdAt(timestamp for the tag was created at)
updatedAt (timestamp for the tag last time updated)
deletedAt (default value null) (timestamp when tag was deleted, we need this field because we will delete tag permanently from audit table). 
注意:保留10号字段将在以后派上用场

表:标签:

此表将用于触发,例如广播其他用户的提要或向他们发送通知。在此表中插入一行后,将有一个服务读取一行,并采取相关操作删除该行

标记表的属性:

Id(PK)
tagId(a tagging row only belongs to a tag, but a tag can have multiple row).
taggableId (id of a user who will receive notification)
taggableType(int) (0=notification, 1=feed message)
taggerId(the person who triggered the broadcast)
taggerType(ad, activity, news)
createdAt(timestamp for the tag was created at)
Id (PK)
activityId(FK)
tagId(FK)
表:Activities_标签

从用户的角度来看,用户能够在实例化一个活动后创建一个标记,所以下表将保留关于哪些活动具有哪些标记的信息

标记表属性的属性:

Id(PK)
tagId(a tagging row only belongs to a tag, but a tag can have multiple row).
taggableId (id of a user who will receive notification)
taggableType(int) (0=notification, 1=feed message)
taggerId(the person who triggered the broadcast)
taggerType(ad, activity, news)
createdAt(timestamp for the tag was created at)
Id (PK)
activityId(FK)
tagId(FK)
表:标记状态

当用户创建一个标签时,将在此表中创建一行tagId和默认状态为inactive/pending,管理员将从状态为pending/inactive的标签表中提取所有标签,查看标签后,如果管理员批准标签,则标签表中的状态值将被批准,标签状态行将被删除。如果admin被拒绝,则tag_status table的status字段的值将被拒绝,触发器将被广播,接收器将向该标签的关联用户发送一个通知,并发送一条消息,说明其标签被拒绝

id(PK)
senderId(Id of the user)
receiverId(Id of admin user)
createdAt(timestamp of created at)
updatedAt(timestamp of updated at)
deletedAt(timestamp of deletedAt) default value null
expiredAt (if a tag never gets approved it will expire after a certain time for removing its information from the database. If a rejected tag gets updated by user then expiredAt will reset to new future time)
status 
Message (string varchar(256)) (message for user)

以前的问题:从2016年开始,使用Solr或ElasticsearchRelated:如果有类似tagGroup的东西,如何处理它,例如,标记被分为几个类别,例如:编程语言:c#、vb、pearl。操作系统:windows7、dos、linuxetc@Thunder:假设一个标记可能只属于一个类别,我将创建一个由category_id和category_name组成的TagCategory表。从那里,我将向Tags表附加一个category_id字段,并对其执行连接。