MAX(id) function in query with indexed condition
DATE(), not MAX() is the villain...

DATE(createdAt) &lt;= '2020-02-07' is not "sargable".

This has the equivalent effect, and can use INDEX(created_at):  created_at &lt;= '2020-02-07'.  Yes, that works for datatypesDATE,DATETIME, andTIMESTAMP`.