More efficiently sort small result sets

We define small as fewer than five hundred results. Arbitrarily,
I've not tested for optimality of size.

The main part of this is to insert a WHERE clause into the SQL
generated when sorting for small result sets. Then we can avoid
the NSSet lookups when looping through the result set, and rely on
the result set already being filtered to the correct result set.

This speeds things up in two ways:

- Avoid extra loops for ordered doc IDs not in result set.
- Avoid lookups inside the NSSet of results.

Fixes #7