Guest User

Untitled

a guest
Jan 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. CREATE TABLE jsonthings(d JSONB NOT NULL);
  2. INSERT INTO jsonthings VALUES
  3. ('{"name":"First","tags":["foo"]}')
  4. , ('{"name":"Second","tags":["foo","bar"]}')
  5. , ('{"name":"Third","tags":["bar","baz"]}')
  6. , ('{"name":"Fourth","tags":["baz"]}');
  7. CREATE INDEX idx_jsonthings_name ON jsonthings USING GIN ((d->'name'));
  8.  
  9. EXPLAIN ANALYZE SELECT d FROM jsonthings WHERE d->'name' ? 'First';
  10.  
  11. QUERY PLAN
  12. ----------------------------------------------------------------------------------------------------------------------------------
  13. Bitmap Heap Scan on jsonthings (cost=113.50..30236.13 rows=10000 width=61) (actual time=0.024..0.025 rows=1 loops=1)
  14. Recheck Cond: ((d -> 'name'::text) ? 'First'::text)
  15. Heap Blocks: exact=1
  16. -> 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)
  17. Index Cond: ((d -> 'name'::text) ? 'First'::text)
  18. Planning time: 0.073 ms
  19. Execution time: 0.047 ms
  20. (7 rows)
  21.  
  22. EXPLAIN ANALYZE SELECT d FROM jsonthings WHERE jsonb_exists(d->'name','First');
  23. QUERY PLAN
  24. ----------------------------------------------------------------------------------------------------------------------------------
  25. Seq Scan on jsonthings (cost=10000000000.00..10000263637.06 rows=3333334 width=61) (actual time=0.016..3135.119 rows=1 loops=1)
  26. Filter: jsonb_exists((d -> 'name'::text), 'First'::text)
  27. Rows Removed by Filter: 10000003
  28. Planning time: 0.051 ms
  29. Execution time: 3135.138 ms
  30. (5 rows)
  31.  
  32. CREATE OPERATOR ### (
  33. PROCEDURE = jsonb_exists,
  34. LEFTARG = jsonb,
  35. RIGHTARG = text,
  36. RESTRICT = contsel,
  37. JOIN = contjoinsel);
  38.  
  39. EXPLAIN ANALYZE SELECT d FROM jsonthings WHERE d->'name' ### 'First';
  40. QUERY PLAN
  41. --------------------------------------------------------------------------------------------------------------------------------
  42. Seq Scan on jsonthings (cost=10000000000.00..10000263637.06 rows=10000 width=61) (actual time=0.012..3381.608 rows=1 loops=1)
  43. Filter: ((d -> 'name'::text) ### 'First'::text)
  44. Rows Removed by Filter: 10000003
  45. Planning time: 0.046 ms
  46. Execution time: 3381.623 ms
  47. (5 rows)
  48.  
  49. Name Indexed Data Type Indexable Operators
  50. ...
  51. jsonb_ops jsonb ? ?& ?| @>
  52.  
  53. CREATE OR REPLACE FUNCTION jb_contains(jsonb, text)
  54. RETURNS bool AS
  55. 'SELECT $1 ? $2' LANGUAGE sql IMMUTABLE;
  56.  
  57. CREATE INDEX idx_jsonthings_name ON jsonthings USING GIN ((d->'name'));
  58.  
  59. CREATE INDEX idx_jsonthings_d_gin ON jsonthings USING GIN (d);
  60.  
  61. SELECT d FROM jsonthings WHERE d @> '{"name":"First"}'
  62.  
  63. CREATE INDEX idx_jsonthings_d_email ON jsonthings ((d->>'email'));
  64.  
  65. SELECT d FROM jsonthings WHERE d->>'email' = 'First';
Add Comment
Please, Sign In to add comment