Postgres 性能不会随着核心数量的增加而增加

Postgres performance not increasing with increase in number of core

我正在试用 postgres google-cloud-sql 并加载了一个简单的学校模式

 CREATE TABLE school ( 
    id SERIAL NOT NULL PRIMARY KEY,
    name TEXT NOT NULL
);

CREATE TABLE class ( 
    id SERIAL NOT NULL PRIMARY KEY,
    name TEXT,
    school_id INTEGER NOT NULL REFERENCES school
);

CREATE TABLE student ( 
    id SERIAL NOT NULL PRIMARY KEY,
    name TEXT,
    class_id INTEGER NOT NULL REFERENCES class
);

-- ALL id and foreign keys have indexs

总共加载了约 1500 万行,1500 所学校,每所学校 500 class,每 class200 名学生。

然后创建一个简单的 pgbench 脚本

\setrandom sId1 1 20000000
\setrandom sId2 1 20000000
\setrandom sId3 1 20000000

select count(*) from school s 
join class c on s.id=c.school_id 
join student stu on c.id=stu.class_id where s.id=:sId1;

select count(*) from school s 
join class c on s.id=c.school_id 
join student stu on c.id=stu.class_id where s.id=:sId2;

select count(*) from school s 
join class c on s.id=c.school_id 
join student stu on c.id=stu.class_id where s.id=:sId3;

现在 运行 脚本

pgbench -c 90 -f ./sql.sql  -n -t 1000

2 个核心,7.5 GB,90 个客户端 --

OUTPUT:
number of transactions actually processed: 90000/90000
tps = 1519.690555 (including connections establishing)
tps = 2320.408683 (excluding connections establishing

26 个核心,30 GB,90 个客户端-

number of transactions actually processed: 90000/90000
tps = 1553.721286 (including connections establishing)
tps = 2405.664795 (excluding connections establishing)

问题: 为什么我们从 2 核增加到 26 核只有 80 tps?

因为一个人SELECT只会在一个进程运行上运行一个核。添加额外的核心将允许同时执行多个操作。因此,如果您向数据库抛出(比方说)1,000 个并发查询,它们在 26 个内核而不是 2 个内核上的执行速度会更快。

我在 postgres irc 上问过同样的问题。

社区确定我正在最大化客户端 pgbench,他们建议在 pgbench 中使用 -j4 并且 tps 增加到每秒 23k。