So, I know that there is no such thing as "last row" on relational databases but I couldn't find a better word to explain what I want to do. I have a table that looks like the following, where id
is an AUTO_INCREMENT
field and time
is the current Unix Time Stamp. Note that I can have the same time stamp for different rows.
╔════╦════════════╦════════════╦═════════════╦════════════╗
║ id ║ field_1 ║ field_2 ║ field_n ║ time ║
╠════╬════════════╬════════════╬═════════════╬════════════╣
║ 1 ║ data_field ║ data_field ║ data_field ║ 1369748934 ║
║ 2 ║ data_field ║ data_field ║ data_field ║ 1369748935 ║
║ 3 ║ data_field ║ data_field ║ data_field ║ 1369748936 ║
║ 4 ║ data_field ║ data_field ║ data_field ║ 1369748936 ║
║ 5 ║ data_field ║ data_field ║ data_field ║ 1369748938 ║
║ 6 ║ data_field ║ data_field ║ data_field ║ 1369748939 ║
╚════╩════════════╩════════════╩═════════════╩════════════╝
What I want to do is a select that starts searching from the highest time
field and since I know it is sorted from lowest to highest I want it to stop searching when the first row does not mach my WHERE
clause because all the other rows won't match it as well. What I think MySQL does is search through all the rows but since I know my data structure and I know the location of the data I want I don't need to search through the entire table. Also if I use ORDER BY
, I believe that all the contents of my WHERE
clause will be executed first, so what I will be doing is basically search the entire row set and then order it. One important thing is I don't know how many rows will be retrieved in order to use a LIMIT
clause.
This is an example query:
SELECT fields FROM table WHERE time > timestamp
So I want to get all the results starting from a given date up to now.
Thank you very much for your help.