按日期分组,查询速度慢

时间:2016-08-21 17:52:39

标签: sql postgresql

我希望将每个app按每天发生的conversations数量分组一段时间。

到目前为止,这是我的查询,但运行速度很慢,我想加快速度。有什么想法吗?

with grouping as (
    SELECT a.id, d.date, count(c.id)
    FROM (
        select to_char(date_trunc('day', (current_date - offs)), 'YYYY-MM-DD') AS date 
        FROM generate_series(0, 7) AS offs
    ) d 
    LEFT OUTER JOIN apps a on true
    LEFT OUTER JOIN conversations c ON (d.date=to_char(date_trunc('day', c.started_at), 'YYYY-MM-DD')) and a.id = c.app_id
    GROUP BY a.id, d.date
)
select 
id,
array_agg(date) as labels,
array_agg(count) as data
from grouping
group by grouping.id

更新:

解释计划:

#   QUERY PLAN
1   HashAggregate  (cost=135934.73..135937.73 rows=200 width=556) (actual time=2391.949..2392.653 rows=540 loops=1)
2     Group Key: grouping.id
3     CTE grouping
4       ->  GroupAggregate  (cost=84656.63..132964.73 rows=108000 width=25) (actual time=2206.657..2382.691 rows=4320 loops=1)
5             Group Key: a.id, (to_char(date_trunc('day'::text, ((('now'::cstring)::date - offs.offs))::timestamp with time zone), 'YYYY-MM-DD'::text))
6             ->  Merge Left Join  (cost=84656.63..126214.73 rows=540000 width=25) (actual time=2206.643..2380.041 rows=4322 loops=1)
7                   Merge Cond: (((a.id)::text = (c.app_id)::text) AND ((to_char(date_trunc('day'::text, ((('now'::cstring)::date - offs.offs))::timestamp with time zone), 'YYYY-MM-DD'::text)) = (to_char(date_trunc('day'::text, c.started_at), 'YYYY-MM-DD'::text))))
8                   ->  Sort  (cost=69275.27..70625.27 rows=540000 width=21) (actual time=80.352..81.059 rows=4320 loops=1)
9                         Sort Key: a.id, (to_char(date_trunc('day'::text, ((('now'::cstring)::date - offs.offs))::timestamp with time zone), 'YYYY-MM-DD'::text))
10                        Sort Method: quicksort  Memory: 530kB
11                        ->  Nested Loop Left Join  (cost=0.00..6782.75 rows=540000 width=21) (actual time=0.051..13.095 rows=4320 loops=1)
12                              ->  Function Scan on generate_series offs  (cost=0.00..10.00 rows=1000 width=4) (actual time=0.008..0.010 rows=8 loops=1)
13                              ->  Materialize  (cost=0.00..24.10 rows=540 width=17) (actual time=0.001..0.079 rows=540 loops=8)
14                                    ->  Seq Scan on apps a  (cost=0.00..21.40 rows=540 width=17) (actual time=0.004..0.207 rows=540 loops=1)
15                  ->  Materialize  (cost=15381.24..15870.93 rows=97938 width=28) (actual time=2103.849..2274.185 rows=97940 loops=1)
16                        ->  Sort  (cost=15381.24..15626.08 rows=97938 width=28) (actual time=2103.843..2255.284 rows=97940 loops=1)
17                              Sort Key: c.app_id, (to_char(date_trunc('day'::text, c.started_at), 'YYYY-MM-DD'::text))
18                              Sort Method: external merge  Disk: 2928kB
19                              ->  Seq Scan on conversations c  (cost=0.00..4917.38 rows=97938 width=28) (actual time=0.016..83.664 rows=97940 loops=1)
20    ->  CTE Scan on grouping  (cost=0.00..2160.00 rows=108000 width=556) (actual time=2206.660..2385.291 rows=4320 loops=1)
21  Planning time: 0.835 ms
22  Execution time: 2420.991 ms

0 个答案:

没有答案