-2
GENERATING UNIQUE PRIMARY APPLICATION KEYS
I would like to get tips on how to generate primary keys with low risk of collision and that do not reduce the performance of Postgresql operations.
Explaining the problem:
Assuming I have three models: Customer, Product and User In a given application I would like to join all these models in the same data table and be able to search through the primary key, but for this each record must have a unique identifier. In this case, I had already thought of using UUID, but they are 128 bits and this ends up damaging the performance of SGDB Postgresql. That’s why I’m here looking for an idea that’s better than the one I’ve already had. I don’t need a universal exclusico identifier, but one that is unique only within the application.
I thought to use, with low possibility of collision:
import uuid
import base64
def get_pk():
pk = str(base64.b64encode(uuid.uuid4().bytes))[2:-3]
return pk
Already I thank you for any hint!
Which version of postgres you are using?
– Danizavtz
The pseudo type SERIAL meets its requirement?
– Augusto Vasques
@Danizavtz I use version 12
– Paulo Lobo
@Augustovasques does not satisfy, because I already use SERIAL in the tables separately. When I join the tables collisions occur. It is logical that I could define different ranges of values for each table, but I am just seeking some more Pitonic method to solve the problem.
– Paulo Lobo
See this example if it helps you: http://sqlfiddle.com/#! 17/163f5/2 if a sequenced to create the keys, with each insert, regardless of which table, a new key is generated.
– Augusto Vasques
Good alternative, @Augustovasques. I will evaluate the alternatives, considering safety, performance and likelihood of collisions, but thank you so much for your cooperation.
– Paulo Lobo