Guest User

Untitled

a guest
Jun 19th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. CREATE TYPE platforms AS ENUM ('p1', 'p2', 'p3');
  2.  
  3. CREATE TABLE mytable (
  4. id bigint NOT NULL DEFAULT nextval('mytable_sq'::regclass),
  5. related_id integer NOT NULL,
  6. platform platforms NOT NULL DEFAULT 'default'::platforms,
  7. name character varying(200) NOT NULL,
  8. CONSTRAINT mytable_pkey PRIMARY KEY (id),
  9. CONSTRAINT mytable_related_id_fkey FOREIGN KEY (related_id)
  10. REFERENCES related (id)
  11. );
  12.  
  13. CREATE INDEX related_id__platform__index ON mytable (related_id, platform);
  14. CREATE UNIQUE INDEX some_other_index ON mytable (related_id, lower(name::text));
  15.  
  16. EXPLAIN ANALYZE
  17. SELECT * FROM mytable
  18. WHERE related_id=1 AND platform='p2'::platforms
  19. LIMIT 20;
  20.  
  21. Limit (cost=0.00..14.07 rows=20 width=737) (actual time=12863.465..12863.465 rows=0 loops=1)
  22. -> Seq Scan on mytable (cost=0.00..1492790.47 rows=2122653 width=737) (actual time=12863.464..12863.464 rows=0 loops=1)
  23. Filter: ((related_id = 1) AND (platform = 'p2'::platforms))
  24. Rows Removed by Filter: 21076656
  25. Planning time: 3.540 ms
  26. Execution time: 12868.190 ms
  27.  
  28. EXPLAIN ANALYZE
  29. SELECT * FROM mytable
  30. WHERE related_id=1 AND platform='p2'::platforms;
  31.  
  32. Bitmap Heap Scan on mytable (cost=60533.63..1295799.94 rows=2122653 width=737) (actual time=0.890..0.890 rows=0 loops=1)
  33. Recheck Cond: ((related_id = 1) AND (platform = 'p2'::platforms))
  34. -> Bitmap Index Scan on related_id__platform__index (cost=0.00..60002.97 rows=2122653 width=0) (actual time=0.888..0.888 rows=0 loops=1)
  35. Index Cond: ((related_id = 1) AND (platform = 'p2'::platforms))
  36. Planning time: 0.827 ms
  37. Execution time: 1.104 ms
  38.  
  39. EXPLAIN ANALYZE
  40. SELECT * FROM mytable
  41. WHERE related_id=1 AND platform='p1'::platforms
  42. LIMIT 20;
  43.  
  44. Limit (cost=0.44..70.95 rows=20 width=737) (actual time=0.759..0.995 rows=20 loops=1)
  45. -> Index Scan using related_id__platform__index on mytable (cost=0.44..1217669.26 rows=345388 width=737) (actual time=0.759..0.993 rows=20 loops=1)
  46. Index Cond: ((related_id = 1) AND (platform = 'p1'::platforms))
  47. Planning time: 5.776 ms
  48. Execution time: 2.476 ms
  49.  
  50. EXPLAIN ANALYZE
  51. SELECT * FROM mytable
  52. WHERE related_id=1 AND platform='p3'::platforms LIMIT 20;
  53.  
  54. Limit (cost=0.44..80.37 rows=20 width=737) (actual time=0.014..0.016 rows=2 loops=1)
  55. -> Index Scan using related_id__platform__index on mytable (cost=0.44..99497.62 rows=24894 width=737) (actual time=0.014..0.015 rows=2 loops=1)
  56. Index Cond: ((related_id = 1) AND (platform = 'p3'::platforms))
  57. Planning time: 0.972 ms
  58. Execution time: 0.123 ms
  59.  
  60. SELECT * FROM tbl
  61. WHERE related_id = 1 AND platform = 'p2'::platforms
  62. LIMIT 20;
  63.  
  64. CREATE INDEX related_id_1_platform_2_idx ON tbl (id)
  65. WHERE related_id = 1 AND platform = 'p2'::platforms;
  66.  
  67. CREATE INDEX related_id_1_idx ON tbl (platform)
  68. WHERE related_id = 1;
  69.  
  70. CREATE INDEX related_id_1_func_idx ON tbl ((platform::text::platforms)) -- double parens!
  71. WHERE related_id = 1;
  72.  
  73. SELECT *
  74. FROM pg_stats
  75. WHERE schemaname = 'public' -- actual schema
  76. AND tablename = 'related_id_1_func_idx'; -- actual idx name
  77.  
  78. SELECT ...
  79. WHERE related_id = 1 AND platform::text::platforms = 'p2'::platforms;
Add Comment
Please, Sign In to add comment