Hello. There is a table with a large number of records. It has a datetime field with the TIMESTAMP type. Grouping by date is required. I had 3 options.
GROUP BY DATE(datetime)GROUP BY UNIX\_TIMESTAMP(datetime) - UNIX\_TIMESTAMP(datetime)%(60\*60\*24)- subtract the remainder of the division by day, that is, round up to the day. The first option works ~ 2.1 seconds, the second ~ 0.95.
Then I realized that it was easier to work with the whole muscle type, and added the unix\_datetime with the INT type to the table, and wrote UNIX\_TIMESTAMP(datetime) to get rid of the transformations.
And I made the 3rd grouping: GROUP BY unix\_datetime - unix\_datetime%(60\*60\*24)
To my surprise, it works for ~ 1.25 seconds.
Indexes are on both datetime and unix_datetime.
Total we have results of performance:
GROUP BY DATE(datetime)- ~ 2.1 secGROUP BY UNIX\_TIMESTAMP(datetime) - UNIX\_TIMESTAMP(datetime)%(60\*60\*24)- ~ 0.95 sec.GROUP BY unix\_datetime - unix\_datetime%(60\*60\*24)- ~ 1.25 sec.
Can anyone explain why the second option is faster than the 3rd? Maybe someone will offer more options (grouping is required not only by date, but also by hours, weeks, months). Just test on grouping by day.