ユーザ用ツール

サイト用ツール


サイドバー

db:mysql:大量データ生成

大量データ生成

検証用の、大量のデータを生成する方法について。
検索すると、以下のような方法が紹介されていましたが、私の環境(Mysql 5.7)で試すと、オートインクリメントの値が飛び飛びになっており、検証に使えませんでした。

create table sample (
  id int primary key auto_increment,
  name varchar(10),
  created_at datetime
);

# 空データを挿入(倍々で増える)
insert into sample () values (); --1行作成
insert into sample (id) select 0 from sample; --2行になる
insert into sample (id) select 0 from sample; --4行になる
insert into sample (id) select 0 from sample; --8行になる
insert into sample (id) select 0 from sample; --16行になる

# id を確認すると歯抜けになっている
select id from sample limit 10;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  6 |
|  7 |
|  8 |
|  9 |
| 13 |
| 14 |
+----+

そこで別のサイトで紹介されていた、テーブルを単純結合する方法を試します。

create table sample (
  id int primary key auto_increment,
  name varchar(10),
  tel varchar(16),
  pref tinyint unsigned,
  address varchar(50),
  created_at datetime
);

# 元となるデータを10件作る
insert into sample (name)
 values
(''), (''), (''), (''), (''), (''), (''), (''), (''), ('');

# 単純結合して insert(結合するテーブルを増やす毎に10倍される)
insert into sample (name)
 select s1.name
 from sample s1, sample s2, sample s3, sample s4;

# 4つ結合で1万件 + 10件
select count(id) from sample;
+-----------+
| count(id) |
+-----------+
|     10010 |
+-----------+

# オートインクリメントが連番になっていることを確認
select id from sample limit 10;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
|  7 |
|  8 |
|  9 |
| 10 |
+----+

# update でランダムな値を入れてあげる
update sample set
    name = concat('山田 ', id, '郎')
    , tel = ceil(rand() * 100000000000)
    , pref = ceil(rand() * 47)
    , address = substring(md5(rand()), 1, 20)
    , created_at = addtime(concat_ws(' ','2014-01-01' + interval rand() * 1460 day, '00:00:00'), sec_to_time(floor(0 + (rand() * 86401))));

# 1万件の検証用データの作成完了
select * from sample limit 10;
+----+--------------+-------------+------+----------------------+---------------------+
| id | name         | tel         | pref | address              | created_at          |
+----+--------------+-------------+------+----------------------+---------------------+
|  1 | 山田 1郎     | 17487518972 |   30 | ada50a04459f9473fa8c | 2014-07-17 20:50:14 |
|  2 | 山田 2郎     | 93616008846 |    4 | 91dc61580389d0451618 | 2016-07-13 10:45:15 |
|  3 | 山田 3郎     | 34149720738 |   18 | 8e19ced55fb4d9c916e0 | 2017-06-21 23:09:41 |
|  4 | 山田 4郎     | 22151722128 |   10 | 9bab6059a8c00dacac59 | 2015-05-28 13:28:19 |
|  5 | 山田 5郎     | 75411782391 |    5 | 558022114ca362d88345 | 2016-05-16 10:55:18 |
|  6 | 山田 6郎     | 49514332740 |    6 | b27ceb4a935d5a5c3af9 | 2014-01-07 19:40:23 |
|  7 | 山田 7郎     | 8581452639  |   46 | fdfa1938c939cb5b421d | 2014-03-18 11:34:46 |
|  8 | 山田 8郎     | 25663569408 |   40 | 59974cf84780f1499707 | 2016-02-24 11:01:20 |
|  9 | 山田 9郎     | 68475667824 |    3 | 9fd65b6af7480d51b616 | 2016-12-17 04:11:57 |
| 10 | 山田 10郎    | 65366457092 |   35 | cd0e5758de45f586c335 | 2016-03-14 11:33:55 |
+----+--------------+-------------+------+----------------------+---------------------+

コメント

コメントを入力. Wiki文法が有効です:
 
db/mysql/大量データ生成.txt · 最終更新: 2019/03/25 13:01 by yusuke_komori