Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 找到与给定列表的交集_Sql_Postgresql - Fatal编程技术网

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;