我们在 PostgreSQL 的单个事务中执行大约 100k DDL 语句。在执行过程中,各个 Postgres 连接的内存使用量逐渐增加,一旦它无法获得更多内存(在 3GB 内存上从 10MB 使用量增加到 2.2GB),OOM 杀手用 9 命中它,导致 Postgres 进入恢复模式.
BEGIN;
CREATE SCHEMA schema_1;
-- create table stmts - 714
-- alter table add pkey stmts - 714
-- alter table add constraint fkey stmts - 34
-- alter table add unique constraint stmts - 2
-- alter table alter column set default stmts - 9161
-- alter table alter column set not null stmts - 2405
-- alter table add check …Run Code Online (Sandbox Code Playgroud) postgresql performance transaction ddl postgresql-9.6 postgresql-performance
我有一个具有以下结构的部门表
创建表
CREATE TABLE `department` (
`id` binary(16) NOT NULL,
`name` varchar(255) NOT NULL,
`type` int(11) NOT NULL,
`status` tinyint(4) NOT NULL, -- Possible values are 0,1,2 only
PRIMARY KEY (`id`),
UNIQUE KEY `UK_2xsp2nild3xbgkg4pln7cviib` (`status`,`type`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Run Code Online (Sandbox Code Playgroud)
以下查询不使用status索引,因为它是唯一约束中最左边的列。
EXPLAIN SELECT * FROM department d WHERE d.status = 1;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
------ ----------- ------ ---------- ------ ---------------------------- ------ ------- ------ ------ -------- -------------
1 SIMPLE d (NULL) …Run Code Online (Sandbox Code Playgroud)