Is there a way to speed up our plpgsql function that counts certain types of docs all in one query which is executed in a loop? ALL in one query?
validador := (s
It should be faster to assign all four variables in one query (only one table or index scan):
SELECT INTO validador, validador2, validador3, validador4
sum(CASE id_tipo_cifra WHEN 901 THEN 1 ELSE 0 END)
,sum(CASE id_tipo_cifra WHEN 902 THEN 1 ELSE 0 END)
,sum(CASE id_tipo_cifra WHEN 905 THEN 1 ELSE 0 END)
,sum(CASE id_tipo_cifra WHEN 907 THEN 1 ELSE 0 END)
FROM webdte.doc_tip_cifra
WHERE id_doc = id_documento;
Same result.
Normally you would have to check id_doc
for NULL
in addition, but since you have a WHERE
condition with =
on it, it cannot be NULL
.