我在 pg major 11 中有以下场景:
DROP TABLE IF EXISTS public.agent_sessions_partitioned;
CREATE TABLE public.agent_sessions_partitioned
(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
account_id uuid,
user_id uuid
) PARTITION BY LIST (account_id);
CREATE TABLE "agent_sessions_bcbc5acc-f020-4073-bdf4-3098bc043e8b"
PARTITION OF agent_sessions_partitioned
FOR VALUES IN ('bcbc5acc-f020-4073-bdf4-3098bc043e8b');
INSERT INTO agent_sessions_partitioned (id, account_id, user_id)
SELECT agent_sessions.id, account_id, user_id FROM agent_sessions;
ALTER TABLE "agent_sessions_bcbc5acc-f020-4073-bdf4-3098bc043e8b" ADD PRIMARY KEY (id);
等等。
当我有任何这样的查询时,这非常有效:
Select * from agent_sessions_partitioned where account_id = 'XX'
但是因为我使用的是 ORM(Rails - 活动记录),所以我没有选择始终在语句中使用 account_id 以及每当我需要执行以下操作时:
UPDATE agent_sessions_partitioned set user_id = 'x' where id = 'y'
PG 扫描所有子表试图找到这个元组,见下面的解释分析:
"Append (cost=0.28..2612.12 rows=355 width=558) (actual time=0.956..277.658 rows=1
loops=1)"
" -> Index Scan using "agent_sessions_a13f3c88-3022-4676-bd48-6580d8877ae2_pkey" on
"agent_sessions_a13f3c88-3022-4676-bd48-6580d8877ae2" (cost=0.28..8.30 rows=1 width=500)
(actual time=0.955..0.956 rows=1 loops=1)"
" Index Cond: (id = 'b21a0178-f97c-4598-ba39-bf763ba377b5'::uuid)"
" -> Index Scan using "agent_sessions_325774d6-e5e7-4fae-9659-8b76349a6c2a_pkey" on
"agent_sessions_325774d6-e5e7-4fae-9659-8b76349a6c2a" (cost=0.29..8.30 rows=1 width=481)
(actual time=0.750..0.750 rows=0 loops=1)"
" Index Cond: (id = 'b21a0178-f97c-4598-ba39-bf763ba377b5'::uuid)"
" -> Index Scan using "agent_sessions_1f781bcd-b941-4915-949a-9af893d8f066_pkey" on
"agent_sessions_1f781bcd-b941-4915-949a-9af893d8f066" (cost=0.29..8.30 rows=1 width=507)
(actual time=1.523..1.523 rows=0 loops=1)"
由于我没有选择更改此通过 id 更新记录的查询的选项,我可以在 postgres 端做些什么吗?任何可能对我有帮助的配置或其他类型的分区,甚至升级到 pg 12/13 的版本?