Efficient algorithm for Next button on a MySQL result set

Posted by David Grayson on Stack Overflow See other posts from Stack Overflow or by David Grayson
Published on 2009-07-05T00:13:09Z Indexed on 2010/05/14 18:24 UTC
Read the original article Hit count: 262

Filed under:
|
|
|
|

I have a website that lets people view rows in a table (each row is a picture). There are more than 100,000 rows. You can view different subsets of the rows, and you can view them with different sort orders. While you are viewing one of the rows, you can click the "Next" or "Previous" buttons to go the next/previous row in the list.

How would you implement the "Next" and "Previous" features of the website?

More specifically, if you have an arbitrary query that returns a list of up to 100,000+ rows, and you know some information about the current row someone is viewing, how do you determine the NEXT row efficiently?

Here is the pseudo-code of the solution I came up with when the website was young, and it worked well when there were only 1000 rows, but now that there are 100,000 rows I think it is eating up too much memory.

int nextRowId(string query, int currentRowId)
{
    array allRowIds = mysql_query(query);  // Takes up a lot of memory!
    int currentIndex = (index of currentRowId in allRowIds);  // Takes time!
    return allRowIds[currentIndex+1];
}

While you are thinking about this problem, remember that the website can store more information about the current row than just its ID (for example, the position of the current row in the result set), and this information can be used as a hint to help determine the ID of the next row.

Edit: Sorry for not mentioning this earlier, but this isn't just a static website: rows can often be added to the list, and rows can be re-ordered in the list. (Much rarer, rows can be removed from the list.) I think that I should worry about that kind of thing, but maybe you can convince me otherwise.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about efficiency