I have this bundles table and each bundles can have between 0 and n items from the items table, this is my query to get the median amount of items
SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY r.count) AS median
FROM (
SELECT bundle.id, COUNT(*)
FROM items
JOIN bundle ON bundle.id=items.bundle_id
GROUP BY bundle.id
) AS r;
But this query takes too long and times out, what would be the best way to split this query into batches using postgreSQL?
bundle.idand another one onitems.bundle_id? How long does the inner SQL statement take ? How long is "too long time" ? How does the EXPLAIN PLAN look like (see: stackoverflow.com/questions/61819909/… and stackoverflow.com/questions/117262/… and Postgres query is sometimes taking a very long time