Periodically optimize core tables

This is instead of optimizing the lock table for every cron run, which
wastes a lot of db server io