On Wed, May 4, 2022 at 7:15 PM André Hänsel <andre@xxxxxxxx> wrote:
Quick(?) question... why is there a Sort node after an Index Only Scan?
Shouldn't the index already spit out sorted tuples?
CREATE INDEX ON orders_test(shipping_date, order_id);
EXPLAIN ANALYZE SELECT
FROM orders_test
WHERE TRUE
AND shipping_date >= '2022-05-01'
AND shipping_date <= '2022-05-01'
ORDER BY order_id
LIMIT 50;
They are sorted by order_id only within sets of the same shipping_date, which is not good enough. (It would be good enough if it were smart enough to know that there is only one possible shipping date to satisfy your weird range condition.)
Cheers,
Jeff