Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. SELECT *
  2. FROM MY_TABLE
  3. WHERE date > ?
  4. AND (
  5. (type1 = ?)
  6. OR (type2 = ?)
  7. OR (type2 = ? AND associated_id = ?)
  8. OR ...
  9. )
  10. ORDER BY date, num;
  11.  
  12. CREATE INDEX my_table_1 ON my_table (date, num);
  13. CREATE INDEX my_table_2 ON my_table (type1);
  14. CREATE INDEX my_table_3 ON my_table (type2, id_in_type2);
  15. CREATE INDEX my_table_4 ON my_table (type1, type2, id_in_type2);
  16.  
  17. explain analyze
  18. select * from my_table
  19. where date >= now() - '1 day'::interval
  20. and (
  21. (type2 = 'T2A' and type1 = 'T1A')
  22. or (type2 = 'T2B' and type1 = 'T1B1')
  23. or (type1 = 'T1C')
  24. or (type2 = 'T2B' and type1 = 'T1B2')
  25. )
  26. order by date, num;
  27.  
  28. Sort (cost=157714.25..158431.63 rows=286949 width=262) (actual time=2153.327..2313.075 rows=600314 loops=1)
  29. Sort Key: date, num
  30. Sort Method: external merge Disk: 144376kB
  31. -> Bitmap Heap Scan on my_table (cost=16602.76..96390.20 rows=286949 width=262) (actual time=199.193..1281.214 rows=600314 loops=1)
  32. Recheck Cond: (((type1)::text = 'T1A'::text) OR (((type1)::text = 'T1B1'::text) AND ((type2)::text = 'T2B'::text)) OR ((type1)::text = 'T1C'::text) OR (((type1)::text = 'T1B2'::text) AND ((type2)::text = 'T2B'::text)))
  33. Rows Removed by Index Recheck: 455892
  34. Filter: ((date >= (now() - '1 day'::interval)) AND ((((type2)::text = 'T2A'::text) AND ((type1)::text = 'T1A'::text)) OR (((type2)::text = 'T2B'::text) AND ((type1)::text = 'T1B1'::text)) OR ((type1)::text = 'T1C'::text) OR (((type2)::text = 'T2B'::text) AND ((type1)::text = 'T1B2'::text))))
  35. Rows Removed by Filter: 2
  36. Heap Blocks: exact=42763 lossy=26546
  37. -> BitmapOr (cost=16602.76..16602.76 rows=299384 width=0) (actual time=188.980..188.980 rows=0 loops=1)
  38. -> Bitmap Index Scan on my_table_2 (cost=0.00..4.44 rows=1 width=0) (actual time=0.037..0.037 rows=30 loops=1)
  39. Index Cond: ((type1)::text = 'T1A'::text)
  40. -> Bitmap Index Scan on my_table_4 (cost=0.00..8094.98 rows=148643 width=0) (actual time=93.562..93.562 rows=300240 loops=1)
  41. Index Cond: (((type1)::text = 'T1B1'::text) AND ((type2)::text = 'T2B'::text))
  42. -> Bitmap Index Scan on my_table_2 (cost=0.00..4.44 rows=1 width=0) (actual time=0.059..0.059 rows=28 loops=1)
  43. Index Cond: ((type1)::text = 'T1C'::text)
  44. -> Bitmap Index Scan on my_table_4 (cost=0.00..8211.96 rows=150741 width=0) (actual time=95.315..95.315 rows=300018 loops=1)
  45. Index Cond: (((type1)::text = 'T1B2'::text) AND ((type2)::text = 'T2B'::text))
  46. Planning time: 0.435 ms
  47. Execution time: 2356.305 ms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement