Improve team migration query