LIMIT/SORT optimization

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: pgsql-patches <pgsql-patches(at)postgresql(dot)org>
Subject: LIMIT/SORT optimization
Date: 2007-02-07 10:49:17
Message-ID: 87bqk6tf82.fsf@stark.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches


Earlier I mentioned I had implemented the limited sort optimization. This
kicks in quite frequently on web applications that implement paging. Currently
Postgres has to sort the entire data set, often in a disk sort. If the page is
early in the result set it's a lot more efficient to just keep the top n
records in memory and do a single pass through the result set.

The patch is quite simple and is mostly localized to tuplesort.c which
provides a new function to advise it of the maximum necessary. It uses the
existing heapsort functionality which makes it easy to keep the top n records
by peeking at the top element of the heap and removing it if the new record
would displace it.

In experimenting I found heap sort about half the speed of quicksort so I made
it switch over to heap sort if the input size reached 2x the specified maximum
or if it can avoid spilling to disk by switching.

The two open issues (which are arguably the same issue) is how to get the
information down to the sort node and how to cost the plan. Currently it's a
bit of a hack: the Limit node peeks at its child and if it's a sort it calls a
special function to provide the limit.

Attachment Content-Type Size
limit-sort.patch.4 application/octet-stream 19.9 KB

Responses

Browse pgsql-patches by date

  From Date Subject
Next Message Peter Eisentraut 2007-02-07 10:57:21 Re: BUG #2977: dow doesn't conform to ISO-8601
Previous Message Adriaan van Os 2007-02-07 10:24:49 BUG #2977: dow doesn't conform to ISO-8601