json - Alternate to Like in jsonb postgres -


i have postgres table more 25m records, there have jsonb column called info.

sample json format:

{ "username1":{"name":"somename","age":22,"gender":"male"}, "fathername":{"name":"somename","age":22,"gender":"male"} } 

i going find number of records match 'user%' key value.

here query

select count(1) tablename info::text '%user%'; 

this query working , getting result it, taking long time execute query in 25m records.

is there way can optimize query or alternate method achieve it? please help.

thanks in advance!

as @klin pointed out, query give 0:

select count(1) tablename info::text 'user%'; 

why because simple pattern match , don't have strings in info column begins 'user'. might have better luck '%user%' that's going awfully slow (unless have trigram extension enabled , index created)

if want count how many users have username1 set it's simple query.

select count(1) tablename info ? 'username1' 

this works on 9.5. if have finite number of 'user%' keywords, can try this:

select count(1) tablename info ?| array['username1','username2',..] 

if uncertain nature of keys can do

select count(1) (     select * jsonb_each(info) tablename ) a.key '%user%' 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -