我现在有一个类似下面的psql查询。它需要很长时间才能执行,大约1:30。我想优化这个查询,但不必对同一个查询执行多个SELECT语句。
SELECT
(SELECT COUNT(*) FROM message WHERE message.created_by_client_id = v_client.id) AS "inboundMessages",
(SELECT v_form."isReceived" FROM v_form
WHERE (v_form."client" ->> 'id')::integer = v_client."id"
AND v_form."formTypeId" = 1
LIMIT 1 ) AS "isIntakeReceived",
(SELECT v_form."isReceived" FROM v_form
WHERE (v_form."client" ->> 'id')::integer = v_client."id"
AND v_form."formTypeId" = 2
LIMIT 1 ) AS "isFollowUpReceived",
*
FROM v_client
字符串
我尝试将SELECTS重写为joint,但它们返回多行。我只需要最新的形式和由客户端创建的所有消息的计数。
1条答案
按热度按时间b4wnujal1#
试试这个,也许会有帮助:
字符串
可以帮助您的原始查询的索引:
v_form (((client ->> 'id')::integer), "formTypeId")
message (created_by_client_id)