具有唯一约束的 Postgres 哈希索引
问题描述:
随着 Postgres 10 正确支持哈希索引,我想使用哈希索引进行 id 查找(与 btree 相比,哈希索引的大小更小,理论上更快).
With Postgres 10 properly supporting hash index, I would like to use hash index for id lookup (hash index is smaller in size compared to btree and theoretically faster).
我有一张桌子
create table t (id int);
create unique index on t using hash (id);
但我得到了以下信息:
错误:访问方法hash"不支持唯一索引
为什么哈希索引不允许唯一约束?有没有办法绕过这个?
Why does hash index not allow unique constraint? Are there ways to circumvent this?
答
文档不容置疑:
目前,只有 B-tree 索引可以声明为唯一.
Currently, only B-tree indexes can be declared unique.
有一个关于黑客名单的讨论 最近关于这个,并得出结论,添加允许 UNIQUE
哈希索引的功能并不简单.
There was a discussion on the hackers list about this recently, and it was concluded that it wouldn't be simple to add the capability to allow UNIQUE
hash indexes.