optimize migration SQL