Your solution is better, but it's for a more important reason that mere efficiency. More importantly, the query-first method leaves a small time window when 2 or more users could be looking for the same user ID, and depending on the time-slicing in the webserver and dbms more than 1 of them could discover that the name was unused, and thus proceed to try to use it. Your method completely eliminates this race condition.