Let's say I paginate like this:
SELECT article_id, article_content FROM articles ORDER BY article_rating OFFSET (page - 1) * items_per_page LIMIT items_per_page;
I have an index over (article_rating, article_id) .
My question is: what is the most effective way to find out which page the article is on if I
a) know article_id
b) know that sorting is ORDER BY article_rating?
It must be effective because I am going to make this type of request very often.
It would be even better if he not only spat out the page number, but also all the articles on this page.
So, for example, if all articles are sorted by their rating, and every ten of them are placed on another page, I want to find out on which page the article with ID 839 is located.
I am using PostgreSQL 8.4 (I am ready to upgrade if necessary).
Thanks!
EDIT:
As indicated in the comments below, my query should look something like this:
SELECT article_id, article_content FROM articles ORDER BY article_rating, article_id OFFSET (page - 1) * items_per_page LIMIT items_per_page;
source share