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

查看:27
本文介绍了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天全站免登陆