Alex Poca Alex Poca - 7 months ago 18
SQL Question

MySQL, detect row value change on sorted index

MySQL newbie.

I have a huge database (some 33 millions rows).
Every X seconds a new row is added.

DATE_TIME is stored as primary key.
A progressive INDEX (unsigned int) is recorded. It grows only.
Of course DATE_TIME and INDEX are already sorted.

The full structure is:

DATE_TIME, INDEX, SOME_VAL1, SOME_VAL2


I need to find only the lines where INDEX changes.
What is the fastest query to obtain this?

Answer
select FIRST(date_time), index, FIRST(some_val1), FIRST(some_val2)
from YOURTABLE
group by index
Comments