Putting aside JDBI, paging is generally better done by finding the
first element in the page and fetching the next $page-size elements
after it. As long as you have an ordered index on your sort field,
this is usually more efficient than limit/offset.
Example:
https://github.com/brianm/jdbi/blob/master/src/test/java/org/skife/jdbi/v2/docs/TestPaging.java
If you really want limit/offset, just bind the limit and offset as
params and you should be good to go.
-Brian
>
> Regards,
>
> Mark Wolfe
Any time you are paging you need an order by clause if you want
consistent ordering. If you are ordering by something, it should have
an ordered index. Whatever this field you are ordering by is, you can
use it to do the id + offset approach.
Using an offset requires the database to count forward to the offset.
Some times this is reasonably efficient, sometimes it is not -- and it
is very database dependent unfortunately. On the other hand an
(ordered) index search to a key is always log N and the sequential
read for the next set of ids (or even entries if scanning on primary
key) is going to be about as efficient as it can be made. In general,
trying to do large offsets is pretty expensive.
-Brian
Hello Brian,I totally agree with your point, but what if I really want to do a "SELECT * FROM table ORDER BY column LIMIT n,m" query?
If you normally bind something to a variable with JDBI, it would be evaluated as a string, not as the fieldname.
I found posts where something similar has been discussed and "@UseStringTemplate3StatementLocator" has beend mentioned, but I'm afraid, I don't know how to implement this.I would be very glad, if you could help :)
Am Donnerstag, 9. Februar 2012 03:49:57 UTC+1 schrieb Brian McCallister:Any time you are paging you need an order by clause if you want
consistent ordering. If you are ordering by something, it should have
an ordered index. Whatever this field you are ordering by is, you can
use it to do the id + offset approach.Using an offset requires the database to count forward to the offset.
Some times this is reasonably efficient, sometimes it is not -- and it
is very database dependent unfortunately. On the other hand an
(ordered) index search to a key is always log N and the sequential
read for the next set of ids (or even entries if scanning on primary
key) is going to be about as efficient as it can be made. In general,
trying to do large offsets is pretty expensive.-Brian
--
You received this message because you are subscribed to the Google Groups "jDBI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.