I have a GIANT MYSQL database that contains 100,000,000 records.
Simplified, three of my columns are ID, date and time, i have several indexes over id and date ( ID , DATE , ID&DATE ) so no performance problem on join
select id, max(date) as last_record from mytable where date < "2010-01-02"
mytable
/--------------------------------\
| ID | date | time |
|--------+--------------+--------|
| 1 | 2009-01-01 | 15 |
|--------+--------------+--------|
| 1 | 2009-01-03 | 14 | <-- This
|--------+--------------+--------|
| 2 | 2009-01-01 | 18 |
|--------+--------------+--------|
| 2 | 2009-01-02 | 12 |
|--------+--------------+--------|
| 2 | 2009-01-02 | 15 | <-- and This
\--------+--------------+--------/
That results like:
/-----------------------\
| ID | last_record |
|--------+--------------|
| 1 | 2009-01-03 |
|--------+--------------|
| 2 | 2009-01-02 |
\--------+--------------/
IMPROVED QUESTION: Now i want this query to tell me about the max*time* of the records that are chosen by group by, like:
/--------------------------------\
| ID | last_record | time |
|--------+--------------+--------|
| 1 | 2009-01-03 | 14 |
|--------+--------------+--------|
| 2 | 2009-01-02 | 15 |
\--------+--------------+--------/
I need some idea to do this !
EDIT:(MORE INFORMATION) i want to know last record and the time of that record for all of my Ids