我怎样才能更好地优化/索引此查询?

时间:2014-01-28 15:12:16

标签: sql postgresql query-optimization

我在Postgres数据库中有以下查询...

SELECT 
   SUM("balance_transactions"."fee") AS sum_id 
   FROM "balance_transactions" 
   JOIN charges ON balance_transactions.source = charges.balance_id 
   WHERE "balance_transactions"."account_id" = 6 
      AND (balance_transactions.type = 'charge' 
      AND charges.refunded = false 
      AND charges.invoice IS NOT NULL) 
      AND ("balance_transactions"."created" BETWEEN '2013-12-20' AND '2014-01-19');

它往往需要一段时间才能运行,这里是EXPLAIN ANALYZE的输出:

 Aggregate  (cost=7263.70..7263.71 rows=1 width=4) (actual time=23.367..23.367 rows=1 loops=1)
   ->  Nested Loop  (cost=28.04..7262.31 rows=556 width=4) (actual time=0.403..23.247 rows=938 loops=1)
         ->  Bitmap Heap Scan on balance_transactions  (cost=28.04..1798.91 rows=754 width=22) (actual time=0.349..0.834 rows=943 loops=1)
               Recheck Cond: ((account_id = 6) AND ((type)::text = 'charge'::text) AND (created >= '2013-12-20 00:00:00'::timestamp without time zone) AND (created <= '2014-01-19 00:00:00'::timestamp without time zone))
               ->  Bitmap Index Scan on index_balance_transactions_account_type_created  (cost=0.00..27.85 rows=754 width=0) (actual time=0.327..0.327 rows=943 loops=1)
                     Index Cond: ((account_id = 6) AND ((type)::text = 'charge'::text) AND (created >= '2013-12-20 00:00:00'::timestamp without time zone) AND (created <= '2014-01-19 00:00:00'::timestamp without time zone))
         ->  Index Scan using index_charges_on_balance_id on charges  (cost=0.00..7.24 rows=1 width=18) (actual time=0.023..0.023 rows=1 loops=943)
               Index Cond: ((balance_id)::text = (balance_transactions.source)::text)
               Filter: ((NOT refunded) AND (invoice IS NOT NULL))
               Rows Removed by Filter: 0
 Total runtime: 23.788 ms
(11 rows)

那么,我可以做些什么来加快查询速度呢?

编辑:以下是这些表格的当前索引......

-- BALANCE TRANSACTIONS INDEXES --
"balance_transactions_pkey" PRIMARY KEY, btree (id)
"index_balance_transactions_account_type_created" btree (account_id, type, created)
"index_balance_transactions_on_account_id" btree (account_id)
"index_balance_transactions_on_account_id_and_item_id" btree (account_id, item_id)
"index_balance_transactions_on_plan" btree (plan)
"index_balance_transactions_on_source" btree (source)
"index_balance_transactions_on_item_id" btree (item_id)
"index_balance_transactions_on_type" btree (type)

-- CHARGES INDEXES --
"charges_pkey" PRIMARY KEY, btree (id)
"index_account_ref_created_plan" btree (account_id, refunded, created, plan)
"index_charges_on_account_id" btree (account_id)
"index_charges_on_account_id_and_amount" btree (account_id, amount)
"index_charges_on_account_id_and_amount_and_created" btree (account_id, amount, created)
"index_charges_on_account_id_and_paid_and_created" btree (account_id, paid, created)
"index_charges_on_account_id_and_balance_id" btree (account_id, balance_id)
"index_charges_on_customer" btree (customer)
"index_charges_on_invoice" btree (invoice)
"index_charges_on_is_projected" btree (is_projected)
"index_charges_on_is_projected_and_projected" btree (is_projected, projected)
"index_charges_on_parent_charge" btree (parent_charge)
"index_charges_on_plan" btree (plan)

0 个答案:

没有答案