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 stocks_idx_item_id_enter_on ON "stocks" ("item_id","enter_on");
SELECT
o.item_id,
o.quantity,
o.order_on,
(SELECT
COALESCE(MAX(s.enter_on),
NULL::DATE)
FROM
stocks s
WHERE
s.enter_on <= o.order_on
AND s.item_id = o.item_id) AS include_after,
(SELECT
COALESCE(MIN(s.enter_on),
NULL::DATE)
FROM
stocks s
WHERE
s.enter_on > o.order_on
AND s.item_id = o.item_id) AS include_before
FROM
orders o