agora inbox for pgsql-sql@postgresql.orghelp / color / mirror / Atom feed
querying within json 5+ messages / 3 participants [nested] [flat]
* querying within json @ 2014-10-30 14:55 Tim Dudgeon <tdudgeon.ml@gmail.com> 0 siblings, 1 reply; 5+ messages in thread From: Tim Dudgeon @ 2014-10-30 14:55 UTC (permalink / raw) To: pgsql-sql Any advice on how to best query for values within json (using 9.4). I have numeric fields within the json and want to include terms for those fields. I've found that something like this works: select * from atable where (json_col->>'numeric_prop')::numeric < 100; But whilst that works: 1. seems to have unnecessary casts? The numeric _prop item is of numeric type, but its getting retrieved as text and then cast to numeric and then compared 2. its not going to use any index on the json_col jsonb column. Is there a better way to do this? Thanks Tim ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: querying within json @ 2014-10-30 15:22 Giuseppe Broccolo <giuseppe.broccolo@2ndquadrant.it> parent: Tim Dudgeon <tdudgeon.ml@gmail.com> 0 siblings, 1 reply; 5+ messages in thread From: Giuseppe Broccolo @ 2014-10-30 15:22 UTC (permalink / raw) To: Tim Dudgeon <tdudgeon.ml@gmail.com>; +Cc: pgsql-sql Hi Tim, 2014-10-30 15:55 GMT+01:00 Tim Dudgeon <tdudgeon.ml@gmail.com>: > Any advice on how to best query for values within json (using 9.4). I have > numeric fields within the json and want to include terms for those fields. > > I've found that something like this works: > > select * from atable where (json_col->>'numeric_prop')::numeric < 100; > > But whilst that works: > 1. seems to have unnecessary casts? The numeric _prop item is of numeric > type, but its getting retrieved as text and then cast to numeric and then > compared > The right operand type of the ->> oeprator is text when ->> is used to get a json object field. So the cast to numeric is needed. > 2. its not going to use any index on the json_col jsonb column. > The usage of an index is mostly ruled by the 'selectivity' of the query. Anyway, if querying for particular items within the key is common (as 'numeric_prop' in your example), defining an index like this may be worthwhile: CREATE INDEX idxgin_numeric_prop ON atable USING gin((json_col->'numeric_prop')); Regards, -- Giuseppe Broccolo - 2ndQuadrant Italy PostgreSQL Training, Services and Support giuseppe.broccolo@2ndQuadrant.it | www.2ndQuadrant.it ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: querying within json @ 2014-10-30 16:24 Tim Dudgeon <tdudgeon.ml@gmail.com> parent: Giuseppe Broccolo <giuseppe.broccolo@2ndquadrant.it> 0 siblings, 1 reply; 5+ messages in thread From: Tim Dudgeon @ 2014-10-30 16:24 UTC (permalink / raw) To: pgsql-sql; +Cc: Giuseppe Broccolo <giuseppe.broccolo@2ndquadrant.it> On 30/10/2014 15:22, Giuseppe Broccolo wrote: > Hi Tim, > > 2014-10-30 15:55 GMT+01:00 Tim Dudgeon <tdudgeon.ml@gmail.com > <mailto:tdudgeon.ml@gmail.com>>: > > Any advice on how to best query for values within json (using > 9.4). I have numeric fields within the json and want to include > terms for those fields. > > I've found that something like this works: > > select * from atable where (json_col->>'numeric_prop')::numeric < 100; > > But whilst that works: > 1. seems to have unnecessary casts? The numeric _prop item is of > numeric type, but its getting retrieved as text and then cast to > numeric and then compared > > > The right operand type of the ->> oeprator is text when ->> is used to > get a json object field. So the cast to numeric is needed. Needed to work, yes. But if my reading of the docs is right then numeric types within json as supposed to be treated as Postgres numeric type? See table 8.23 here: http://www.postgresql.org/docs/9.4/static/datatype-json.html So this would mean a cast from numeric to text and then back to numeric? There is no way to ask for a json 'field' in its actual data type so avoiding the cast? > 2. its not going to use any index on the json_col jsonb column. > > > The usage of an index is mostly ruled by the 'selectivity' of the > query. Anyway, if querying for particular items within the key is > common (as 'numeric_prop' in your example), defining an index like > this may be worthwhile: > > CREATE INDEX idxgin_numeric_prop ON atable USING > gin((json_col->'numeric_prop')); I can add the index, but no evidence of it being used when I run a query like this: select * from atable where (json_col->>'numeric_prop')::numeric < 100; Tim > > Regards, > -- > Giuseppe Broccolo - 2ndQuadrant Italy > PostgreSQL Training, Services and Support > giuseppe.broccolo@2ndQuadrant.it > <mailto:giuseppe.broccolo@2ndQuadrant.it> | www.2ndQuadrant.it > <http://www.2ndQuadrant.it; ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: querying within json @ 2014-10-30 16:48 David G Johnston <david.g.johnston@gmail.com> parent: Tim Dudgeon <tdudgeon.ml@gmail.com> 0 siblings, 1 reply; 5+ messages in thread From: David G Johnston @ 2014-10-30 16:48 UTC (permalink / raw) To: pgsql-sql Tim Dudgeon wrote > On 30/10/2014 15:22, Giuseppe Broccolo wrote: >> Hi Tim, >> >> 2014-10-30 15:55 GMT+01:00 Tim Dudgeon < > tdudgeon.ml@ > > > <mailto: > tdudgeon.ml@ > >>: >> >> Any advice on how to best query for values within json (using >> 9.4). I have numeric fields within the json and want to include >> terms for those fields. >> >> I've found that something like this works: >> >> select * from atable where (json_col->>'numeric_prop')::numeric < >> 100; >> >> But whilst that works: >> 1. seems to have unnecessary casts? The numeric _prop item is of >> numeric type, but its getting retrieved as text and then cast to >> numeric and then compared >> >> >> The right operand type of the ->> oeprator is text when ->> is used to >> get a json object field. So the cast to numeric is needed. > > Needed to work, yes. But if my reading of the docs is right then numeric > types within json as supposed to be treated as Postgres numeric type? > See table 8.23 here: > http://www.postgresql.org/docs/9.4/static/datatype-json.html > So this would mean a cast from numeric to text and then back to numeric? > There is no way to ask for a json 'field' in its actual data type so > avoiding the cast? While the semantic definition of json-number and postgres-numeric are made to be similar (allowed range of values mostly, plus when creating json from a numeric the system knows to store the data as a number instead of as text) there is currently no way to directly go from the internal json-number representation to postgres-numeric. On a technical side-note: "The right operand type of the ->> oeprator [sic] is text when ->> is used to get a json object field. So the cast to numeric is needed." doesn't make sense. The fact that the right operand is text has no bearing on whether a cast to numeric is required. The fact that the operator "json->>text" returns text does. Note that since an operator cannot return different types dependent upon the value being returned if you really wanted to try and directly return a numeric you would need a different operator/function. >> >> CREATE INDEX idxgin_numeric_prop ON atable USING >> gin((json_col->'numeric_prop')); > > I can add the index, but no evidence of it being used when I run a query > like this: > select * from atable where (json_col->>'numeric_prop')::numeric < 100; The index is storing text while the expression has been cast to numeric so, no, if what is shown above is exactly what you did then you would not be using the index. -- View this message in context: http://postgresql.1045698.n5.nabble.com/querying-within-json-tp5825042p5825055.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: querying within json @ 2014-10-30 19:22 Tim Dudgeon <tdudgeon.ml@gmail.com> parent: David G Johnston <david.g.johnston@gmail.com> 0 siblings, 0 replies; 5+ messages in thread From: Tim Dudgeon @ 2014-10-30 19:22 UTC (permalink / raw) To: pgsql-sql On 30/10/2014 16:48, David G Johnston wrote: >>> CREATE INDEX idxgin_numeric_prop ON atable USING >>> gin((json_col->'numeric_prop')); >> I can add the index, but no evidence of it being used when I run a query >> like this: >> select * from atable where (json_col->>'numeric_prop')::numeric < 100; > The index is storing text while the expression has been cast to numeric so, > no, if what is shown above is exactly what you did then you would not be > using the index. So I tried to create the index to make it a numeric index using a cast (won't show the failed details) but failed. Any suggestions on how to do this? Tim > > > > > > -- > View this message in context: http://postgresql.1045698.n5.nabble.com/querying-within-json-tp5825042p5825055.html > Sent from the PostgreSQL - sql mailing list archive at Nabble.com. > > -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql ^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2014-10-30 19:22 UTC | newest] Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2014-10-30 14:55 querying within json Tim Dudgeon <tdudgeon.ml@gmail.com> 2014-10-30 15:22 ` Giuseppe Broccolo <giuseppe.broccolo@2ndquadrant.it> 2014-10-30 16:24 ` Tim Dudgeon <tdudgeon.ml@gmail.com> 2014-10-30 16:48 ` David G Johnston <david.g.johnston@gmail.com> 2014-10-30 19:22 ` Tim Dudgeon <tdudgeon.ml@gmail.com>
This inbox is served by agora; see mirroring instructions for how to clone and mirror all data and code used for this inbox