Facebook“喜欢”数据结构 [英] Facebook "like" data structure

查看:125
本文介绍了Facebook“喜欢”数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想,facebook如何管理所有可以喜欢的所有不同的东西的数据库设计。如果只有一件事要喜欢,这很简单,只是一个你喜欢的外键,还有一个外键,你是谁。

I've been wondering how facebook manages the database design for all the different things that you can "like". If there is only one thing to like, this is simple, just a foreign key to what you like and a foreign key to who you are.

但是必须有数百个不同的表,你可以喜欢在Facebook上。他们如何存储这些喜好?

But there must be hundreds of different tables that you can "like" on facebook. How do they store the likes?

推荐答案

如果要在关系数据库中表示此类结构,则需要使用通常称为表继承的层次结构。在表继承中,您有一个表格定义了一个父类型类型,然后是表示其主键也是外键返回到父表单。

If you want to represent this sort of structure in a relational database, then you need to use a hierarchy normally referred to as table inheritance. In table inheritance, you have a single table that defines a parent type, then child tables whose primary keys are also foreign keys back to the parent.

使用Facebook示例,您可能会有以下情况:

Using the Facebook example, you might have something like this:

User
------------
UserId (PK)

Item
-------------
ItemId (PK)
ItemType (discriminator column)
OwnerId (FK to User)

Status
------------
ItemId (PK, FK to Item)
StatusText 

RelationshipUpdate
------------------
ItemId (PK, FK to Item)
RelationshipStatus
RelationTo (FK to User)

Like
------------
OwnerId (FK to User)
ItemId (FK to Item)
Compound PK of OwnerId, ItemId

在兴趣完整性方面,值得注意的是,Facebook不会使用RDBMS进行此类事情。他们为这种存储选择了NoSQL解决方案。然而,这是在RDBMS中存储这种松散耦合的信息的一种方式。

In the interest completeness, it's worth noting that Facebook doesn't use an RDBMS for this sort of thing. They have opted for a NoSQL solution for this sort of storage. However, this is one way of storing such loosely-coupled information within an RDBMS.

这篇关于Facebook“喜欢”数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆