我试图了解以下设计对于 Facebook react 之类的内容是否有意义。我的想法是,每个 react 最终都是一个 user_ids 列表。 例子: 类似 react =[19,24,36,75] 其中 [19,24,36,75] 是人们的用户 ID
CREATE TABLE users (
user_id BIGINT(20) unsigned NOT NULL auto_increment,
user_name varchar(255) NOT NULL UNIQUE,
first_name varchar(255) NOT NULL,
last_name varchar(255) NOT NULL,
phone_number varchar(255) NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(user_id)
) ENGINE = InnoDB;
CREATE TABLE posts (
user_id BIGINT(20) unsigned NOT NULL auto_increment,
post_id BIGINT(20) unsigned NOT NULL auto_increment,
post_text varchar(255) NOT NULL,
reaction_happy BIGINT(20) unsigned NOT NULL auto_increment
reaction_sad BIGINT(20) unsigned NOT NULL auto_increment
reaction_surprised BIGINT(20) unsigned NOT NULL auto_increment
reaction_like BIGINT(20) unsigned NOT NULL auto_increment
PRIMARY KEY (post_id)
)
最佳答案
您需要第三张 table 。从 posts
表中删除 reaction_happy
、reaction_sad
等。然后做这样的事情:
CREATE TABLE reactions (
reaction_id BIGINT(20) unsigned NOT NULL auto_increment,
user_id BIGINT(20) unsigned NOT NULL,
post_id BIGINT(20) unsigned NOT NULL,
type ENUM('happy','sad','surprised','like') NOT NULL,
PRIMARY KEY (reaction_id)
)
此外,为了提高性能,您可能希望在所有 ID 列上添加索引。当您尝试执行某些操作时,您需要将 LEFT JOIN
users
和 reactions
加入到 posts
表中就像以 Facebook 呈现方式呈现主要提要一样。
关于mysql - 如果我想制作一个类似facebook的 "reactions"数据库,这种架构有意义吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57733244/