Note that your LEFT JOIN condition is probably quite slow...

Please post EXPLAIN ANALYZE for this simplified version :

SELECT
        R."Osoba weryfikuj?ca",
        R."LP",
        A."NKA",
        A."NTA",
        Sum("Ile")
FROM            "NumeryA" A
LEFT JOIN       "Rejestr stacji do naprawy" R ON (
            A."NKA" = R."Numer kierunkowy"
        and A."NTA" like R."Numer stacji"
        and substr(A."NTA",1,5) = substr(R."Numer stacji",1,5)
)
WHERE
            A."DataPliku" >= current_date-4*30
        and A."KodB??du"=74::text
        and R."Data weryfikacji" >= current_date-4*30
GROUP BY R."Osoba weryfikuj?ca",R."LP",A."NKA", A."NTA"
ORDER BY Sum("Ile") DESC
LIMIT 5000

And also post EXPLAIN ANALYZE for this :

SELECT
        A."NKA",
        A."NTA",
        Sum("Ile") AS ss -- if it's in this table
FROM            "NumeryA" A
WHERE
            A."DataPliku" >= current_date-4*30
        and A."KodB??du"=74::text
GROUP BY A."NKA", A."NTA"

--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance

Reply via email to