简体   繁体   English

用于存储社交媒体帖子的数据库模式示例

[英]Database schema sample for storing social media post

I am building a small social networking website, I have a doubt regarding database schema:我正在构建一个小型社交网站,我对数据库架构有疑问:

How should I store the posts(text) by a user?我应该如何存储用户的帖子(文本)?

I'll have a separate POST table and will link USERS table with it, through USERS_POST table.我将有一个单独的 POST 表,并将通过 USERS_POST 表将 USERS 表与其链接。

But every time to display all the posts on user's profile, system will have to search the entire USERS_POST table for USER id and then display?但是每次要显示用户个人资料上的所有帖子,系统都必须在整个 USERS_POST 表中搜索 USER id 然后显示?

What else should I do?我还应该做什么?

Similarly how should I store the multiple places the user has worked or studied?同样,我应该如何存储用户工作或学习过的多个地方?

I understand it's broad but I am new to Database.我知道它很广泛,但我是数据库的新手。 :) :)

First don't worry too much, start by making it work and see where you get performance problems.首先不要太担心,从让它工作开始,看看你在哪里遇到性能问题。 The database might be a lot quicker then you expect.数据库可能比您预期的要快得多。 Also it is often much easier to see what the best solution is when you have an actual query that is too slow.此外,当您的实际查询太慢时,通常更容易查看最佳解决方案。

Regarding your design, if a post is never linked to more then one user then forget the USERS_POST table and put the user id in the POST table.关于您的设计,如果一个帖子永远不会链接到多个用户,那么忘记 USERS_POST 表并将用户 ID 放在 POST 表中。 In either case an index on the user id would help (as in not having to read the whole table) when the database grows large.在任何一种情况下,当数据库变大时,用户 id 上的索引都会有所帮助(因为不必读取整个表)。

Multiple places for a single user you would store in an additional table.您将存储在附加表中的单个用户的多个位置。 For instance called USERS_PLACES, give it a column user_id to link it to USERS plus other columns for the data you wish to store per place.例如称为 USERS_PLACES,给它一个列 user_id 以将其链接到 USERS 以及您希望每个地方存储的数据的其他列。

BTW In postgresql you might want to keep all object (tables, columns, ...) names lowercase because unless you take care to always quote them like "USERS" postgresql will make them lowercase which can be confusing.顺便说一句,在 postgresql 中,您可能希望将所有对象(表、列、...)名称保留为小写,因为除非您注意总是像“USERS”那样引用它们,否则 postgresql 会使它们变成小写,这可能会令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM