views:

163

answers:

2

i have extracted a list of usernames using the answers from this:

http://stackoverflow.com/questions/1273259/how-to-optimize-mysql-query-if-i-have-too-many-or-operators-in-my-query

now i have another issue. I have a friend table made up of just 2 columns, requestor and buddy both form a composite primary key.

Now suppose i have an array of facebook friends for a given user using the above SO question.

how do i automatically make them friends in my web app as well?

That means if i have 5 actual friends for a user called 'usera'. i need to perform 10 insert statements.

eg: friendA, usera and usera, friendA so that friendA is reflected as a friend for usera.

i understand that insert select is efficient. but unfortunately the list of usernames of these friends are obtained via a PHP array of values using IN clause as suggested by fellow SO members in my above question.

So what should i do?

should i

a) loop through the names and perform 2 insert statements for each step?

b) use insert select? but how to do it exactly?

my DB is designed this way:

1 table called users with usernames and fb_uid where the username is primary key and fb_uid is nullable.

1 table called friends with a column called requestor and another column called buddy. Both columns form a composite key.

A friendship is established between 2 people (usera, userb) in my web app when you have the following data: (usera, userb) and (userb, usera) in the friends table.

Thank you.

+1  A: 

You can always make bulk insert.

INSERT INTO friends (user_id, friend_user_id) VALUES (1, 2), (2, 1)
Harri Siirak
is there a performance issue with using bulk inserts? i am sure i cna use php and do a loop to construct a single bulk insert. should i also add in insert ignore or insert delayed?
keisimone
Bulk inserts are typically faster than regular inserts, the only downside is that there is a limit on the number of values (a thousand I believe).
Alix Axel
what if halfway through the bulk inserts i have a duplicate record? will the successful inserts before that get abandoned?
keisimone
A: 

I would go with INSERT SELECT's, you can find the detailed syntax here.

Alix Axel
are you suggesting this? even if the insert select is combined with a IN clause?insert into friends (requestor, buddy) select 'usera', distinct(username) from users where fb_uid in (value1, value2, ...)
keisimone
I don't know how your DB is designed.
Alix Axel
i edited my question and added some idea of my DB schema.
keisimone