Skip to content

Instantly share code, notes, and snippets.

@bdmac
Last active August 29, 2015 13:59
Show Gist options
  • Save bdmac/10496601 to your computer and use it in GitHub Desktop.
Save bdmac/10496601 to your computer and use it in GitHub Desktop.
Range Queries
d1k52r595ne826=> EXPLAIN SELECT "reservations".* FROM "reservations" WHERE (arrival_day && tsrange('2014-04-11 18:42:18.813381', NULL, '[]'));
QUERY PLAN
-----------------------------------------------------------------------
Seq Scan on reservations (cost=0.00..5733.91 rows=14230 width=1038)
Filter: (arrival_day && '["2014-04-11 18:42:18.813381",)'::tsrange)
(2 rows)
d1k52r595ne826=> EXPLAIN SELECT "reservations".* FROM "reservations" WHERE (arrival_day && tsrange('2014-04-11 18:42:18.813381', '2014-04-15 18:42:18.813381', '[]'));
QUERY PLAN
-------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on reservations (cost=68.03..2939.55 rows=2564 width=1038)
Recheck Cond: (arrival_day && '["2014-04-11 18:42:18.813381","2014-04-15 18:42:18.813381"]'::tsrange)
-> Bitmap Index Scan on index_reservations_on_arrival_day (cost=0.00..67.90 rows=2564 width=0)
Index Cond: (arrival_day && '["2014-04-11 18:42:18.813381","2014-04-15 18:42:18.813381"]'::tsrange)
(4 rows)
d1k52r595ne826=> EXPLAIN SELECT "reservations".* FROM "reservations" WHERE (arrival_day && tsrange('2014-04-11 18:42:18.813381', '2029-04-15 18:42:18.813381', '[]'));
QUERY PLAN
---------------------------------------------------------------------------------------------------
Seq Scan on reservations (cost=0.00..5733.91 rows=14230 width=1038)
Filter: (arrival_day && '["2014-04-11 18:42:18.813381","2029-04-15 18:42:18.813381"]'::tsrange)
(2 rows)
d1k52r595ne826=> EXPLAIN SELECT "reservations".* FROM "reservations" WHERE (arrival_day && tsrange('2014-04-11 18:42:18.813381', '2015-04-15 18:42:18.813381', '[]'));
QUERY PLAN
---------------------------------------------------------------------------------------------------
Seq Scan on reservations (cost=0.00..5733.91 rows=14052 width=1038)
Filter: (arrival_day && '["2014-04-11 18:42:18.813381","2015-04-15 18:42:18.813381"]'::tsrange)
(2 rows)
d1k52r595ne826=> EXPLAIN SELECT "reservations".* FROM "reservations" WHERE (arrival_day && tsrange('2014-04-11 18:42:18.813381', '2014-04-15 18:42:18.813381', '[]'));
QUERY PLAN
-------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on reservations (cost=68.03..2939.55 rows=2564 width=1038)
Recheck Cond: (arrival_day && '["2014-04-11 18:42:18.813381","2014-04-15 18:42:18.813381"]'::tsrange)
-> Bitmap Index Scan on index_reservations_on_arrival_day (cost=0.00..67.90 rows=2564 width=0)
Index Cond: (arrival_day && '["2014-04-11 18:42:18.813381","2014-04-15 18:42:18.813381"]'::tsrange)
(4 rows)
d1k52r595ne826=> EXPLAIN SELECT "reservations".* FROM "reservations" WHERE (tsrange('2014-04-11 18:42:18.813381', NULL, '[]') && arrival_day);
QUERY PLAN
-----------------------------------------------------------------------
Seq Scan on reservations (cost=0.00..5733.91 rows=14230 width=1038)
Filter: ('["2014-04-11 18:42:18.813381",)'::tsrange && arrival_day)
(2 rows)
d1k52r595ne826=> EXPLAIN SELECT "reservations".* FROM "reservations" WHERE NOT(arrival_day << tsrange('2014-04-11 18:42:18.813381', '2014-04-11 18:42:18.813381', '[]'));
QUERY PLAN
---------------------------------------------------------------------------------------------------------
Seq Scan on reservations (cost=0.00..5733.91 rows=15201 width=1038)
Filter: (NOT (arrival_day << '["2014-04-11 18:42:18.813381","2014-04-11 18:42:18.813381"]'::tsrange))
(2 rows)
d1k52r595ne826=> EXPLAIN SELECT "reservations".* FROM "reservations" WHERE NOT(arrival_day >> tsrange('2014-04-11 18:42:18.813381', '2014-04-11 18:42:18.813381', '[]'));
QUERY PLAN
---------------------------------------------------------------------------------------------------------
Seq Scan on reservations (cost=0.00..5733.91 rows=80287 width=1038)
Filter: (NOT (arrival_day >> '["2014-04-11 18:42:18.813381","2014-04-11 18:42:18.813381"]'::tsrange))
(2 rows)
d1k52r595ne826=> EXPLAIN SELECT "reservations".* FROM "reservations" WHERE NOT(tsrange('2014-04-11 18:42:18.813381', '2014-04-11 18:42:18.813381', '[]') >> arrival_day);
QUERY PLAN
---------------------------------------------------------------------------------------------------------
Seq Scan on reservations (cost=0.00..5733.91 rows=15201 width=1038)
Filter: (NOT ('["2014-04-11 18:42:18.813381","2014-04-11 18:42:18.813381"]'::tsrange >> arrival_day))
(2 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment