Don't update the session unless it changes

And deadband session token updates to 1 per 10 seconds
Don't fetch ticket thread unless necessary
Don't generate staff statistics for AJAX requests
Periodically optimize core tables

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