python - Is there a faster way of inserting N^2 items than using an N^2 Loop? -


you have database many foos have many bars, so:

foo --< foobar >-- bar 

you have 10 foos , 10 bars , need associate every foo every bar inserting 100 records foobar

is there more efficient way perform other following n^2 loop, or stuck this?

def associate(foos, bars):     foo in foos:         bar in bars:             # insert foobar (foo_id, bar_id) values (foo.id, bar.id) 

you need insert n^2 elements. there no way faster in n^2. thing can suggest use batch insert instead of individual inserts.