title: Optimizing postgres query
body: <pre><code>                                 QUERY PLAN                                   
  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   Unique  (cost=32164.87..32164.89 rows=1 width=44) (actual time=221552.831..221552.831 rows=0 loops=1)
     -&gt;  Sort  (cost=32164.87..32164.87 rows=1 width=44) (actual time=221552.827..221552.827 rows=0 loops=1)
           Sort Key: t.date_effective, t.acct_account_transaction_id, p.method, t.amount, c.business_name, t.amount
           -&gt;  Nested Loop  (cost=22871.67..32164.86 rows=1 width=44) (actual time=221552.808..221552.808 rows=0 loops=1)
                 -&gt;  Nested Loop  (cost=22871.67..32160.37 rows=1 width=52) (actual time=221431.071..221546.619 rows=670 loops=1)
                       -&gt;  Nested Loop  (cost=22871.67..32157.33 rows=1 width=43) (actual time=221421.218..221525.056 rows=2571 loops=1)
                             -&gt;  Hash Join  (cost=22871.67..32152.80 rows=1 width=16) (actual time=221307.382..221491.019 rows=2593 loops=1)
                                   Hash Cond: ("outer".acct_account_id = "inner".acct_account_fk)
                                   -&gt;  Seq Scan on acct_account a  (cost=0.00..7456.08 rows=365008 width=8) (actual time=0.032..118.369 rows=61295 loops=1)
                                   -&gt;  Hash  (cost=22871.67..22871.67 rows=1 width=16) (actual time=221286.733..221286.733 rows=2593 loops=1)
                                         -&gt;  Nested Loop Left Join  (cost=0.00..22871.67 rows=1 width=16) (actual time=1025.396..221266.357 rows=2593 loops=1)
                                               Join Filter: ("inner".orig_acct_payment_fk = "outer".acct_account_transaction_id)
                                               Filter: ("inner".link_type IS NULL)
                                               -&gt;  Seq Scan on acct_account_transaction t  (cost=0.00..18222.98 rows=1 width=16) (actual time=949.081..976.432 rows=2596 loops=1)
                                                     Filter: ((("type")::text = 'debit'::text) AND ((transaction_status)::text = 'active'::text) AND (date_effective &gt;= '2012-03-01'::date) AND (date_effective &lt; '2012-04-01 00:00:00'::timestamp without time zone))
                                               -&gt;  Seq Scan on acct_payment_link l  (cost=0.00..4648.68 rows=1 width=15) (actual time=1.073..84.610 rows=169 loops=2596)
                                                     Filter: ((link_type)::text ~~ 'return_%'::text)
                             -&gt;  Index Scan using contact_pk on contact c  (cost=0.00..4.52 rows=1 width=27) (actual time=0.007..0.008 rows=1 loops=2593)
                                   Index Cond: (c.contact_id = "outer".contact_fk)
                       -&gt;  Index Scan using acct_payment_transaction_fk on acct_payment p  (cost=0.00..3.02 rows=1 width=13) (actual time=0.005..0.005 rows=0 loops=2571)
                             Index Cond: (p.acct_account_transaction_fk = "outer".acct_account_transaction_id)
                             Filter: ((method)::text &lt;&gt; 'trade'::text)
                 -&gt;  Index Scan using contact_role_pk on contact_role  (cost=0.00..4.48 rows=1 width=4) (actual time=0.007..0.007 rows=0 loops=670)
                       Index Cond: ("outer".contact_id = contact_role.contact_fk)
                       Filter: (exchange_fk = 74)
  Total runtime: 221553.019 ms
  </code></pre>