title: What indexes would help speed this query up?
body: <p>I have the following query that's currently taking around 450ms to run:</p>
  
  <pre><code>SELECT stripe_balance_transactions.created, fee, stripe_charges.plan 
  FROM stripe_balance_transactions
  JOIN stripe_charges ON stripe_balance_transactions.source = stripe_charges.stripe_id 
  WHERE 
     stripe_balance_transactions.account_id = 93 
     AND (stripe_balance_transactions.type = 'charge' AND stripe_charges.refunded = false) 
     AND (stripe_charges.invoice IS NOT NULL) 
     AND (stripe_balance_transactions.created BETWEEN '2013-01-20 00:00:00.000000' AND '2014-02-19 23:59:59.999999') 
  ORDER BY stripe_balance_transactions.created;
  </code></pre>
  
  <p>Are there any additional indexes I could be using here to try and speed that query up?</p>
  
  <p>Here's the output from <code>EXPLAIN ANALYZE</code>:</p>
  
  <pre><code> Sort  (cost=30742.84..30748.08 rows=2096 width=25) (actual time=456.942..457.082 rows=1579 loops=1)
     Sort Key: stripe_balance_transactions.created
     Sort Method: quicksort  Memory: 138kB
     -&gt;  Hash Join  (cost=24408.50..30627.21 rows=2096 width=25) (actual time=360.555..455.798 rows=1579 loops=1)
           Hash Cond: ((stripe_balance_transactions.source)::text = (stripe_charges.stripe_id)::text)
           -&gt;  Bitmap Heap Scan on stripe_balance_transactions  (cost=75.16..3924.19 rows=2016 width=30) (actual time=0.756..1.644 rows=1610 loops=1)
                 Recheck Cond: ((account_id = 93) AND ((type)::text = 'charge'::text) AND (created &gt;= '2013-01-20 00:00:00'::timestamp without time zone) AND (created &lt;= '2014-02-19 23:59:59.999999'::timestamp without time zone))
                 -&gt;  Bitmap Index Scan on index_balance_transactions_account_type_created  (cost=0.00..74.66 rows=2016 width=0) (actual time=0.732..0.732 rows=1610 loops=1)
                       Index Cond: ((account_id = 93) AND ((type)::text = 'charge'::text) AND (created &gt;= '2013-01-20 00:00:00'::timestamp without time zone) AND (created &lt;= '2014-02-19 23:59:59.999999'::timestamp without time zone))
           -&gt;  Hash  (cost=18027.34..18027.34 rows=326080 width=31) (actual time=358.523..358.523 rows=216688 loops=1)
                 Buckets: 2048  Batches: 32  Memory Usage: 423kB
                 -&gt;  Seq Scan on stripe_charges  (cost=0.00..18027.34 rows=326080 width=31) (actual time=0.065..233.197 rows=216688 loops=1)
                       Filter: ((NOT refunded) AND (invoice IS NOT NULL))
                       Rows Removed by Filter: 131346
   Total runtime: 457.260 ms
  (15 rows)
  </code></pre>
  
  <p>I'm running Postgres 9.3.1.</p>
  
  <p>EDIT: Here are current indexes...</p>
  
  <pre><code># stripe_charges #
  "stripe_charges_pkey" PRIMARY KEY, btree (id)
  "index_account_ref_created_plan" btree (account_id, refunded, created, plan)
  "index_stripe_charges_on_account_id" btree (account_id)
  "index_stripe_charges_on_account_id_and_amount" btree (account_id, amount)
  "index_stripe_charges_on_account_id_and_amount_and_created" btree (account_id, amount, created)
  "index_stripe_charges_on_account_id_and_paid_and_created" btree (account_id, paid, created)
  "index_stripe_charges_on_account_id_and_stripe_id" btree (account_id, stripe_id)
  "index_stripe_charges_on_customer" btree (customer)
  "index_stripe_charges_on_invoice" btree (invoice)
  "index_stripe_charges_on_is_projected" btree (is_projected)
  "index_stripe_charges_on_is_projected_and_projected" btree (is_projected, projected)
  "index_stripe_charges_on_parent_charge" btree (parent_charge)
  "index_stripe_charges_on_plan" btree (plan)
  "index_stripe_charges_on_projected" btree (projected)
  "index_stripe_charges_on_refunded" btree (refunded)
  "index_stripe_charges_on_stripe_id" btree (stripe_id)
  
  # stripe_balance_transactions #
  "stripe_balance_transactions_pkey" PRIMARY KEY, btree (id)
  "index_balance_transactions_account_type_created" btree (account_id, type, created)
  "index_stripe_balance_transactions_on_account_id" btree (account_id)
  "index_stripe_balance_transactions_on_account_id_and_stripe_id" btree (account_id, stripe_id)
  "index_stripe_balance_transactions_on_plan" btree (plan)
  "index_stripe_balance_transactions_on_source" btree (source)
  "index_stripe_balance_transactions_on_stripe_id" btree (stripe_id)
  "index_stripe_balance_transactions_on_type" btree (type)
  </code></pre>