Answer the question
In order to leave comments, you need to log in
Should I use uuid or is there something else?
Often there is a task when it is necessary to "complete" an object and especially to know its identifier before it flies to the database.
This can be achieved using uuid, but there are much more minuses than pluses:
- Performance drops when selecting and inserting
- Performance drops when JOIN
- The base grows
Therefore, the question is:
How can an id for an object be generated earlier without much harm to the base?
Answer the question
In order to leave comments, you need to log in
> How without special harm to basis it is possible to generate id for object for earlier?
SELECT nextval('name_of_sequence');
Options:
1) Generated unique id (guid)
2) Numerator, you need to store a global counter somewhere, redis / db
3) Prefixes / suffixes, each writer has his own prefix and enumerator, for example
Writer 1: 10001, 10002, 10003 .. Writer 2 :
20001, 20002, 20003...
Writer 3: 30001, 30002, 30003...
Either modulo, writer 1 only writes odd numbers, writer 2 only writes even numbers
Why know in advance? I don't imagine it.
If, after adding a new line to the database, you want to know the unique ID generated in the database, then do this
INSERT INTO table_name (column1,column2) VALUES (value1,value2) RETURNING table_column_id;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question