Speed up plpgsql that counts doc types in a loop?

后端 未结 1 529
灰色年华
灰色年华 2021-01-25 07:28

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         


        
相关标签:
1条回答
  • 2021-01-25 08:02

    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.

    0 讨论(0)
提交回复
热议问题