Optimize upsert select statement to use indexes when better