繁体   English   中英

如何使用gin索引查询postgres数组?

[英]How does one query a postgres array using the gin index?

我有一个带有杜松子酒索引的postgres数组列:

CREATE TABLE things (
    id integer NOT NULL,
    tags character varying(255)[]
);

CREATE INDEX index_things_on_tags ON things USING gin (tags);

有几种方法可以使用各种数组运算符检查列中是否存在元素。 以下是我见过的:

  1. select * from things where 'blue' = ANY (tags)
  2. select * from things where tags <@ '{"blue"}'
  3. select * from things where '{"blue","yellow"}' && tags;

在postgres 9.3中:

  • 第一个会使用杜松子酒指数吗?
  • 我很确定第二个会使用索引。 但是,它与第一个不同。 它不允许我检查蓝色是否是标签之一,它需要我指定确切的数组。 有没有办法让2中的语法风格实现1实现的目标?
  • 在第三个,我想要任何有蓝色或黄色的行。 这个查询会使用杜松子酒索引吗? 如果没有,我如何使用索引进行此查询?

为什么不试试看?

regress=> SET enable_seqscan  = off;
SET

regress=> explain select * from things where 'blue' = ANY (tags);
                                QUERY PLAN                                 
---------------------------------------------------------------------------
 Seq Scan on things  (cost=10000000000.00..10000000037.67 rows=6 width=36)
   Filter: ('blue'::text = ANY ((tags)::text[]))
(2 rows)

regress=> explain select * from things where tags <@ '{"blue"}';
                                     QUERY PLAN                                     
------------------------------------------------------------------------------------
 Bitmap Heap Scan on things  (cost=12.05..21.52 rows=6 width=36)
   Recheck Cond: (tags <@ '{blue}'::character varying[])
   ->  Bitmap Index Scan on index_things_on_tags  (cost=0.00..12.05 rows=6 width=0)
         Index Cond: (tags <@ '{blue}'::character varying[])
(4 rows)

regress=> explain select * from things where '{"blue","yellow"}' && tags;
                                     QUERY PLAN                                      
-------------------------------------------------------------------------------------
 Bitmap Heap Scan on things  (cost=12.10..22.78 rows=12 width=36)
   Recheck Cond: ('{blue,yellow}'::character varying[] && tags)
   ->  Bitmap Index Scan on index_things_on_tags  (cost=0.00..12.09 rows=12 width=0)
         Index Cond: ('{blue,yellow}'::character varying[] && tags)
(4 rows)

所以Pg使用&&<@查询的索引,但不是= ANY (...)

我确信有可能教Pg将x = ANY (y)变换为ARRAY[x] @> y ,但目前还没有。

2你做的正是你想要的。 测试“蓝色”是否是标签之一。 这不是一个平等测试,它是一个成员资格测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM