从PostgreSQL视图中获取性能时性能下降

时间:2018-01-10 13:34:53

标签: postgresql performance

我正在运行此查询,但性能低下。我们从视图中获取数据,但是一些如何提供低性能。

我得到解释分析

  "Aggregate  (cost=387.95..387.96 rows=1 width=0) (actual time=0.561..0.561 rows=1 loops=1)"
    "  ->  Unique  (cost=387.95..387.95 rows=1 width=36) (actual time=0.558..0.558 rows=0 loops=1)"
    "        ->  Sort  (cost=387.95..387.95 rows=1 width=36) (actual time=0.558..0.558 rows=0 loops=1)"
    "              Sort Key: at.id, at.cid, at.created_at, ps.channel"
    "              Sort Method: quicksort  Memory: 25kB"
    "              ->  Nested Loop  (cost=15.89..387.94 rows=1 width=36) (actual time=0.525..0.525 rows=0 loops=1)"
    "                    ->  Hash Join  (cost=15.78..269.20 rows=56 width=108) (actual time=0.212..0.347 rows=11 loops=1)"
    "                          Hash Cond: (at."LV" = br.id)"
    "                          ->  Nested Loop  (cost=8.47..261.68 rows=56 width=105) (actual time=0.078..0.209 rows=11 loops=1)"
    "                                Join Filter: (at."aRR" = ar.id)"
    "                                Rows Removed by Join Filter: 11"
    "                                ->  Hash Join  (cost=8.47..260.00 rows=56 width=89) (actual time=0.071..0.196 rows=11 loops=1)"
    "                                      Hash Cond: (at."Type" = at.id)"
    "                                      ->  Nested Loop  (cost=6.28..257.60 rows=56 width=90) (actual time=0.043..0.161 rows=11 loops=1)"
    "                                            Join Filter: (at."Src" = sa.id)"
    "                                            Rows Removed by Join Filter: 231"
    "                                            ->  Bitmap Heap Scan on at  (cost=6.28..252.88 rows=67 width=94) (actual time=0.026..0.109 rows=11 loops=1)"
    "                                                  Recheck Cond: (created_at > '2018-01-05 11:33:28'::timestamp without time zone)"
    "                                                  Filter: (status = 't'::text)"
    "                                                  Heap Blocks: exact=11"
    "                                                  ->  Bitmap Index Scan on created_date_ids  (cost=0.00..6.28 rows=128 width=0) (actual time=0.011..0.011 rows=12 loops=1)"
    "                                                        Index Cond: (created_at > '2018-01-05 11:33:28'::timestamp without time zone)"
    "                                            ->  Materialize  (cost=0.00..2.04 rows=10 width=28) (actual time=0.001..0.002 rows=22 loops=11)"
    "                                                  ->  Seq Scan on sa  (cost=0.00..2.03 rows=10 width=28) (actual time=0.002..0.006 rows=22 loops=1)"
    "                                      ->  Hash  (cost=2.09..2.09 rows=29 width=31) (actual time=0.018..0.018 rows=30 loops=1)"
    "                                            Buckets: 1024  Batches: 1  Memory Usage: 10kB"
    "                                            ->  Seq Scan on at  (cost=0.00..2.09 rows=29 width=31) (actual time=0.005..0.010 rows=30 loops=1)"
    "                                ->  Materialize  (cost=0.00..1.01 rows=3 width=48) (actual time=0.000..0.000 rows=2 loops=11)"
    "                                      ->  Seq Scan on ar  (cost=0.00..1.01 rows=3 width=48) (actual time=0.002..0.002 rows=2 loops=1)"
    "                          ->  Hash  (cost=6.06..6.06 rows=355 width=35) (actual time=0.122..0.122 rows=370 loops=1)"
    "                                Buckets: 1024  Batches: 1  Memory Usage: 33kB"
    "                                ->  Seq Scan on br  (cost=0.00..6.06 rows=355 width=35) (actual time=0.006..0.048 rows=370 loops=1)"
    "                    ->  Index Only Scan using prs_Src_application_at_activit_key on prs ps  (cost=0.11..2.12 rows=1 width=63) (actual time=0.015..0.015 rows=0 loops=11)"
    "                          Index Cond: ((Src_application = (sa."Name")::text) AND (at = (at."Name")::text) AND (aRR = (ar."Name")::text) AND (LV = (br."Name")::text))"
    "                          Filter: (btrim((channel)::text) = 'V'::text)"
    "                          Rows Removed by Filter: 1"
    "                          Heap Fetches: 0"
    "Planning time: 7.735 ms"
    "Execution time: 0.721 ms"


    ``` 

我们的观点看起来像

 SELECT DISTINCT at.id,
        at.cid,
        at.created_at,
        at.status,
        ps.channel
       FROM at
         JOIN sa ON sa.id = at."Src"
         JOIN at ON at.id = at."Type"
         JOIN ar ON ar.id = at."aRR"
         JOIN br ON br.id = at."LV"
         JOIN prs ps ON ps.aRR::text = ar."Name"::text AND ps.at::text = at."Name"::text AND ps.LV::text = br."Name"::text AND ps.Src_application::text = sa."Name"::text
      WHERE at.status = 't'::text and
      trim(ps.channel)= 'V' and at.created_at > '2018-01-05 11:33:28' 

此查询花费了太多时间。如何提高此查询的性能。

0 个答案:

没有答案