I have a web application in which I show a series of messages based on this table schema (there are thousands of rows like this and other columns too (deleted as not required for this question)): -
+---------+----------+----------+ | ID | COL1 | COL2 | +---------+----------+----------+ | 1 | NULL | ---- | | 2 | --- | NULL | | 3 | NULL | ---- | | 4 | --- | NULL | | 5 | NULL | NULL | | 6 | --- | NULL | | 7 | NULL | ---- | | 8 | --- | NULL | +---------+----------+----------+
And I use this query: -
SELECT * from `TABLE` WHERE `COL1` IS NOT NULL AND `COL2` IS NULL ORDER BY `COL1`;
And the result of the set is obtained as follows: -
+---------+----------+----------+ | ID | COL1 | COL2 | +---------+----------+----------+ | 12 | --- | NULL | | 1 | --- | NULL | | 6 | --- | NULL | | 8 | --- | NULL | | 11 | --- | NULL | | 13 | --- | NULL | | 5 | --- | NULL | | 9 | --- | NULL | | 17 | --- | NULL | | 21 | --- | NULL | | 23 | --- | NULL | | 4 | --- | NULL | | 32 | --- | NULL | | 58 | --- | NULL | | 61 | --- | NULL | | 43 | --- | NULL | +---------+----------+----------+
Note that the identifier column is mixed using the order by clause.
I have the correct indexes to optimize these queries. Now let me explain the real problem. In my web application, I have a lazy function. So, I show about 10 posts per page, using LIMIT 10 after the request for the first page.
We are good so far. But the real problem arises when I have to load a second page. What am I requesting now? I do not want messages to be repeated. And there are new messages that appear almost every 15 seconds, which makes them go on top (top literally means the first line) of the result set (I do not want to display these last messages on the second or third page, but they change so I can not use LIMIT 10,10 for the second page, and so on, as the messages will be repeated.).
Now, all I know is the last identifier of the posted message. Say 21 here. So, I want to display messages ID 23,4,32,58,61,43 (see the table of results above). Now load all the lines without using the LIMIT and display the 10 identifiers that appear after id 21 . But for this I will have to interact with thousands of useless strings. But I canβt use the LIMIT for the 2nd, 3rd ... pages, which is for sure. Also, the ids are mixed, so I definitely can't use WHERE ID>... So where are we going now?