Large INSERTs performance optimization
Two hints for you:


The KEY uid is redundant, because it is covered by the PRIMARY KEY
40,000 rows at a time might make for too large a transaction. Although these are very small rows (two INTs) this may cause the transaction to go to disk, depending on your settings. I usually go with around 1,000 rows at a time (I go as low as 100 and as high as 10,000).
Please try doing 40 * 1,000 and see if this works better for you than 1 * 40,000