I have a table
create table device_entity
(
id uuid not null primary key,
name varchar(255),
metadata hstore
);
This table has entries with 'country' = 'uk' in the metadata field.
This query returns all entries where the key 'country' is present in the metadata field.
SELECT * from device_entity where metadata ? 'country'
How can I select all entries where the key 'country' = 'uk'?
asked Sep 17, 2022 at 9:20
1 Answer 1
SELECT * from device_entity where (metadata -> 'country') = 'uk';
This query will probably have quite slow performance which can probably be improved significantly by deploying the right inverted index.
answered Sep 17, 2022 at 10:31
-
No need for an "inverted index". A B-Tree index on the expression should work just fineuser1822– user18222022年09月17日 11:19:59 +00:00Commented Sep 17, 2022 at 11:19
-
Of course, a dedicated column "country" in the table would be best for ease of use, index, and performance.Erwin Brandstetter– Erwin Brandstetter2022年09月17日 11:45:47 +00:00Commented Sep 17, 2022 at 11:45
-
b-tree would work if this is the only interesting keyJasen– Jasen2022年09月17日 11:59:53 +00:00Commented Sep 17, 2022 at 11:59
lang-sql
->
operator - but nowadays I would prefer a jsonb column over hstore