Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT *
- FROM MY_TABLE
- WHERE date > ?
- AND (
- (type1 = ?)
- OR (type2 = ?)
- OR (type2 = ? AND associated_id = ?)
- OR ...
- )
- ORDER BY date, num;
- CREATE INDEX my_table_1 ON my_table (date, num);
- CREATE INDEX my_table_2 ON my_table (type1);
- CREATE INDEX my_table_3 ON my_table (type2, id_in_type2);
- CREATE INDEX my_table_4 ON my_table (type1, type2, id_in_type2);
- explain analyze
- select * from my_table
- where date >= now() - '1 day'::interval
- and (
- (type2 = 'T2A' and type1 = 'T1A')
- or (type2 = 'T2B' and type1 = 'T1B1')
- or (type1 = 'T1C')
- or (type2 = 'T2B' and type1 = 'T1B2')
- )
- order by date, num;
- Sort (cost=157714.25..158431.63 rows=286949 width=262) (actual time=2153.327..2313.075 rows=600314 loops=1)
- Sort Key: date, num
- Sort Method: external merge Disk: 144376kB
- -> 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)
- 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)))
- Rows Removed by Index Recheck: 455892
- 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))))
- Rows Removed by Filter: 2
- Heap Blocks: exact=42763 lossy=26546
- -> BitmapOr (cost=16602.76..16602.76 rows=299384 width=0) (actual time=188.980..188.980 rows=0 loops=1)
- -> 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)
- Index Cond: ((type1)::text = 'T1A'::text)
- -> 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)
- Index Cond: (((type1)::text = 'T1B1'::text) AND ((type2)::text = 'T2B'::text))
- -> 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)
- Index Cond: ((type1)::text = 'T1C'::text)
- -> 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)
- Index Cond: (((type1)::text = 'T1B2'::text) AND ((type2)::text = 'T2B'::text))
- Planning time: 0.435 ms
- Execution time: 2356.305 ms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement