views:

474

answers:

19

To elaborate .. a) A table (BIGTABLE) has a capacity to hold a million rows with a primary Key as the ID. (random and unique) b) What algorithm can be used to arrive at an ID that has not been used so far. This number will be used to insert another row into table BIGTABLE.

Updated the question with more details.. C) This table already has about 100 K rows and the primary key is not an set as identity. d) Currently, a random number is generated as the primary key and a row inserted into this table, if the insert fails another random number is generated. the problem is sometimes it goes into a loop and the random numbers generated are pretty random, but unfortunately, They already exist in the table. so if we re try the random number generation number after some time it works. e) The sybase rand() function is used to generate the random number.

Hope this addition to the question helps clarify some points.

A: 

If ID is purely random, there is no algorithm to find an unused ID in a similarly random fashion without brute forcing. However, as long as the bit-depth of your random unique id is reasonably large (say 64 bits), you're pretty safe from collisions with only a million rows. If it collides on insert, just try again.

bmdhacks
+1  A: 

Pick a random number, check if it already exists, if so then keep trying until you hit one that doesn't.

Edit: Or better yet, skip the check and just try to insert the row with different IDs until it works.

This is the current approach. But the loop goes on...
vicky
If ID is 32bit and you only have a million or so rows, the loop should run only once almost every time. Perhaps your random number generator is not as random as you think...
Yes , that seems to be the problem.. the loop goes on for more than 50 retries
vicky
A: 

depending on your database you might have the option of either using a sequenser (oracle) or a autoincrement (mysql, ms sql, etc). Or last resort do a select max(id) + 1 as new id - just be carefull of concurrent requests so you don't end up with the same max-id twice - wrap it in a lock with the upcomming insert statement

Per Hornshøj-Schierbeck
with lock i ment a transaction that does a table-lock or similar
Per Hornshøj-Schierbeck
yes the logic would need to have the maxID + 1 and then a check o see if that exists already, and if so, then generate another ID, check again.. etc. But is there any logic that gives me a number from the available numbers that are not used so that i dont have to insert.. get the error, try again..
vicky
+1  A: 

Make the key field UNIQUE and IDENTITY and you wont have to worry about it.

Ferruccio
This is an existing BIGTABLE, The Primary key is not an identity field.
vicky
+3  A: 

Why is the unique ID Random? Why not use IDENTITY? How was the ID chosen for the existing rows.

The simplest thing to do is probably (Select Max(ID) from BIGTABLE) and then make sure your new "Random" ID is larger than that...

EDIT: Based on the added information I'd suggest that you're screwed.

If it's an option: Copy the table, then redefine it and use an Identity Column.

If, as another answer speculated, you do need a truly random Identifier: make your PK two fields. An Identity Field and then a random number.

If you simply can't change the tables structure checking to see if the id exists before trying the insert is probably your only recourse.

theo
Uh, that would very rapidly exhaust the range of allowable ID values.
Depends on what dataType the the ID field is.
theo
Many thanks for your reply.. Some more details have been added to the question
vicky
The check is what's in place now, but the check loops many times and wastes database time and locks out other transactions waiting on this one to complete..
vicky
+2  A: 

There isn't really a good algorithm for this. You can use this basic construct to find an unused id:

int id;
do {
  id = generateRandomId();
} while (doesIdAlreadyExist(id));
doSomethingWithNewId(id);
John Meagher
+1  A: 

If this is something you'll need to do often you will probably want to maintain a live (non-db) data structure to help you quickly answer this question. A 10-way tree would be good. When the app starts it populates the tree by reading the keys from the db, and then keeps it in sync with the various inserts and deletes made in the db. So long as your app is the only one updating the db the tree can be consulted very quickly when verifying that the next large random key is not already in use.

Jeff
A: 

I've seen this done so many times before via brute force, using random number generators, and it's always a bad idea. Generating a random number outside of the db and attempting to see if it exists will put a lot strain on your app and database. And it could lead to 2 processes picking the same id.

Your best option is to use MySQL's autoincrement ability. Other databases have similar functionality. You are guaranteed a unique id and won't have issues with concurrency.

Ryan Doherty
It was a Bad idea.. done a very long time back.. stuck with it now.
vicky
A: 

It is probably a bad idea to scan every value in that table every time looking for a unique value. I think the way to do this would be to have a value in another table, lock on that table, read the value, calculate the value of the next id, write the value of the next id, release the lock. You can then use the id you read with the confidence your current process is the only one holding that unique value. Not sure how well it scales.

Alternatively use a GUID for your ids, since each newly generated GUID is supposed to be unique.

Grant Wagner
+1  A: 

First question: Is this a planned database or a already functional one. If it already has data inside then the answer by bmdhacks is correct. If it is a planned database here is the second question:
Does your primary key really need to be random? If the answer is yes then use a function to create a random id from with a known seed and a counter to know how many Ids have been created. Each Id created will increment the counter.
If you keep the seed secret (i.e., have the seed called and declared private) then no one else should be able to predict the next ID.

WolfmanDragon
vicky
A: 

Is it a requirement that the new ID also be random? If so, the best answer is just to loop over (randomize, test for existence) until you find one that doesn't exist.

If the data just happens to be random, but that isn't a strong constraint, you can just use SELECT MAX(idcolumn), increment in a way appropriate to the data, and use that as the primary key for your next record.

You need to do this atomically, so either lock the table or use some other concurrency control appropriate to your DB configuration and schema. Stored procs, table locks, row locks, SELECT...FOR UPDATE, whatever.

Note that in either approach you may need to handle failed transactions. You may theoretically get duplicate key issues in the first (though that's unlikely if your key space is sparsely populated), and you are likely to get deadlocks on some DBs with approaches like SELECT...FOR UPDATE. So be sure to check and restart the transaction on error.

Robert Sanders
No the new Id can be random. - could be sequential also. But if it's sequential, wont the index page be a hotspot when the next row is being inserted and the same index page is being locked?
vicky
+1  A: 

Your best bet is to make your key space big enough that the probability of collisions is extremely low, then don't worry about it. As mentioned, GUIDs will do this for you. Or, you can use a pure random number as long as it has enough bits.

This page has the formula for calculating the collision probability.

Jason DeFontes
+1  A: 

What's your platform? Will GUIDs work? They are guaranteed to be unique on the system.

cciotti
sybase. Table already exists, so GUID is out.
vicky
A: 

First check if Max(ID) + 1 is not taken and use that.

If Max(ID) + 1 exceeds the maximum then select an ordered chunk at the top and start looping backwards looking for a hole. Repeat the chunks until you run out of numbers (in which case throw a big error).

if the "hole" is found then save the ID in another table and you can use that as the starting point for the next case to save looping.

Turnkey
thought of storing the holes in another table and use it one by one (holes would be huge chunks), typically in sequence within the chunk.But if it's sequential, wont the index page be a hotspot when the next row is being inserted and the same index page is being locked
vicky
A: 

Skipping the reasoning of the task itself, the only algorithm that

  • will give you an ID not in the table
  • that will be used to insert a new line in the table
  • will result in a table still having random unique IDs

is generating a random number and then checking if it's already used

Orlangur
Yes, this is the current process, generate an ID and check if it's used. but this check goes on and on when the random numbers are already in the table.
vicky
+3  A: 

The question is of course: why do you want a random ID?

One case where I encountered a similar requirement, was for client IDs of a webapp: the client identifies himself with his client ID (stored in a cookie), so it has to be hard to brute force guess another client's ID (because that would allow hijacking his data).

The solution I went with, was to combine a sequential int32 with a random int32 to obtain an int64 that I used as the client ID. In 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());

The generated IDs are 'half' random, while the other 'half' guarantees you cannot obtain the same ID twice:

select lift(1, random_pos_int());  => 3108167398
select lift(2, random_pos_int());  => 4673906795
select lift(3, random_pos_int());  => 7414644984
...
Bruno De Fraine
A: 

The best algorithm in that case is to generate a random number and do a select to see if it exists, or just try to add it if your database errs out sanely. Depending on the range of your key, vs, how many records there are, this could be a small amount of time. It also has the ability to spike and isn't consistent at all.

Would it be possible to run some queries on the BigTable and see if there are any ranges that could be exploited? ie. between 100,000 and 234,000 there are no ID's yet, so we could add ID's there?

Ray Jenkins
Yes, But if we sequence the Primary key, we need to worry about the Index page being locked when multiple inserts happen to the table.
vicky
+2  A: 

A bit outside of the box.

Why not pre-generate your random numbers ahead of time? That way, when you insert a new row into bigtable, the check has already been made. That would make inserts into bigtable a constant time operation.

You will have to perform the checks eventually, but that could be offloaded to a second process that doesn’t involve the sensitive process of inserting into bigtable.

Or go generate a few billion random numbers, and delete the duplicates, then you won't have to worry for quite some time.

TonyOssa
A: 

Why not append your random number creator with the current date in seconds. This way the only way to have an identical ID is if two users are created at the same second and are given the same random number by your generator.

Lumpy