How can I use SQL only on mysql DB to replace first and last names of real users in users table using a random name from two related tables: random_first_names and random_last_names. Our users table contains over 250K records and each of the rundome tables contain over 5000 names that should be picked at random for each record in users table. Is it possible to achieve with SQL only?
[UPDATE]
I solved the problem based on Rick's input and shared the full SQL script below.
Best Answer
If you decide that the Update is too slow, I suggest the following will be about 1000 times as fast.
Loop (can be done in a Stored Proc)...
UPDATE
the 'next' 5000 rows joined to the two random tables. UseON Users.id % 5000 = RandomFirstNames.id
(etc)End Loop
Shuffling the table (step 1 of the loop) is something like
After OP's UPDATE
Don't do
Instead, do this once:
and use
@mask_ct
instead ofcount_names
;As for the skipped ids,
CREATE TABLE _RandomFirstNames
without an id, thenALTER TABLE _RandomFirstNames ADD id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY;
to get the ids. This should give you ids without gaps (unless you are on a multi-Master cluster of any kind).