I'm having trouble figuring out an efficient way to register a big number of data in my database, using php/mysql.
I have a list of phone numbers, which are stored in a number table.
PHONE_ID | PHONE_NUMBER | COUNTRY_ID
varchar
And a table linking a phone number with a "contact list group". Let's assume it's name is link_phonegroup.
For each phone/group link, I have a parameters list.
GROUP_ID | PHONE_ID | PARAMETERS_LIST
varchar
My script should be able to, given a group_id, compute through millions of numbers and :
- retrieve the number_id associated with the number OR insert the number if it does not exist (and return the insert_id)
- update the parameters_list associated with this group/number pair and if this pair does not exist, insert it
Currently, I loop (foreach) through my numbers, and, foreach number :
SELECT PHONE_ID FROM number WHERE PHONE_NUMBER = '$number'- If it does not exist, I
INSERT INTO ...and retrieve this newly created id withmysql_insert_id() - Then, with this ID, I
SELECT PARAMETERS_LIST FROM link_phonegroup WHERE GROUPE_ID = $group_id AND PHONE_ID = $phone_id. - If it does exist, I then
UPDATEmy parameters list, and if it does not, IINSERTa new row inside mylink_phonegrouptable.
My problem, as you may imagine, is that for each of my X millions numbers, I will fire 4 queries. That is slow, inefficient, and scary.
I learned about the INSERT INTO ON DUPLICATE KEY technique (MySQL manual page). My tests were super-slow, and i gave up.
I learned about the UPDATE CASE WHEN technique (Example).
Basically, my current goal is to fire ONE query each 200-ish loop (200 is a random number here, i'll do some tests with other values), which would insert/update/retrieveid/insert_into_this_other_table/make_me_a_sandwich/and_dont_forget_coffee, in a few words do all the work, which - I HOPE ! - will be a faster and less stressfull method for the database.
Is this the good way to go ? Is this the best way to go ? And if it is, what would be the skeleton of this mecha-query-of-death ? I cannot figure out how to insert-or-retrieve the phone ID in the same request of the insert-or-update parameters_list given this phone ID in the same request of a hundreds of other similar requests ? Is this even possible ?
I hope you understand my nerves here have given up since a long time. I would be happy and thankful for any help you can give to me.
Thank you.