In case you have your own slow SQL query, you can optimize it automatically here.
For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
CREATE INDEX event_idx_id_state_sub_id ON "event" ("id","state","sub_id");
CREATE INDEX event_idx_sub_id_state ON "event" ("sub_id","state");
SELECT
*
FROM
event AS event1
LEFT JOIN
event AS event2
ON (
event1.sub_id = event2.sub_id
AND event2.state = 'PENDING'
AND event2.il_flag = event2.true
)
AND (
event1.id < event2.id
)
WHERE
(
1 = 1
AND NOT EXISTS (
SELECT
se2.id
FROM
event se2
WHERE
se1.sub_id = se2.sub_id
AND se2.state IN (
'ACCEPTED', 'FAILED', 'INPROCESS'
)
)
)
AND (
event2.id IS NULL
)