SQL tweaks for metrics upgrade