System Design: SQL databases

karanpratapsingh

Karan Pratap Singh

Posted on September 6, 2022

System Design: SQL databases

A SQL (or relational) database is a collection of data items with pre-defined relationships between them. These items are organized as a set of tables with columns and rows. Tables are used to hold information about the objects to be represented in the database. Each column in a table holds a certain kind of data and a field stores the actual value of an attribute. The rows in the table represent a collection of related values of one object or entity.

Each row in a table could be marked with a unique identifier called a primary key, and rows among multiple tables can be made related using foreign keys. This data can be accessed in many different ways without re-organizing the database tables themselves. SQL databases usually follow the ACID consistency model.

Materialized views

A materialized view is a pre-computed data set derived from a query specification and stored for later use. Because the data is pre-computed, querying a materialized view is faster than executing a query against the base table of the view. This performance difference can be significant when a query is run frequently or is sufficiently complex.

It also enables data subsetting and improves the performance of complex queries that run on large data sets which reduces network loads. There are other uses of materialized views, but they are mostly used for performance and replication.

N+1 query problem

The N+1 query problem happens when the data access layer executes N additional SQL statements to fetch the same data that could have been retrieved when executing the primary SQL query. The larger the value of N, the more queries will be executed, the larger the performance impact.

This is commonly seen in GraphQL and ORM (Object-Relational Mapping) tools and can be addressed by optimizing the SQL query or using a dataloader that batches consecutive requests and makes a single data request under the hood.

Advantages

Let's look at some advantages of using relational databases:

  • Simple and accurate
  • Accessibility
  • Data consistency
  • Flexibility

Disadvantages

Below are the disadvantages of relational databases:

  • Expensive to maintain
  • Difficult schema evolution
  • Performance hits (join, denormalization, etc.)
  • Difficult to scale due to poor horizontal scalability

Examples

Here are some commonly used relational databases:


This article is part of my open source System Design Course available on Github.

💖 💪 🙅 🚩
karanpratapsingh
Karan Pratap Singh

Posted on September 6, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

System Design: WhatsApp
distributedsystems System Design: WhatsApp

September 21, 2022

System Design: Netflix
distributedsystems System Design: Netflix

September 22, 2022

System Design: Twitter
distributedsystems System Design: Twitter

September 22, 2022

System Design: Uber
distributedsystems System Design: Uber

September 22, 2022

System Design: URL Shortener
distributedsystems System Design: URL Shortener

September 21, 2022