查询运行速度很慢

时间:2017-10-04 09:45:00

标签: sql postgresql query-optimization

我有这样的查询,它运行了很长时间,比如10分钟。没有解决方案来重写查询,从逻辑上讲它完全没问题。 我非常关心聚合函数,我确信它们是问题,但我不知道如何重写它们。

select
    sa.id,
    sa.s_id,
    sa.date,
    sum(pd.check_cnt) as pd_count,
    greatest(max(pd.check_date_time), sa.date::timestamp) as time
    from
        st_data_act sa
    join
        data.posdata pd
    on
        pd.id::VARCHAR = sa.id and
        pd.s_id = sa.s_id and
        pd.check_date_time > sa.date::timestamp
    group by
        sa.id,
        sa.s_id,
        sa.date;

这是一个解释计划:

HashAggregate  (cost=41273084.68..41273296.74 rows=21206 width=60)
  Group Key: sa.id, sa.s_id, sa.date
  ->  Merge Join  (cost=24152295.69..38866545.48 rows=192523136 width=60)
        Merge Cond: (((sa.id)::text = (((pd.id)::character varying)::text)) AND (sa.s_id = pd.s_id))
        Join Filter: (pd.check_date_time > sa.date)
        ->  Sort  (cost=30023.46..30553.60 rows=212058 width=44)
              Sort Key: sa.id, sa.s_id
              ->  Seq Scan on st_data_act sa  (cost=0.00..4738.58 rows=212058 width=44)
        ->  Materialize  (cost=24122272.24..24667000.01 rows=108945554 width=24)
              ->  Sort  (cost=24122272.24..24394636.12 rows=108945554 width=24)
                    Sort Key: (((pd.id)::character varying)::text), pd.s_id
                    ->  Result  (cost=0.00..2875861.53 rows=108945554 width=24)
                          ->  Append  (cost=0.00..2875861.53 rows=108945554 width=24)
                                ->  Seq Scan on posdata pd  (cost=0.00..0.00 rows=1 width=24)
                                ->  Seq Scan on posdata_201704 pd_1  (cost=0.00..94585.70 rows=3583670 width=24)
                                ->  Seq Scan on posdata_201705 pd_2  (cost=0.00..83787.47 rows=3174547 width=24)
                                ->  Seq Scan on posdata_201505 pd_3  (cost=0.00..98090.62 rows=3716462 width=24)
                                ->  Seq Scan on posdata_201506 pd_4  (cost=0.00..85579.02 rows=3242402 width=24)
                                ->  Seq Scan on posdata_201504 pd_5  (cost=0.00..100560.22 rows=3810022 width=24)
                                ->  Seq Scan on posdata_201503 pd_6  (cost=0.00..107818.47 rows=4085047 width=24)
                                ->  Seq Scan on posdata_201507 pd_7  (cost=0.00..82150.25 rows=3112525 width=24)
                                ->  Seq Scan on posdata_201508 pd_8  (cost=0.00..85627.26 rows=3244226 width=24)
                                ->  Seq Scan on posdata_201509 pd_9  (cost=0.00..84631.11 rows=3206511 width=24)
                                ->  Seq Scan on posdata_201512 pd_10  (cost=0.00..143863.01 rows=5450701 width=24)
                                ->  Seq Scan on posdata_201511 pd_11  (cost=0.00..95637.01 rows=3623501 width=24)
                                ->  Seq Scan on posdata_201510 pd_12  (cost=0.00..94878.48 rows=3594748 width=24)
                                ->  Seq Scan on posdata_201602 pd_13  (cost=0.00..94925.47 rows=3596547 width=24)
                                ->  Seq Scan on posdata_201601 pd_14  (cost=0.00..85699.73 rows=3246973 width=24)
                                ->  Seq Scan on posdata_201603 pd_15  (cost=0.00..97823.51 rows=3706351 width=24)
                                ->  Seq Scan on posdata_201604 pd_16  (cost=0.00..96419.01 rows=3653101 width=24)
                                ->  Seq Scan on posdata_201606 pd_17  (cost=0.00..81189.96 rows=3076096 width=24)
                                ->  Seq Scan on posdata_201605 pd_18  (cost=0.00..85859.40 rows=3253040 width=24)
                                ->  Seq Scan on posdata_201607 pd_19  (cost=0.00..79150.74 rows=2998874 width=24)
                                ->  Seq Scan on posdata_201608 pd_20  (cost=0.00..76012.86 rows=2879986 width=24)
                                ->  Seq Scan on posdata_201609 pd_21  (cost=0.00..89201.79 rows=3379679 width=24)
                                ->  Seq Scan on posdata_201610 pd_22  (cost=0.00..96044.10 rows=3638910 width=24)
                                ->  Seq Scan on posdata_201611 pd_23  (cost=0.00..96867.08 rows=3670108 width=24)
                                ->  Seq Scan on posdata_201612 pd_24  (cost=0.00..148607.53 rows=5630453 width=24)
                                ->  Seq Scan on posdata_201701 pd_25  (cost=0.00..82898.74 rows=3140874 width=24)

0 个答案:

没有答案