Postgres 13 Sort Performance
1
vote
1
answer
158
views
Sort performance on one of our query is very bad that it takes up to 14 seconds to run the query.
Here is the Query:
SELECT "stock_quant".id
FROM "stock_quant"
WHERE ((((("stock_quant"."qty" > 0.0)
AND "stock_quant"."reservation_id" IS NULL )
AND ("stock_quant"."location_id" in (34)))
AND ("stock_quant"."product_id" = 203330))
AND ("stock_quant"."company_id" = 5))
ORDER BY "stock_quant"."in_date" ,"stock_quant"."id"
limit 10;
When used Explain, this is what postgres says
explain (analyze,buffers) SELECT "stock_quant".id FROM "stock_quant" WHERE ((((("stock_quant"."qty" > 0.0) AND "stock_quant"."reservation_id" IS NULL ) AND ("stock_quant"."location_id" in (34))) AND ("stock_quant"."product_id" = 203330)) AND ("stock_quant"."company_id" = 5)) ORDER BY "stock_quant"."in_date" ,"stock_quant"."id" limit 10;
Limit (cost=0.56..4723.78 rows=10 width=12) (actual time=15754.259..15754.260 rows=0 loops=1)
Buffers: shared hit=9988201 read=94226
-> Index Scan using stock_quant_multisort_idx on stock_quant (cost=0.56..1923768.25 rows=4073 width=12) (actual time=15754.257..15754.257 rows=0 loops=1)
Filter: ((reservation_id IS NULL) AND (qty > '0'::double precision) AND (location_id = 34) AND (product_id = 203330) AND (company_id = 5))
Rows Removed by Filter: 24052667
Buffers: shared hit=9988201 read=94226
Planning Time: 0.291 ms
Execution Time: 15754.288 ms
(8 rows)
explain SELECT "stock_quant".id FROM "stock_quant" WHERE ((((("stock_quant"."qty" > 0.0) AND "stock_quant"."reservation_id" IS NULL ) AND ("stock_quant"."location_id" in (34))) AND ("stock_quant"."product_id" = 203330)) AND ("stock_quant"."company_id" = 5)) ORDER BY "stock_quant"."in_date" ,"stock_quant"."id" limit 10;
Limit (cost=0.56..4723.82 rows=10 width=12)
-> Index Scan using stock_quant_multisort_idx on stock_quant (cost=0.56..1923781.40 rows=4073 width=12)
Filter: ((reservation_id IS NULL) AND (qty > '0'::double precision) AND (location_id = 34) AND (product_id = 203330) AND (company_id = 5)) (3 rows)
And here are the indexes in the table:
"stock_quant_pkey" PRIMARY KEY, btree (id)
"stock_quant_company_id_index" btree (company_id)
"stock_quant_location_id_index" btree (location_id)
"stock_quant_lot_id_index" btree (lot_id)
"stock_quant_multisort_idx" btree (in_date, id)
"stock_quant_owner_id_index" btree (owner_id)
"stock_quant_package_id_index" btree (package_id)
"stock_quant_product_id_index" btree (product_id)
"stock_quant_product_location_index" btree (product_id, location_id, company_id, qty, in_date, reservation_id)
"stock_quant_propagated_from_id_index" btree (propagated_from_id)
"stock_quant_qty_index" btree (qty)
"stock_quant_reservation_id_index" btree (reservation_id)
Work Mem is set at **512MB**
Any idea what needs to be changed? Without sort, the same query executes in less than 200ms.
Update:
Explain Analyze without Order by
explain (analyze,buffers) SELECT "stock_quant".id FROM "stock_quant" WHERE ((((("stock_quant"."qty" > 0.0) AND "stock_quant"."reservation_id" IS NULL ) AND ("stock_quant"."location_id" in (34))) AND ("stock_quant"."product_id" = 203330)) AND ("stock_quant"."company_id" = 5)) limit 10;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.56..33.76 rows=10 width=4) (actual time=0.661..0.662 rows=0 loops=1)
Buffers: shared hit=2 read=2
-> Index Scan using stock_quant_product_location_index on stock_quant (cost=0.56..13524.04 rows=4074 width=4) (actual time=0.660..0.660 rows=0 loops=1)
Index Cond: ((product_id = 203330) AND (location_id = 34) AND (company_id = 5) AND (qty > '0'::double precision) AND (reservation_id IS NULL))
Buffers: shared hit=2 read=2
Planning:
Buffers: shared hit=248 read=16
Planning Time: 7.005 ms
Execution Time: 0.691 ms
Asked by Abul Hassan
(11 rep)
Oct 27, 2021, 03:56 AM
Last activity: Jul 12, 2025, 06:04 AM
Last activity: Jul 12, 2025, 06:04 AM