今天測試一下 1 億條數據,MySQL 和 PostgreSQL 的性能表現。說明下,只是做一些基本的測試,并沒有用一些數據庫 Benchmark 工具進行測試。
準備
建表語句:
CREATE TABLE user_mysql / user_postgresql (
id SERIAL PRIMARY KEY,
username VARCHAR(50),
emAIl VARCHAR(100),
password VARCHAR(100),
first_name VARCHAR(50),
last_name VARCHAR(50),
address VARCHAR(200),
city VARCHAR(50),
state VARCHAR(50),
zip_code VARCHAR(10),
country VARCHAR(50),
phone_number VARCHAR(50),
date_of_birth DATE,
gender VARCHAR(10),
occupation VARCHAR(100),
education_level VARCHAR(50),
registration_date TIMESTAMP,
last_login TIMESTAMP,
is_active BOOLEAN,
is_admin BOOLEAN,
additional_field1 VARCHAR(100),
additional_field2 VARCHAR(100)
);
接下來記錄一下相關數據。
1.插入耗時
- MySQL:≈ 67分鐘
- PostgreSQL:≈ 55分鐘
2.count(*)耗時
MySQL:45 s 877 ms,明細如下:
mydatabase> select count(*) from user_mysql
[2023-09-26 22:22:24] 1 row retrieved starting from 1 in 45 s 877 ms (execution: 45 s 767 ms, fetching: 110 ms)
PostgreSQL:8 s 169 ms,明細如下:
postgres.public> select count(*) from user_postgresql
[2023-09-26 22:24:08] 1 row retrieved starting from 1 in 8 s 169 ms (execution: 8 s 133 ms, fetching: 36 ms)
1億數據量
3.根據主鍵查詢數據
MySQL:47 ms,明細如下:
mydatabase> select * from user_mysql where id = 19279833
[2023-09-26 22:28:10] 1 row retrieved starting from 1 in 47 ms (execution: 16 ms, fetching: 31 ms)
PostgreSQL:46 ms,明細如下:
postgres.public> select * from user_postgresql where id = 19279833
[2023-09-26 22:29:51] 1 row retrieved starting from 1 in 46 ms (execution: 15 ms, fetching: 31 ms)
4.根據username查詢(無索引)
MySQL:1 m 56 s 986 ms,明細如下:
// 查詢第99279833行數據
mydatabase> select * from user_mysql where username = '10190439674'
[2023-09-26 22:36:09] 1 row retrieved starting from 1 in 1 m 56 s 986 ms (execution: 1 m 56 s 939 ms, fetching: 47 ms)
PostgreSQL:38 s 73 ms,明細如下:
// 同樣查詢第99279833行數據
postgres.public> select * from user_postgresql where username = '14998727834'
[2023-09-26 22:38:25] 1 row retrieved starting from 1 in 38 s 73 ms (execution: 38 s 18 ms, fetching: 55 ms)
5.創建索引耗時
MySQL創建B+TREE索引:5 m 31 s 276 ms,明細如下:
mydatabase> ALTER TABLE user_mysql ADD INDEX idx_name (username)
[2023-09-26 22:47:37] completed in 5 m 31 s 276 ms
PostgreSQL創建B-TREE索引:9 m 20 s 847 ms,明細如下:
postgres.public> CREATE INDEX idx_name ON user_postgresql (username)
[2023-09-26 22:57:59] completed in 9 m 20 s 847 ms
6.根據username查詢(有索引)
MySQL:93 ms,明細如下:
// 查詢第99279833行數據
mydatabase> select * from user_mysql where username = '10190439674'
[2023-09-26 23:01:48] 1 row retrieved starting from 1 in 93 ms (execution: 0 ms, fetching: 93 ms)
PostgreSQL:63 ms,明細如下:
// 同樣查詢第99279833行數據
postgres.public> select * from user_postgresql where username = '14998727834'
[2023-09-26 23:00:07] 1 row retrieved starting from 1 in 63 ms (execution: 0 ms, fetching: 63 ms)
7.根據username修改(有索引)
MySQL:16 ms,明細如下:
mydatabase> update user_mysql set email='myemail' where username = '10190439674'
[2023-09-26 23:06:05] 1 row affected in 16 ms
PostgreSQL:15 ms,明細如下:
postgres.public> update user_postgresql set email='myemail' where username = '14998727834'
[2023-09-26 23:07:13] 1 row affected in 15 ms
8.分頁查詢(不加條件)
MySQL:1 m 40 s 265 ms,明細如下:
mydatabase> select * from user_mysql limit 89999980, 20
[2023-09-26 23:10:54] 20 rows retrieved starting from 1 in 1 m 40 s 265 ms (execution: 1 m 40 s 234 ms, fetching: 31 ms)
PostgreSQL:27 s 750 ms,明細如下:
postgres.public> select * from user_postgresql limit 20 offset 89999980
[2023-09-26 23:12:32] 20 rows retrieved starting from 1 in 27 s 750 ms (execution: 27 s 688 ms, fetching: 62 ms)
9.分頁查詢(加條件,條件為索引)
MySQL:94 ms,明細如下:
mydatabase> select * from user_mysql where id >= 89999980 limit 20
[2023-09-26 23:13:34] 20 rows retrieved starting from 1 in 94 ms (execution: 0 ms, fetching: 94 ms)
PostgreSQL:78 ms,明細如下:
postgres.public> select * from user_postgresql where id >= 89999980 limit 20
[2023-09-26 23:14:12] 20 rows retrieved starting from 1 in 78 ms (execution: 0 ms, fetching: 78 ms)
總結
在數據量達到1億時,數據庫操作的開銷都會比較大,尤其是不走索引的操作和DDL操作等。因此在生產環境時,不建議數據量太大,數據庫特別大的情況下,建議使用更強大的數據庫,不建議分表分庫。對大表進行DDL操作時也需要謹慎操作。
聲明:這些數據均為本機測試,并未用專業測試軟件測試,僅供參考。