优化慢速Postgres查询,其中包含>运行4秒

时间:2016-08-25 17:59:30

标签: performance postgresql postgresql-9.2

这是我的Postgres查询:

select 
    date_trunc('month', member.member_since), 
    count(member_change.member_id)
from 
    member_change 
left join 
    member on (member_change.member_id = member.member_id)
left join
    import_stats on member_change.import_history_id = import_stats.import_id
where 
    member.account_id = 1050007 and 
    change_source = 'i' and 
    change_type = 'a' and 
    member_status_id = 'a' and 
    member_since >= '2015-08-01' and 
    member_change.import_history_id in (select import_id from import_stats where account_id = 1050007) 
group by 1;

以下是对查询的解释分析

"HashAggregate  (cost=43.50..43.55 rows=4 width=16) (actual time=2.727..2.728 rows=2 loops=1)"
"  ->  Nested Loop Semi Join  (cost=18.00..43.48 rows=4 width=16) (actual time=2.089..2.717 rows=33 loops=1)"
"        Join Filter: (public.member_change.import_history_id = public.import_stats.import_id)"
"        Rows Removed by Join Filter: 79"
"        ->  Hash Right Join  (cost=18.00..33.68 rows=4 width=24) (actual time=0.121..0.715 rows=33 loops=1)"
"              Hash Cond: (public.import_stats.import_id = public.member_change.import_history_id)"
"              ->  Append  (cost=0.00..14.10 rows=411 width=8) (actual time=0.004..0.584 rows=7 loops=1)"
"                    ->  Seq Scan on import_stats  (cost=0.00..0.00 rows=1 width=8) (actual time=0.000..0.000 rows=0 loops=1)"
"                    ->  Seq Scan on import_stats  (cost=0.00..14.10 rows=410 width=8) (actual time=0.004..0.583 rows=7 loops=1)"
"              ->  Hash  (cost=17.97..17.97 rows=2 width=24) (actual time=0.108..0.108 rows=33 loops=1)"
"                    Buckets: 1024  Batches: 1  Memory Usage: 2kB"
"                    ->  Hash Join  (cost=15.72..17.97 rows=2 width=24) (actual time=0.077..0.098 rows=33 loops=1)"
"                          Hash Cond: (public.member.member_id = public.member_change.member_id)"
"                          ->  Append  (cost=0.00..2.08 rows=38 width=16) (actual time=0.024..0.038 rows=34 loops=1)"
"                                ->  Seq Scan on member  (cost=0.00..0.00 rows=1 width=16) (actual time=0.005..0.005 rows=0 loops=1)"
"                                      Filter: ((member_since >= '2015-08-01 00:00:00-05'::timestamp with time zone) AND (account_id = 1050007) AND ((member_status_id)::text = 'a'::text))"
"                                ->  Seq Scan on member  (cost=0.00..2.08 rows=37 width=16) (actual time=0.019..0.030 rows=34 loops=1)"
"                                      Filter: ((member_since >= '2015-08-01 00:00:00-05'::timestamp with time zone) AND (account_id = 1050007) AND ((member_status_id)::text = 'a'::text))"
"                                      Rows Removed by Filter: 28"
"                          ->  Hash  (cost=15.70..15.70 rows=2 width=16) (actual time=0.048..0.048 rows=61 loops=1)"
"                                Buckets: 1024  Batches: 1  Memory Usage: 3kB"
"                                ->  Append  (cost=0.00..15.70 rows=2 width=16) (actual time=0.007..0.026 rows=61 loops=1)"
"                                      ->  Seq Scan on member_change  (cost=0.00..0.00 rows=1 width=16) (actual time=0.000..0.000 rows=0 loops=1)"
"                                            Filter: ((change_source = 'i'::text) AND (change_type = 'a'::text))"
"                                      ->  Seq Scan on member_change  (cost=0.00..15.70 rows=1 width=16) (actual time=0.006..0.022 rows=61 loops=1)"
"                                            Filter: ((change_source = 'i'::text) AND (change_type = 'a'::text))"
"                                            Rows Removed by Filter: 27"
"        ->  Materialize  (cost=0.00..9.62 rows=3 width=8) (actual time=0.059..0.060 rows=3 loops=33)"
"              ->  Append  (cost=0.00..9.61 rows=3 width=8) (actual time=0.030..0.030 rows=4 loops=1)"
"                    ->  Seq Scan on import_stats  (cost=0.00..0.00 rows=1 width=8) (actual time=0.001..0.001 rows=0 loops=1)"
"                          Filter: (account_id = 1050007)"
"                    ->  Bitmap Heap Scan on import_stats  (cost=4.27..9.61 rows=2 width=8) (actual time=0.027..0.027 rows=4 loops=1)"
"                          Recheck Cond: (account_id = 1050007)"
"                          ->  Bitmap Index Scan on ix_import_stats_account_id  (cost=0.00..4.27 rows=2 width=0) (actual time=0.019..0.019 rows=4 loops=1)"
"                                Index Cond: (account_id = 1050007)"
"Total runtime: 3.447 ms"

基本上我只是想在change_source='i'处添加成员的汇总数量。 member_change表非常庞大(超过一百万条记录并计算在内)但我无法让查询得到1秒以下

0 个答案:

没有答案