Skip site navigation (1) Skip section navigation (2)

Optimizing query

From: pasman pasmański <pasman(dot)p(at)gmail(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Optimizing query
Date: 2010-11-24 14:48:43
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-performance

I have a query which works a bit slow.

It's runned on desktop computer: AMD Athlon X2 2GHz , Win Xp sp2, 1GB ram.
Postgres 8.4.5 with some changes in config:

shared_buffers = 200MB                  # min 128kB
                               # (change requires restart)
temp_buffers = 8MB                      # min 800kB
work_mem = 12MB                         # min 64kB
maintenance_work_mem = 32MB             # min 1MB

Indexes in table "NumeryA":
"NTA", "NKA", "KodBłędu", "Plik"        primary key
"DataPliku", "KodBłędu" index dp_kb
"NKA", "NTA"                        index nka_nta

Indexes in table "Rejestr stacji do naprawy":
"LP"    - primary key
"Numer kierunkowy", substr("Numer stacji"::text, 1, 5)  - index "3"
"Data weryfikacji"      - index "Data weryfikacji_1"
"Numer kierunkowy", "Numer stacji", "Data odrzucania bilingu z
Serat"  - index "Powtórzenia"

Query is:
 Min("PołączeniaMin") || ',' || Max("PołączeniaMax") AS "Biling",
 Sum("Ile")::text AS "Ilość CDR",
 R."LP"::text AS "Sprawa",
 (R."Osoba weryfikująca") AS "Osoba",
 to_char(min("Wartość"),'FM9999990D00') AS "Wartość po kontroli",
 max(R."Kontrola po naprawie w Serat - CDR")::text AS "CDR po kontroli",
 min(A."KodBłędu")::text AS KodBłędu,
 Max(to_char(R."Data kontroli",'YYYY-MM-DD')) AS "Ostatnia Kontrola"
, max("Skutek wprowadzenia błednej ewidencji w Serat") as "Skutek"
, sum(www.a_biling_070("NRB"))::text
, sum(www.a_biling_darmowy("NRB"))::text
 (SELECT "NumeryA".*
  FROM ONLY "NumeryA"
  WHERE "DataPliku" >= current_date-4*30 and "KodBłędu"=74::text
 ) AS A
 (SELECT * FROM "Rejestr stacji do naprawy"
  WHERE "Data weryfikacji" >= current_date-4*30
 ) AS R
 A."NKA" = R."Numer kierunkowy"
 and substr(A."NTA",1,5) = substr(R."Numer stacji",1,5)
 and A."NTA" like R."Numer stacji"
GROUP BY R."Osoba weryfikująca",R."LP",A."NKA", A."NTA"
LIMIT 5000
Explain analyze:

"Limit  (cost=30999.84..31012.34 rows=5000 width=149) (actual
time=7448.483..7480.094 rows=5000 loops=1)"
"  ->  Sort  (cost=30999.84..31073.19 rows=29341 width=149) (actual
time=7448.475..7459.663 rows=5000 loops=1)"
"        Sort Key: (sum("NumeryA"."Ile"))"
"        Sort Method:  top-N heapsort  Memory: 1488kB"
"        ->  GroupAggregate  (cost=11093.77..29050.46 rows=29341
width=149) (actual time=4700.654..7377.762 rows=14225 loops=1)"
"              ->  Sort  (cost=11093.77..11167.12 rows=29341
width=149) (actual time=4699.587..4812.776 rows=46732 loops=1)"
"                    Sort Key: "Rejestr stacji do naprawy"."Osoba
weryfikująca", "Rejestr stacji do naprawy"."LP", "NumeryA"."NKA",
"                    Sort Method:  quicksort  Memory: 9856kB"
"                    ->  Merge Left Join  (cost=8297.99..8916.58
rows=29341 width=149) (actual time=2931.449..3735.876 rows=46732
"                          Merge Cond: ((("NumeryA"."NKA")::text =
("Rejestr stacji do naprawy"."Numer kierunkowy")::text) AND
((substr(("NumeryA"."NTA")::text, 1, 5)) = (substr(("Rejestr stacji do
naprawy"."Numer stacji")::text, 1, 5))))"
"                          Join Filter: (("NumeryA"."NTA")::text ~~
("Rejestr stacji do naprawy"."Numer stacji")::text)"
"                          ->  Sort  (cost=6062.18..6135.53 rows=29341
width=95) (actual time=2131.297..2241.303 rows=46694 loops=1)"
"                                Sort Key: "NumeryA"."NKA",
(substr(("NumeryA"."NTA")::text, 1, 5))"
"                                Sort Method:  quicksort  Memory: 7327kB"
"                                ->  Bitmap Heap Scan on "NumeryA"
(cost=1502.09..3884.98 rows=29341 width=95) (actual
time=282.570..1215.355 rows=46694 loops=1)"
"                                      Recheck Cond: (("DataPliku" >=
(('now'::text)::date - 120)) AND (("KodBłędu")::text = '74'::text))"
"                                      ->  Bitmap Index Scan on dp_kb
(cost=0.00..1494.75 rows=29341 width=0) (actual time=281.991..281.991
rows=46694 loops=1)"
"                                            Index Cond: (("DataPliku"
>= (('now'::text)::date - 120)) AND (("KodBłędu")::text =
"                          ->  Sort  (cost=2235.82..2285.03 rows=19684
width=64) (actual time=800.101..922.463 rows=54902 loops=1)"
"                                Sort Key: "Rejestr stacji do
naprawy"."Numer kierunkowy", (substr(("Rejestr stacji do
naprawy"."Numer stacji")::text, 1, 5))"
"                                Sort Method:  quicksort  Memory: 3105kB"
"                                ->  Seq Scan on "Rejestr stacji do
naprawy"  (cost=0.00..831.88 rows=19684 width=64) (actual
time=2.118..361.463 rows=19529 loops=1)"
"                                      Filter: ("Data weryfikacji" >=
(('now'::text)::date - 120))"
"Total runtime: 7495.697 ms"

How to make it faster ?



pgsql-performance by date

Next:From: Markus SchulzDate: 2010-11-24 18:37:07
Subject: problem with from_collapse_limit and joined views
Previous:From: Kevin GrittnerDate: 2010-11-24 14:46:29
Subject: Re: Performance under contention

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group