Big MySQL Database - Advice

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
Hi,

I have a list of 10,000 mobile phone numbers, stored in the following MySQL table:

ID
Mobile_Number
KEY

As part of a competition page I'm doing, I need to create a unique KEY for each Mobile_Number. As this is a pretty big dataset, is my below logic flawed in any way and if so, what would you recommend?


  • Select * Records from a database
  • Generate a unique key via PHP
  • Check that key doesn't already exist
  • If key is unique, insert this into the database
This would all be done within a loop.

Thanky
 
Check that key doesn't already exist

^ There's your performance killer, although for 10k records it should still be pretty quick if you want to do it that way.

To skip this step you could append the ID field to the start/end of the key to guarantee uniqueness without having to check.
 
Maybe i missed something but isnt the problem that a mobile number could exist twice or more? Using Newid() would generate a different key for each of the duplicated mobile numbers, which is not what is wanted?
 
That uuid() would usually be fine, but the number is a kind of key that needs to be quickly typed in by attendants. Guess I'll have to just genrate this via PHP in my loop, which I imagine will be slower :(
 
So basically your just trying to make sure that you can't insert duplicate phone numbers? If that is the case then the only way really is to check for that phone number each time you want to insert a new one.

Or are you just trying to get the unique numbers out of the database ( 'select distinct phone_number from database' would achieve that).
 
Because 10,000 is 5 digits?

Nar, there's some sort of method you use calculate this.

So basically your just trying to make sure that you can't insert duplicate phone numbers? If that is the case then the only way really is to check for that phone number each time you want to insert a new one.

Or are you just trying to get the unique numbers out of the database ( 'select distinct phone_number from database' would achieve that).


All the phone numbers will be unique. I just need to make sure that the KEY is unique for every number.
 
Nar, there's some sort of method you use calculate this.




All the phone numbers will be unique. I just need to make sure that the KEY is unique for every number.
Why not use the phone number as the key?

also
Code:
ALTER TABLE `TableName` ADD UNIQUE (`mobile_number`)
 
Don't worry too much about duplicates, if you use a sound random number generator you won't get many.

I'd suggest adding a unique index and just catching the error if the db spits it back at you.
 
Back
Top Bottom