Optimize set_targets command for large DBs