I have a table users (username, email, validts, confirmed.
I want add 1 billion unique rows
I tried to do this but it only adds 16 unique values
INSERT INTO users (username, email, validts, confirmed)
SELECT
CONCAT('user', n) AS username,
CONCAT('user', n, '@example.com') AS email,
UNIX_TIMESTAMP() + 2592000 AS validts,
0 AS confirmed
FROM
(SELECT @n := @n + 1 AS n FROM (SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3) t1, (SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 10000) t2, (SELECT @n := 0) t3) numbers
LIMIT 1000000;
2
Answers
in MySQL 5.7 this would work
but this will take very long time. you may try to move
START TRANSACTION
beforeWHILE
loop, so MySQL will do one big transaction instead of 1 billion of small ones. That will speed it up a little bit, but in case of failure or Ctrl-C it will rollback all the inserted lines. Also disabling indexes on the table and disabling binlog would help a little bit. Beware, disabling binlog will break replication, if it takes place.In more recent version you might be able to achieve this with recursive CTE. You generate CTE recursively until reaching 1 billion, and then insert into table from it. I think this should be faster.
You might try this technique to generate Ns (and you can add other columns as you need):
You need to continue with the pattern to T9; I don’t have any facility to test how long it takes, and you may need to perform some type-casting if the value of N overflows.