Pagination query

Pagination query

Paging queries consume lot’s of resources and are not scalable very well, should be avoided.

Oracle

1
2
3
4
SELECT ID, JSON FROM (
SELECT ROWNUM RN, ID, JSON FROM MY_BASS ORDER BY ID ASC
) AS T WHERE
RN BETWEEN ((@PageNumber - 1) * @RowspPage + 1) AND (@PageNumber * @RowspPage);

PostgreSQL and MySQL

1
2
SELECT ID, JSON FROM MY_BASS ORDER BY ID
LIMIT @RowspPage OFFSET (@PageNumber - 1) * @RowspPage);

SQL Server (2012 +)

1
2
3
SELECT ID, JSON FROM MY_BASS ORDER BY ID
OFFSET ((@PageNumber - 1) * @RowspPage) ROWS
FETCH NEXT @RowspPage ROWS ONLY;

DB2 (DB2 9.7.4 +)

1
2
SELECT ID, JSON FROM MY_BASS ORDER BY ID
LIMIT @RowspPage OFFSET (@PageNumber - 1) * @RowspPage);