加速 postgres 聚合查询

问题描述

我有一个包含大约 700 万条记录的表,想知道如何加快此查询的速度:

SELECT sold_at as m,AVG(sold_price) as sold_avg,SUM(sold_price) as sold_sum,MIN(sold_price) as sold_min,MAX(sold_price) as sold_max,MEDIAN(sold_price) as sold_median
      FROM listings l
       INNER JOIN styles s ON s.id = l.style_id
      INNER JOIN addresses a on a.id = l.address_id
    WHERE municipality = 'Toronto'
           AND status_id = 3 
           AND sold_at >= '2000-01-01'
           AND sold_at < '2020-12-01' 
           AND is_rental = false
           and lower(s.name) = lower('detached house')
    group by 1
    order by 1;

运行大约需要 15 秒。我希望能够实时绘制此时间序列数据,但此查询太慢了。

这里是解释分析:

GroupAggregate  (cost=734376.47..736238.98 rows=6043 width=168) (actual time=14270.391..14487.870 rows=6539 loops=1)
  Group Key: l.sold_at
  ->  Sort  (cost=734376.47..734413.77 rows=14921 width=14) (actual time=14270.016..14291.047 rows=217907 loops=1)
        Sort Key: l.sold_at
        Sort Method: external merge  disk: 5208kB
        ->  nested Loop  (cost=8751.70..733342.07 rows=14921 width=14) (actual time=352.173..14169.688 rows=217907 loops=1)
              ->  Hash Join  (cost=8751.27..629773.16 rows=15243 width=22) (actual time=352.084..12756.543 rows=218943 loops=1)
                    Hash Cond: (l.style_id = s.id)
                    ->  Bitmap Heap Scan on listings l  (cost=8750.08..629122.23 rows=182916 width=26) (actual time=351.995..12621.513 rows=635687 loops=1)
                          Recheck Cond: ((municipality = 'Toronto'::text) AND (status_id = 3) AND (sold_at >= '2000-01-01 00:00:00-05'::timestamp with time zone) AND (sold_at < '2020-12-01 00:00:00-05'::timestamp with time zone))
                          Rows Removed by Index Recheck: 1463154
                          Filter: (NOT is_rental)
                          Rows Removed by Filter: 117250
                          Heap Blocks: exact=53497 lossy=433073
                          ->  Bitmap Index Scan on muni_status_sold_at_is_rental_idx  (cost=0.00..8704.35 rows=182916 width=0) (actual time=340.924..340.925 rows=635687 loops=1)
                                Index Cond: ((municipality = 'Toronto'::text) AND (status_id = 3) AND (sold_at >= '2000-01-01 00:00:00-05'::timestamp with time zone) AND (sold_at < '2020-12-01 00:00:00-05'::timestamp with time zone) AND (is_rental = false))
                    ->  Hash  (cost=1.18..1.18 rows=1 width=4) (actual time=0.041..0.041 rows=1 loops=1)
                          Buckets: 1024  Batches: 1  Memory Usage: 9kB
                          ->  Seq Scan on styles s  (cost=0.00..1.18 rows=1 width=4) (actual time=0.026..0.030 rows=1 loops=1)
                                Filter: (lower(name) = 'detached house'::text)
                                Rows Removed by Filter: 11
              ->  Index Only Scan using addresses_pkey on addresses a  (cost=0.43..6.79 rows=1 width=8) (actual time=0.006..0.006 rows=1 loops=218943)
                    Index Cond: (id = l.address_id)
                    Heap Fetches: 217907
Planning Time: 4.145 ms
Execution Time: 14490.964 ms

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)