More performant sql query for getLastTimestamp + new metric