Skip to content

Commit

Permalink
Optimized notifications.getorder_includestatus and added indexes (#279)
Browse files Browse the repository at this point in the history
* Optimized notifications.getorder_includestatus and added indexes

* Added newline at the end of the file

---------

Co-authored-by: Henning Normann <[email protected]>
  • Loading branch information
HenningNormann and Henning Normann authored Oct 23, 2023
1 parent fa7d2ef commit 3a17466
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE INDEX IF NOT EXISTS notifications_sendersreference_creatorname ON notifications.orders (sendersreference, creatorname);
CREATE INDEX IF NOT EXISTS notifications_emailnotifications_orderid ON notifications.emailnotifications (_orderid) include (_id, result);
CREATE INDEX IF NOT EXISTS notifications_emailnotifications_result ON notifications.emailnotifications (result) include (_id, _orderid);
CREATE INDEX IF NOT EXISTS notifications_emailtexts_orderid ON notifications.emailtexts (_orderid);
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
DROP FUNCTION IF EXISTS notifications.getorder_includestatus(_alternateid UUID, _creatorname TEXT);
CREATE OR REPLACE FUNCTION notifications.getorder_includestatus(_alternateid UUID, _creatorname TEXT)
RETURNS TABLE (
alternateid UUID,
creatorname TEXT,
sendersreference TEXT,
created TIMESTAMPTZ,
requestedsendtime TIMESTAMPTZ,
processed TIMESTAMPTZ,
processedstatus orderprocessingstate,
generatedEmailCount BIGINT,
succeededEmailCount BIGINT
)
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
_target_orderid INTEGER;
_succeededEmailCount BIGINT;
_generatedEmailCount BIGINT;
BEGIN
SELECT _id INTO _target_orderid FROM notifications.orders
WHERE orders.alternateid = _alternateid AND orders.creatorname = _creatorname;

SELECT
SUM(CASE WHEN result = 'Succeeded' THEN 1 ELSE 0 END), COUNT(1) AS generatedEmailCount
INTO _succeededEmailCount, _generatedEmailCount
FROM notifications.emailnotifications
WHERE _orderid = _target_orderid;

RETURN QUERY
SELECT
orders.alternateid,
orders.creatorname,
orders.sendersreference,
orders.created,
orders.requestedsendtime,
orders.processed,
orders.processedstatus,
_generatedEmailCount,
_succeededEmailCount
FROM
notifications.orders AS orders
WHERE
orders.alternateid = _alternateid;
END;
$BODY$;

0 comments on commit 3a17466

Please sign in to comment.