I’m trying to build a mailbox where we can group the messages in x.
If you put x to 20 you’ll see messages 1-20 on the first page, opening the second page will show message 21-40 etc.
How do I efficiently query this? The best I could come up with is this:
select top 20 *
from tbl_messages
where
tnr_id not in
(
select top 40 tnr_id —20/40/60/80/…
from tbl_messages
order by dt_made desc, tnr_id desc
)
order by dt_made desc, tnr_id desc
Is there a more efficient way to do this? Databases used are SQL server, oracle & sybase.
In
Oracle:In
SQL Server 2005and above:ROW_NUMBERis supported byOracletoo, but due to implementation details is a little bit less efficient thanrownum.See this article in my blog for performance comparison:
Update:
If you can tolerate some discrepancies resulted from the concurrent updates, you can remember the last record on the current page of the client side and use it to get the next results faster: