postgresql 分区表 主键索引问题

1 天前
 kanonlee207

pg 分区表如果不设主键,而设置唯一索引,会对性能有什么影响吗, 主要是递增序列 id 字段很多时候都不在分区字段里,这种情况应该怎么处理比较好.

比如有一个用户道具分区表, 有字段 id, user_id, item_id, amount. 通过 user_id 来分区,这种情况怎么建立索引好. 是建立 user_id,item_id 的复合主键,还是唯一索引, id 递增序列该怎么处理. 主要查询是通过 user_id 查列表或某个道具

大神们一般是怎么做的,求指教.第一次用 pg,之前都是 mysql,有些地方不太懂

413 次点击
所在节点    程序员
1 条回复
sagaxu
1 天前
跟 MySQL 不同的是,pgsql 里面,primary key = unique B-tree index + not null ,如果都是 not null ,你用复合主键还是唯一索引,性能上没有区别。

题外话,用 user_id 来分区是糟糕的设计。

All constraints on all children of the parent table are examined during constraint exclusion, so large numbers of children are likely to increase query planning time considerably. So the legacy inheritance based partitioning will work well with up to perhaps a hundred child tables; don't try to use many thousands of children.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1074457

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX