PostgreSQL - query against GIN index of HSTORE value

匿名 (未验证) 提交于 2019-12-03 08:56:10

问题:

I have the following constructor (as a test):

CREATE TABLE product (id BIGSERIAL PRIMARY KEY, ext hstore); CREATE INDEX ix_product_ext ON product USING GIN(ext);  INSERT INTO    product (id, ext) SELECT  id, ('size=>' || CEILING(10 + RANDOM() * 90) || ',mass=>' || CEILING(10 + RANDOM() * 90))::hstore FROM    generate_series(1, 100000) id; 

I have the following query, which works ok:

SELECT  COUNT(id) FROM    (     SELECT  id     FROM    product     WHERE  (ext->'size')::INT >= 41     AND    (ext->'mass')::INT <= 20 ) T 

But I believe the correct way to do this is using the @> operator. I have the following, but it gives a syntax error:

SELECT  COUNT(id) FROM    (     SELECT  id     FROM    product     WHERE  ext @> 'size>=41,mass<=20' ) T 

How should I write this?

回答1:

Reading hstore documentation your (last query) size>=41 does not mean "when size is greater or equal than 41":

text => text    make single-pair hstore 

Following that you can't write mass<=20, because there is no such operation. Using @> operator:

hstore @> hstore    does left operand contain right? 

you can write:

SELECT count(id) FROM product WHERE ext @> 'size=>41,mass=>20'; 

However it takes only these products where size is equal to 41 and mass is equal to 20.



回答2:

Your initial attempt is correct but you need to use (partial) btree indexes and bitmap index scans to rely on it:

create index on product(((ext->'size')::int)) where ((ext->'size') is not null); 

The same for mass, and if the planner doesn't get it on the spot add two where clauses, ie where ext->'size' is not null and the same for mass.

If there is a pattern of some kind (which is likely, since most products with a size also have a mass), potentially create a multicolumn index combining the two - one sac, the other desc.

The gin index as you wrote it, along with the accompanying query (with a syntax error) will basically do the same thing but unordered; it'll be slower.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!