Index in hstore with no predictable key

这一生的挚爱 提交于 2019-12-12 20:34:48

问题


Lets say I have a blog database where posts table stores tags in hstore.
Keys represent tag ids and values are tag names.

ex : 1=>'Test', 56=>'SQL', 42=>'Java'

I want to have optimized selects on posts with tag filter. How and what type of select should I create on tags column in order to optimize this query

SELECT * FROM posts WHERE tags?'4'

The issue is that I don't have predictable key names on order to create index based on their value.

Would it be a good solution to create gist index on column?
And how I can make it to treat key as an integer?


回答1:


A GIN index is probably the better choice:

CREATE INDEX posts_tags_idx ON posts USING gin (tags);

That works for all keys.
You could further optimize the index if queries are only for a specific key with a partial index:

CREATE INDEX posts_tags_idx ON posts USING gin (tags -> '4')) WHERE x ? '4';

hstore ? text ... does hstore contain key?

But that doesn't seem to be an option for you.

hstore stores text only, not integer. Per documentation:

Keys and values are simply text strings.



来源:https://stackoverflow.com/questions/22183372/index-in-hstore-with-no-predictable-key

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