Answer the question
In order to leave comments, you need to log in
C architecture / selection from the table?
Hello!
Help, please, to solve the situation: there is a certain table, let's call it timeline , where all the events that happen to the user are written. For example, someone added him as a friend, something happened to his friends and all that kind. The table has a primary key, auto-increment - everything is classical. There is a date_created field - the date the record was created.
Now for the complications. I want to load messages when scrolling. For example, I displayed 10 messages, scrolled to the bottom - 10 more are loaded. Everything is logical and simple until events occur that fall on the same date up to a second. Those. sampling
select * from timeline where date_created<дата_последнего_сообщения_в_ленте LIMIT 10
it’s already normal not to do it - some of the messages will disappear. Sorting by the primary key cannot be done either - in the future, events for past periods can be written to the timeline table and the ID acting as order_id will lose its relevance. Answer the question
In order to leave comments, you need to log in
Why not use?:
select * from timeline where ORDER BY date_created DESC LIMIT start, 10
make an index by date_created, it’s better to store it in general in the form of unix_timestamp (or better, create a composite index id & date_created at once)
when you need to load 10 more records, send the parameters (id and date_created) of the last record (i.e. the lowest in the list of events ), and the query will look like:
SELECT * FROM timeline WHERE id > %s AND date_created <= %s ORDER BY date_created DESC, id ASC LIMIT 10
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question