How to organize the generation of a unique codes?

The problem is not only that it is impossible to generate unique codes, the problem of degeneracy. For example, you need to generate 500k codes, for example in the database is already 400K, the question is how to degenerate the necessary number of codes in less time. Not to make 400K checks to add 400 001 code.

I'm sorry for not complete. Codes can be of different lengths. The service code also sets some rules, for example, the code length and the alphabet.
April 7th 20 at 15:26
2 answers
April 7th 20 at 15:28
Use the formula: CODE=N.
Where code is the code, and N is a sequence number.
Or a more complex formula based on code from N without collisions.

Another option: make already have all available codes single 3-dimensional matrix for each symbol: symbol, position, id code.
Where there will be empty cells are free combinations (familiarity) for the new codes.
April 7th 20 at 15:30
You can try to use a GUID. It is alleged that they are unique. Ie can not check whether the same database when you insert a new one.
Well, or a unique code may be an integer. Find the maximum which is already saved in the database and begin to generate up.

Find more questions by tags Algorithms