SELECT t0.address, t1.address, COUNT(t2.time), MAX(t2.time) FROM Prelude_Alert AS top_table LEFT JOIN Prelude_Address AS t0 ON (t0._parent_type='S' AND t0._message_ident=top_table._ident AND t0._parent0_index = 0 AND t0._index != -1) LEFT JOIN Prelude_Address AS t1 ON (t1._parent_type='T' AND t1._message_ident=top_table._ident AND t1._parent0_index = 0 AND t1._index != -1) LEFT JOIN Prelude_CreateTime AS t2 ON (t2._parent_type='A' AND t2._message_ident=top_table._ident) WHERE t2.time >= '2007-03-09 10:52:13' AND t2.time < '2007-03-09 11:52:13' GROUP BY 1, 2 ORDER BY 4 DESC LIMIT 50 explain analyze output: ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=1771864.90..1771864.94 rows=16 width=42) (actual time=231233.299..231233.338 rows=50 loops=1) -> Sort (cost=1771864.90..1771864.94 rows=16 width=42) (actual time=231233.297..231233.315 rows=50 loops=1) Sort Key: max(t2."time") -> HashAggregate (cost=1771864.34..1771864.58 rows=16 width=42) (actual time=231232.670..231232.853 rows=133 loops=1) -> Merge Join (cost=1668080.32..1771845.15 rows=1919 width=42) (actual time=143418.179..231230.540 rows=1556 loops=1) Merge Cond: ("outer"._ident = "inner"._message_ident) -> Merge Left Join (cost=1664600.77..1763435.02 rows=1960612 width=42) (actual time=140815.174..230531.083 rows=1947114 loops=1) Merge Cond: ("outer"._ident = "inner"._message_ident) -> Merge Left Join (cost=830777.20..902806.18 rows=1960612 width=25) (actual time=71583.365..127267.700 rows=1947114 loops=1) Merge Cond: ("outer"._ident = "inner"._message_ident) -> Index Scan using prelude_alert_pkey on prelude_alert top_table (cost=0.00..45385.73 rows=1960612 width=8) (actual time=11.919..20127.718 rows=1947032 loops=1) -> Sort (cost=830777.20..834400.82 rows=1449448 width=25) (actual time=71571.434..104661.819 rows=1943115 loops=1) Sort Key: t0._message_ident -> Bitmap Heap Scan on prelude_address t0 (cost=32246.36..517087.41 rows=1449448 width=25) (actual time=25295.354..61881.225 rows=1953299 loops=1) Recheck Cond: (((_parent_type)::text = 'S'::text) AND (_parent0_index = 0)) Filter: (_index <> -1) -> Bitmap Index Scan on prelude_address_index_address (cost=0.00..32246.36 rows=2666060 width=0) (actual time=25265.752..25265.752 rows=3906516 loops=1) Index Cond: (((_parent_type)::text = 'S'::text) AND (_parent0_index = 0)) -> Sort (cost=833823.57..837474.20 rows=1460249 width=25) (actual time=69231.799..101040.433 rows=1944996 loops=1) Sort Key: t1._message_ident -> Bitmap Heap Scan on prelude_address t1 (cost=32486.56..517675.26 rows=1460249 width=25) (actual time=21513.229..59289.124 rows=1958487 loops=1) Recheck Cond: (((_parent_type)::text = 'T'::text) AND (_parent0_index = 0)) Filter: (_index <> -1) -> Bitmap Index Scan on prelude_address_index_address (cost=0.00..32486.56 rows=2685926 width=0) (actual time=21466.907..21466.907 rows=3916974 loops=1) Index Cond: (((_parent_type)::text = 'T'::text) AND (_parent0_index = 0)) -> Sort (cost=3479.56..3484.49 rows=1972 width=16) (actual time=126.249..126.599 rows=1556 loops=1) Sort Key: t2._message_ident -> Index Scan using prelude_createtime_index on prelude_createtime t2 (cost=0.00..3371.63 rows=1972 width=16) (actual time=85.597..123.163 rows=1556 loops=1) Index Cond: (((_parent_type)::text = 'A'::text) AND ("time" >= '2007-03-09 10:52:13'::timestamp without time zone) AND ("time" < '2007-03-09 11:52:13'::timestamp without time zone)) Total runtime: 231399.145 ms explain analyze with work_mem=8192, effective_cache_size=100000 and shared_buffers=16384 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=1545902.98..1545903.02 rows=17 width=42) (actual time=280342.025..280352.585 rows=50 loops=1) -> Sort (cost=1545902.98..1545903.02 rows=17 width=42) (actual time=280342.023..280342.038 rows=50 loops=1) Sort Key: max(t2."time") -> HashAggregate (cost=1545902.37..1545902.63 rows=17 width=42) (actual time=280341.298..280341.424 rows=133 loops=1) -> Merge Join (cost=1439580.89..1545884.65 rows=1772 width=42) (actual time=193234.235..280339.028 rows=1556 loops=1) Merge Cond: ("outer"._ident = "inner"._message_ident) -> Merge Left Join (cost=1436389.69..1537765.10 rows=1960612 width=42) (actual time=190628.151..279633.910 rows=1947114 loops=1) Merge Cond: ("outer"._ident = "inner"._message_ident) -> Merge Left Join (cost=738814.84..812865.50 rows=1960612 width=25) (actual time=98436.798..151293.804 rows=1947114 loops=1) Merge Cond: ("outer"._ident = "inner"._message_ident) -> Index Scan using prelude_alert_pkey on prelude_alert top_table (cost=0.00..45385.73 rows=1960612 width=8) (actual time=27.367..21083.326 rows=1947032 loops=1) -> Sort (cost=738814.84..742776.04 rows=1584479 width=25) (actual time=98409.408..127868.644 rows=1943115 loops=1) Sort Key: t0._message_ident -> Bitmap Heap Scan on prelude_address t0 (cost=35710.67..525563.46 rows=1584479 width=25) (actual time=48565.877..86570.402 rows=1953299 loops=1) Recheck Cond: (((_parent_type)::text = 'S'::text) AND (_parent0_index = 0)) Filter: (_index <> -1) -> Bitmap Index Scan on prelude_address_index_address (cost=0.00..35710.67 rows=2952445 width=0) (actual time=48413.923..48413.923 rows=3906516 loops=1) Index Cond: (((_parent_type)::text = 'S'::text) AND (_parent0_index = 0)) -> Sort (cost=697574.85..701312.65 rows=1495120 width=25) (actual time=92191.341..126094.592 rows=1944996 loops=1) Sort Key: t1._message_ident -> Bitmap Heap Scan on prelude_address t1 (cost=33696.63..520635.54 rows=1495120 width=25) (actual time=45640.668..80507.115 rows=1958487 loops=1) Recheck Cond: (((_parent_type)::text = 'T'::text) AND (_parent0_index = 0)) Filter: (_index <> -1) -> Bitmap Index Scan on prelude_address_index_address (cost=0.00..33696.63 rows=2785938 width=0) (actual time=45480.201..45480.201 rows=3916974 loops=1) Index Cond: (((_parent_type)::text = 'T'::text) AND (_parent0_index = 0)) -> Sort (cost=3191.19..3195.75 rows=1821 width=16) (actual time=156.087..156.458 rows=1556 loops=1) Sort Key: t2._message_ident -> Index Scan using prelude_createtime_index on prelude_createtime t2 (cost=0.00..3092.58 rows=1821 width=16) (actual time=125.140..154.208 rows=1556 loops=1) Index Cond: (((_parent_type)::text = 'A'::text) AND ("time" >= '2007-03-09 10:52:13'::timestamp without time zone) AND ("time" < '2007-03-09 11:52:13'::timestamp without time zone)) Total runtime: 280496.491 ms another query: prelude=> explain analyze SELECT t0.address, t1.address, COUNT(t2.time), MAX(t2.time) FROM Prelude_CreateTime as t2 JOIN Prelude_Alert as top_table ON (t2._parent_type='A' AND t2._message_ident=top_table._ident) LEFT JOIN Prelude_Address AS t0 ON (t0._parent_type='S' AND t0._message_ident=top_table._ident AND t0._parent0_index = 0 AND t0._index != -1) LEFT JOIN Prelude_Address AS t1 ON (t1._parent_type='T' AND t1._message_ident=top_table._ident AND t1._parent0_index = 0 AND t1._index != -1) WHERE t2.time >= '2007-03-09 10:52:13' AND t2.time < '2007-03-09 11:52:13' GROUP BY 1, 2 ORDER BY 4 DESC LIMIT 50; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=1460538.30..1460538.34 rows=17 width=42) (actual time=189181.325..189202.431 rows=50 loops=1) -> Sort (cost=1460538.30..1460538.34 rows=17 width=42) (actual time=189181.323..189181.332 rows=50 loops=1) Sort Key: max(t2."time") -> HashAggregate (cost=1460537.70..1460537.95 rows=17 width=42) (actual time=189180.935..189181.060 rows=133 loops=1) -> Merge Left Join (cost=1445088.23..1460519.98 rows=1772 width=42) (actual time=184401.247..189179.057 rows=1556 loops=1) Merge Cond: ("outer"._ident = "inner"._message_ident) -> Merge Left Join (cost=747513.38..755455.16 rows=1772 width=33) (actual time=93014.156..95242.059 rows=1556 loops=1) Merge Cond: ("outer"._ident = "inner"._message_ident) -> Sort (cost=8698.54..8702.97 rows=1772 width=16) (actual time=16.586..16.907 rows=1556 loops=1) Sort Key: top_table._ident -> Nested Loop (cost=0.00..8602.93 rows=1772 width=16) (actual time=0.048..13.885 rows=1556 loops=1) -> Index Scan using prelude_createtime_index on prelude_createtime t2 (cost=0.00..3092.58 rows=1821 width=16) (actual time=0.035..2.088 rows=1556 loops=1) Index Cond: (((_parent_type)::text = 'A'::text) AND ("time" >= '2007-03-09 10:52:13'::timestamp without time zone) AND ("time" < '2007-03-09 11:52:13'::timestamp without time zone)) -> Index Scan using prelude_alert_pkey on prelude_alert top_table (cost=0.00..3.01 rows=1 width=8) (actual time=0.005..0.006 rows=1 loops=1556) Index Cond: ("outer"._message_ident = top_table._ident) -> Sort (cost=738814.84..742776.04 rows=1584479 width=25) (actual time=92897.548..94659.217 rows=1943115 loops=1) Sort Key: t0._message_ident -> Bitmap Heap Scan on prelude_address t0 (cost=35710.67..525563.46 rows=1584479 width=25) (actual time=44639.243..81020.246 rows=1953299 loops=1) Recheck Cond: (((_parent_type)::text = 'S'::text) AND (_parent0_index = 0)) Filter: (_index <> -1) -> Bitmap Index Scan on prelude_address_index_address (cost=0.00..35710.67 rows=2952445 width=0) (actual time=44417.835..44417.835 rows=3906516 loops=1) Index Cond: (((_parent_type)::text = 'S'::text) AND (_parent0_index = 0)) -> Sort (cost=697574.85..701312.65 rows=1495120 width=25) (actual time=91250.030..93371.417 rows=1944914 loops=1) Sort Key: t1._message_ident -> Bitmap Heap Scan on prelude_address t1 (cost=33696.63..520635.54 rows=1495120 width=25) (actual time=44966.371..79724.554 rows=1958487 loops=1) Recheck Cond: (((_parent_type)::text = 'T'::text) AND (_parent0_index = 0)) Filter: (_index <> -1) -> Bitmap Index Scan on prelude_address_index_address (cost=0.00..33696.63 rows=2785938 width=0) (actual time=44820.608..44820.608 rows=3916974 loops=1) Index Cond: (((_parent_type)::text = 'T'::text) AND (_parent0_index = 0)) Total runtime: 189369.618 ms