MODULO SQL & Database Design Master SeriesLAB

Designing a Scalable Social Media Database

Designing a Scalable Social Media Database

=====================================================

In this masterclass, we will delve into the world of database design for social media platforms. We'll explore the key components, data models, and relationships required to build a scalable and efficient database.

Requirements Gathering


Before designing the database, let's identify the key features of a social media platform:

  • Users can create accounts and profiles
  • Users can post updates (text, images, videos)
  • Users can follow other users
  • Users can like and comment on posts
  • Users can share posts
  • Users can send direct messages

Entity-Relationship Modeling


Based on the requirements, we can identify the following entities:

  • Users: represents a user's account and profile information
  • Posts: represents a user's post (text, image, video)
  • Followers: represents a user's followers
  • Likes: represents a user's likes on a post
  • Comments: represents a user's comments on a post
  • Shares: represents a user's shares of a post
  • Messages: represents a user's direct messages

We can now create an entity-relationship diagram (ERD) to visualize the relationships between these entities:

ERD

  • A user can have multiple posts (one-to-many)
  • A post belongs to one user (many-to-one)
  • A user can follow multiple users (many-to-many)
  • A user can like multiple posts (many-to-many)
  • A post can have multiple likes (many-to-many)
  • A user can comment on multiple posts (many-to-many)
  • A post can have multiple comments (many-to-many)
  • A user can share multiple posts (many-to-many)
  • A post can be shared by multiple users (many-to-many)
  • A user can send multiple messages (one-to-many)
  • A message belongs to one user (many-to-one)

Database Schema Design


Based on the ERD, we can create the following database schema:

Users Table

| Column Name | Data Type | Description | | --- | --- | --- | | id | int | unique identifier for the user | | username | varchar(255) | username chosen by the user | | email | varchar(255) | email address of the user | | password | varchar(255) | password for the user's account | | profile_picture | varchar(255) | URL of the user's profile picture | | bio | text | user's bio |

Posts Table

| Column Name | Data Type | Description | | --- | --- | --- | | id | int | unique identifier for the post | | user_id | int | foreign key referencing the Users table | | content | text | text content of the post | | image_url | varchar(255) | URL of the post's image | | video_url | varchar(255) | URL of the post's video | | created_at | timestamp | timestamp when the post was created |

Followers Table

| Column Name | Data Type | Description | | --- | --- | --- | | id | int | unique identifier for the follower | | user_id | int | foreign key referencing the Users table | | follower_id | int | foreign key referencing the Users table |

Likes Table

| Column Name | Data Type | Description | | --- | --- | --- | | id | int | unique identifier for the like | | user_id | int | foreign key referencing the Users table | | post_id | int | foreign key referencing the Posts table |

Comments Table

| Column Name | Data Type | Description | | --- | --- | --- | | id | int | unique identifier for the comment | | user_id | int | foreign key referencing the Users table | | post_id | int | foreign key referencing the Posts table | | content | text | text content of the comment |

Shares Table

| Column Name | Data Type | Description | | --- | --- | --- | | id | int | unique identifier for the share | | user_id | int | foreign key referencing the Users table | | post_id | int | foreign key referencing the Posts table |

Messages Table

| Column Name | Data Type | Description | | --- | --- | --- | | id | int | unique identifier for the message | | sender_id | int | foreign key referencing the Users table | | recipient_id | int | foreign key referencing the Users table | | content | text | text content of the message |

SQL Queries


Here are some example SQL queries to demonstrate the usage of the database schema:

Create a new user

INSERT INTO Users (username, email, password, profile_picture, bio)
VALUES ('john_doe', 'john.doe@example.com', 'password123', 'https://example.com/john_doe.jpg', 'Hello, I''m John!');

Create a new post

INSERT INTO Posts (user_id, content, image_url, video_url)
VALUES (1, 'Hello, world!', 'https://example.com/hello_world.jpg', NULL);

Get a user's followers

SELECT u.* FROM Users u
JOIN Followers f ON u.id = f.follower_id
WHERE f.user_id = 1;

Get a post's likes

SELECT u.* FROM Users u
JOIN Likes l ON u.id = l.user_id
WHERE l.post_id = 1;

Get a post's comments

SELECT c.* FROM Comments c
WHERE c.post_id = 1;

Indexing and Optimization


To improve the performance of the database, we can add indexes to the columns used in the WHERE and JOIN clauses:

CREATE INDEX idx_users_username ON Users (username);
CREATE INDEX idx_posts_user_id ON Posts (user_id);
CREATE INDEX idx_followers_user_id ON Followers (user_id);
CREATE INDEX idx_likes_post_id ON Likes (post_id);
CREATE INDEX idx_comments_post_id ON Comments (post_id);

We can also optimize the database by partitioning the tables based on the user_id or post_id columns:

CREATE TABLE Posts_partitioned (
  id INT,
  user_id INT,
  content TEXT,
  image_url VARCHAR(255),
  video_url VARCHAR(255)
) PARTITION BY RANGE (user_id) (
  PARTITION p_1 VALUES LESS THAN (1000),
  PARTITION p_2 VALUES LESS THAN (2000),
  PARTITION p_3 VALUES LESS THAN MAXVALUE
);

⚔️ YOUR MISSION

Design a database schema for a social media platform that allows users to create groups and invite other users to join. The platform should also allow users to post updates to the group.

SQL practice editor
Read the exercise prompt
CREATE TABLE Groups (
  id INT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  description TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE Group_Members (
  id INT PRIMARY KEY,
  group_id INT NOT NULL,
  user_id INT NOT NULL,
  role VARCHAR(255) NOT NULL DEFAULT 'member',
  joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (group_id) REFERENCES Groups (id),
  FOREIGN KEY (user_id) REFERENCES Users (id)
);

CREATE TABLE Group_Posts (
  id INT PRIMARY KEY,
  group_id INT NOT NULL,
  user_id INT NOT NULL,
  content TEXT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (group_id) REFERENCES Groups (id),
  FOREIGN KEY (user_id) REFERENCES Users (id)
);
WAITING FOR SIGNAL...
BACKNEXT STAGE