随着Postgres 10对哈希索引的支持,我想使用哈希索引进行ID查找(哈希索引与btree相比尺寸更小,理论上更快)。
我有一个表:
create table t (id int);
create unique index on t using hash (id);
但是我得到了以下错误:
ERROR:访问方法“hash”不支持唯一索引。
为什么哈希索引不允许唯一约束? 有没有绕过这个问题的方法?
随着Postgres 10对哈希索引的支持,我想使用哈希索引进行ID查找(哈希索引与btree相比尺寸更小,理论上更快)。
我有一个表:
create table t (id int);
create unique index on t using hash (id);
create table t (id int);
alter table t add constraint c exclude using hash (id with =);
为什么不直接为这个列添加一个约束呢?
create table t (id int);
create index on t using hash (id);
-- simply create a new constraint for this column, do not mix it with hash index
alter table t add constraint unique_id unique (id);
非常老的想法
在链接上创建唯一索引(将md5(url)转换为uuid);
我现在正在尝试
更新。它很慢,就像在每次插入时重新计算每个总和。
更新2。添加功能唯一索引不会加速列上的搜索。
要查找相关字符串,应该使用相同的转换进行搜索或添加哈希索引。
ON CONFLICT
子句中命名约束,而不是让 Postgres 从列中推断它(这仅适用于唯一索引,而不适用于排除约束)。在我的答案示例中,它将是ON CONFLICT (c) DO ...
。请参阅文档:https://www.postgresql.org/docs/current/sql-insert.html#SQL-ON-CONFLICT - jbgON CONFLICT ON CONSTRAINT cnp_profiles_id_excl DO UPDATE
导致WrongObjectTypeError: ON CONFLICT DO UPDATE not supported with exclusion constraints
。我无法在这种类型的upserts中使用哈希索引。 - Lukasz Madon