What would be the best algorithm for finding an identifier that is not used from a table that has the ability to hold a million rows

Develop .. a) A table (BIGTABLE) has the ability to hold a million rows with the primary key as an identifier. (random and unique) b) What algorithm can be used to obtain an identifier that has not yet been used. This number will be used to insert another row in the BIGTABLE table.

Updated question in more detail .. C) This table already contains about 100 K rows, and the primary key is not an identifier. d) A random number is currently being created as the primary key and a row is inserted into this table if the insert produces another random number. the problem is that sometimes it goes into a loop, and the generated random numbers are quite random, but, unfortunately, they already exist in the table. so if we try the random number generation number after a while, it will work. e) The sybase rand () function is used to generate a random number.

We hope that this addition to the question will help clarify some points.

+5
source share
18 answers

, : ?

, , webapp: ( cookie), ( ).

, , , int32 int32, int64, . PostgreSQL:

CREATE FUNCTION lift(integer, integer) returns bigint AS $$
SELECT ($1::bigint << 31) + $2
$$ LANGUAGE SQL;

CREATE FUNCTION random_pos_int() RETURNS integer AS $$
select floor((lift(1,0) - 1)*random())::integer
$$ LANGUAGE sql;

ALTER TABLE client ALTER COLUMN id SET DEFAULT
lift((nextval('client_id_seq'::regclass))::integer, random_pos_int());

"" , "" , :

select lift(1, random_pos_int());  => 3108167398
select lift(2, random_pos_int());  => 4673906795
select lift(3, random_pos_int());  => 7414644984
...
+5

Random? IDENTITY? .

, , (Select Max (ID) BIGTABLE), , "Random" ID ...

. , , .

: , Identity.

: PK . , .

, , , , , .

+3

. id:

int id;
do {
  id = generateRandomId();
} while (doesIdAlreadyExist(id));
doSomethingWithNewId(id); 
+2

, , . , GUID . , .

.

+2

.

? , , . bigtable .

, , .

, .

+2

, , , , , , .

: , , .

+1

UNIQUE IDENTITY, .

+1

, , , , (-db), . 10-way tree . , , db, , db. , , db, , .

+1

: . , bmdhacks . , :
, ? , , , . Id . (.. ), .

+1

, . , - (, 64 ), . , .

0

() (mysql, ms sql ..). select max (id) + 1 - , id - upcomming statement statement

0

, , , . db , , . , 2 .

- MySQL. . concurrency.

0

, , . , , , , id, id, . , , , , . , .

GUID , GUID .

0

, ? , (, ), , .

, , SELECT MAX (idcolumn), , , .

, , - concurrency, . procs, , , SELECT... FOR UPDATE, .

, . ( , ), , , , SELECT... FOR UPDATE. .

0

, Max (ID) + 1 .

Max (ID) + 1 , , . , ( ).

"", , .

0

, ,

  • , -

, ,

0

- , , , , . , , , . .

BigTable , - , ? . 100 000 234 000 , ID?

0

Why not add a random number maker with the current date in seconds. Thus, the only way to have an identical identifier is that two users are created in one second and receive the same random number by your generator.

0
source

All Articles