Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 什么索引有助于加快查询速度?_Sql_Performance_Postgresql - Fatal编程技术网

Sql 什么索引有助于加快查询速度?

Sql 什么索引有助于加快查询速度?,sql,performance,postgresql,Sql,Performance,Postgresql,我有以下查询,目前运行大约需要450毫秒: 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.a

我有以下查询,目前运行大约需要450毫秒:

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;
这里有没有其他索引可以用来加快查询速度

以下是
EXPLAIN ANALYZE
的输出:

 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
   ->  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)
         ->  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 >= '2013-01-20 00:00:00'::timestamp without time zone) AND (created <= '2014-02-19 23:59:59.999999'::timestamp without time zone))
               ->  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 >= '2013-01-20 00:00:00'::timestamp without time zone) AND (created <= '2014-02-19 23:59:59.999999'::timestamp without time zone))
         ->  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
               ->  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)

重新格式化查询后,应将创建的
上的条件重新写入:

SELECT b.created, fee, c.plan 
FROM   stripe_charges              c
JOIN   stripe_balance_transactions b ON b.source = c.stripe_id 
WHERE  c.refunded = FALSE
AND    c.invoice IS NOT NULL
AND    b.account_id = 93 
AND    b.type = 'charge'
AND   b.created >= '2013-01-20 00:00'
AND   b.created <  '2014-02-20 00:00'
ORDER  BY b.created;
但这是一个不知道所有细节的暗中刺杀。从昨天起,关于DBA.SE的相关答案,请参阅关于<强>覆盖索引< /强>:
的更多信息和链接


正如他的评论:测试是否所有索引都被实际使用。您可以检查pg_stat_user_index
,或者如果您应该使用pgAdmin,请单击“统计”选项卡(右上角)以获取索引或表的索引集合。

您是否在stripe_charges.stripe_ip上有索引?试试一个(s_b_t.account_id,s_b_t.type,s_b_t.created)。@bma:我确实已经有了这两个索引。哇,它们是alotta索引!您可能希望定期访问这些表的pg_stat_user_索引,并查看哪些索引未被使用。出于好奇,您最近是否对条带余额交易运行了
真空分析
。source=条带费用。条带id
条带余额交易的数据类型是什么。source
条带费用。条带id
?简而言之:请将表定义添加到您的问题中。获取正确的索引是一个尝试和测试的练习,您经常使用移动的目标。考虑到这一点,您可以尝试在stripe\u charges.returned=false)和(stripe\u charges.invoice不为NULL)上添加一个部分索引。请参见以下内容:创建索引stripe\u charges\u stripe\u id\u在stripe\u charges(stripe\u id)上过滤,其中returned=false,invoice不为NULL;
SELECT b.created, fee, c.plan 
FROM   stripe_charges              c
JOIN   stripe_balance_transactions b ON b.source = c.stripe_id 
WHERE  c.refunded = FALSE
AND    c.invoice IS NOT NULL
AND    b.account_id = 93 
AND    b.type = 'charge'
AND   b.created >= '2013-01-20 00:00'
AND   b.created <  '2014-02-20 00:00'
ORDER  BY b.created;
CREATE INDEX foo_idx ON stripe_charges (stripe_id, fee, plan)
WHERE refunded = FALSE
AND   invoice IS NOT NULL;

CREATE INDEX bar_idx ON stripe_balance_transactions (account_id, source, created)
WHERE  type = 'charge';