Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE jsonthings(d JSONB NOT NULL);
- INSERT INTO jsonthings VALUES
- ('{"name":"First","tags":["foo"]}')
- , ('{"name":"Second","tags":["foo","bar"]}')
- , ('{"name":"Third","tags":["bar","baz"]}')
- , ('{"name":"Fourth","tags":["baz"]}');
- CREATE INDEX idx_jsonthings_name ON jsonthings USING GIN ((d->'name'));
- EXPLAIN ANALYZE SELECT d FROM jsonthings WHERE d->'name' ? 'First';
- QUERY PLAN
- ----------------------------------------------------------------------------------------------------------------------------------
- Bitmap Heap Scan on jsonthings (cost=113.50..30236.13 rows=10000 width=61) (actual time=0.024..0.025 rows=1 loops=1)
- Recheck Cond: ((d -> 'name'::text) ? 'First'::text)
- Heap Blocks: exact=1
- -> Bitmap Index Scan on idx_jsonthings_name (cost=0.00..111.00 rows=10000 width=0) (actual time=0.015..0.015 rows=1 loops=1)
- Index Cond: ((d -> 'name'::text) ? 'First'::text)
- Planning time: 0.073 ms
- Execution time: 0.047 ms
- (7 rows)
- EXPLAIN ANALYZE SELECT d FROM jsonthings WHERE jsonb_exists(d->'name','First');
- QUERY PLAN
- ----------------------------------------------------------------------------------------------------------------------------------
- Seq Scan on jsonthings (cost=10000000000.00..10000263637.06 rows=3333334 width=61) (actual time=0.016..3135.119 rows=1 loops=1)
- Filter: jsonb_exists((d -> 'name'::text), 'First'::text)
- Rows Removed by Filter: 10000003
- Planning time: 0.051 ms
- Execution time: 3135.138 ms
- (5 rows)
- CREATE OPERATOR ### (
- PROCEDURE = jsonb_exists,
- LEFTARG = jsonb,
- RIGHTARG = text,
- RESTRICT = contsel,
- JOIN = contjoinsel);
- EXPLAIN ANALYZE SELECT d FROM jsonthings WHERE d->'name' ### 'First';
- QUERY PLAN
- --------------------------------------------------------------------------------------------------------------------------------
- Seq Scan on jsonthings (cost=10000000000.00..10000263637.06 rows=10000 width=61) (actual time=0.012..3381.608 rows=1 loops=1)
- Filter: ((d -> 'name'::text) ### 'First'::text)
- Rows Removed by Filter: 10000003
- Planning time: 0.046 ms
- Execution time: 3381.623 ms
- (5 rows)
- Name Indexed Data Type Indexable Operators
- ...
- jsonb_ops jsonb ? ?& ?| @>
- CREATE OR REPLACE FUNCTION jb_contains(jsonb, text)
- RETURNS bool AS
- 'SELECT $1 ? $2' LANGUAGE sql IMMUTABLE;
- CREATE INDEX idx_jsonthings_name ON jsonthings USING GIN ((d->'name'));
- CREATE INDEX idx_jsonthings_d_gin ON jsonthings USING GIN (d);
- SELECT d FROM jsonthings WHERE d @> '{"name":"First"}'
- CREATE INDEX idx_jsonthings_d_email ON jsonthings ((d->>'email'));
- SELECT d FROM jsonthings WHERE d->>'email' = 'First';
Add Comment
Please, Sign In to add comment