Sql 找到与给定列表的交集
我有一个文件路径表及其内容的散列,可能有多个文件具有相同的散列Sql 找到与给定列表的交集,sql,postgresql,Sql,Postgresql,我有一个文件路径表及其内容的散列,可能有多个文件具有相同的散列 create table files( path varchar(256) not null, hash varchar(100) not null ); create index files_hash on files (hash); 假设我有3个散列'a'、'b'、'c'的数组,如何有效地找到files表包含的那些散列 我可以使用select distinct hash获取文件中存在的哈希: 但这会有效率吗?比如说有
create table files(
path varchar(256) not null,
hash varchar(100) not null
);
create index files_hash on files (hash);
假设我有3个散列'a'、'b'、'c'的数组,如何有效地找到files表包含的那些散列
我可以使用select distinct hash获取文件中存在的哈希:
但这会有效率吗?比如说有数十万个散列为“a”的文件,PostgreSQL会迭代所有这些记录吗?有没有办法告诉它一旦找到第一个就立即停止?如果您想从一个数组中获取所有哈希值,我建议:
select distinct hash
from files
where hash = any(array['a', 'b', 'c']);
为了提高此查询的性能,您需要在fileshash上建立索引
如果您只希望返回一个,那么这应该更快:
select hash
from files
where hash = any(array['a', 'b', 'c'])
limit 1;
这应该尽可能快: 选择* 从unnest'{a,b,c}'::varchar[]作为arrhash 如果存在,从文件f中选择,其中f.hash=arr.hash; 如果您的表是真空的,那么无论有多少个哈希匹配,您都可以在您的文件上得到一个只有索引的扫描,而哈希索引具有恒定的优异性能。见:
select hash
from files
where hash = any(array['a', 'b', 'c'])
limit 1;