Optimized SQL queries in cron task