How you create unique_id is a useful question, but you seem to make a counter fruitful assumption about when you create them!
My point is that you do not need to generate this unique identifier during the creation of your rows, since they are essentially independent of the inserted data.
What I am doing is pre-generating a unique identifier for future use, so I can take my own sweet time and absolutely guarantee that they are unique and there is no processing that needs to be done during insertion.
For example, I have an order table with order_id. This identifier is generated on the fly when the user enters an order, gradually 1,2,3, etc. Forever and ever. The user should not see this internal identifier.
Then I have another table - unique_ids with (order_id, unique_id). I have a regular program that runs every night that preloads this table with enough unique_id rows to cover orders that can be inserted in the next 24 hours. (If I ever receive 10,000 orders in one day, I will have a problem - but it will be a good problem!)
This approach guarantees uniqueness and distracts any processing from the insert transaction and into the batch procedure, where it does not affect the user.
Rob beer
source share