Databases 10 min read

MySQL vs PostgreSQL: Which Database Wins the Ingestion and Query Battle?

This article presents a detailed performance benchmark comparing MySQL 9.0 and PostgreSQL 17.0, measuring data‑ingestion latency, throughput, saturation, CPU and memory usage, as well as query efficiency, and concludes which open‑source database delivers superior write and read performance.

21CTO
21CTO
21CTO
MySQL vs PostgreSQL: Which Database Wins the Ingestion and Query Battle?
Reading note: Which open‑source database system is stronger? This article compares MySQL vs PostgreSQL.

In this article we compare the performance of MySQL and PostgreSQL relational databases. We will run a series of tests, with the first test focusing on data‑ingestion efficiency.

First we will measure:

Data insertion request latency using the p99 percentile.

Database throughput , i.e., the number of requests and inserts per second.

Database saturation , i.e., load conditions, focusing on CPU usage and memory usage.

Additionally we will measure:

Disk write operations .

Storage efficiency on disk .

The differences between the two databases in these aspects are very large.

Finally we will evaluate the database connection pool to see how they manage connections for data insertion.

In the second test we will measure the efficiency of data retrieval .

Current versions used:

PostgreSQL 17.0

MySQL 9.0

Test Design

For the test we created two tables in each database with the following SQL (illustrated in the images below).

Image
Image

Assume we have an analytics backend that records user actions on a website, such as login, logout, search, and adding items to a cart.

Customer table stores user name and address with a primary key ID.

Event table stores an event ID (foreign key to Customer ID) and the action type.

First Test: Data Insertion

We will insert data using randomly generated customer IDs and action types.

Second Test: Data Retrieval

We will query about 70 million records with a simple JOIN that returns the user's name, address, and action type, after restarting both databases to clear caches.

Code Overview

Image
Image

We use Golang because it is popular and has mature database driver libraries.

Golang is a popular programming language.

It has mature database driver libraries.

To keep the test fair we use the database/sql interface for both databases instead of the pgx driver (which could lower query latency). The focus is on comparing the databases themselves, not driver implementations.

Execute the same SQL queries.

Use the same database connection configuration.

First Test: Data Insertion Execution

We deploy multiple clients, gradually increase load, continuously send requests, and add a virtual client every 60 seconds.

Deploy multiple clients.

Gradually increase load.

Continuously send requests.

Add a virtual client every 60 seconds.

The right‑hand chart shows QPS, the left‑hand chart shows insertion latency measured from the client.

Test Results

From the start, PostgreSQL insertion latency is clearly lower than MySQL.

MySQL insertion time is almost twice that of PostgreSQL.

PostgreSQL uses less CPU, indicating higher throughput capacity.

PostgreSQL also uses less memory.

Key Differences

MySQL requires more disk operations to store the same number of records.

PostgreSQL has higher storage efficiency on disk.

PostgreSQL performs fewer disk operations and occupies less space.

Connection‑Pool Situation

Both databases’ connection pools eventually reach 80 connections.

Each client’s maximum connections are set to 40.

Each database has two clients generating load continuously.

Critical Points

When QPS reaches 5,500, MySQL starts falling behind PostgreSQL; its latency and CPU usage spike.

When QPS reaches 12,000, an extra client is added to PostgreSQL.

PostgreSQL can handle up to 19,000 QPS (4‑core CPU + direct‑attached SSD).

MySQL tops out at about 10,000 QPS, roughly half of PostgreSQL.

Conclusion

In both data‑write and data‑read scenarios, PostgreSQL is more efficient than MySQL.

If you have suggestions for improving the client code or test design, please let us know or submit a Pull Request.

Second Test: Data Retrieval

Before running the second test we added extra records to MySQL so that both databases have comparable data volumes.

Query the Event table.

JOIN on event ID.

Return user name, address, and action type.

Restart both databases before the test to clear caches.

Second Test Results

The latency gap is smaller than in the first test, but PostgreSQL still shows lower latency.

MySQL’s CPU usage is significantly higher than PostgreSQL’s.

When QPS reaches 18,000, MySQL’s performance degrades noticeably.

MySQL’s latency fluctuates, while PostgreSQL remains stable.

At 20,000 QPS MySQL can no longer handle more queries.

PostgreSQL reaches 90 % CPU at 32,000 QPS and then starts to decline.

Reference:

[1] MySQL vs PostgreSQL Performance Benchmark (Latency – Throughput – Saturation): https://www.youtube.com/watch?v=R7jBtnrUmYI

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Connection PoolmysqlPostgreSQLBenchmarkDatabase Performancedata ingestionquery latency
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.