Group harvester SQL to avoid prolonged locking of tables