回答編集履歴

1

sample

2018/11/19 06:01

投稿

yambejp
yambejp

スコア114962

test CHANGED
@@ -1,3 +1,117 @@
1
1
  プロシージャをつかって処理しますが、そもそもidがユニークじゃないですが大丈夫ですか?
2
2
 
3
3
  なんらかのプライマリーキーが必要そうなテーブルに見えます
4
+
5
+
6
+
7
+ # sample
8
+
9
+ ```SQL
10
+
11
+ create table test_table(ID int primary key auto_increment, Name varchar(10),CreatedDate datetime, CreatedBy varchar(10), LastUpdatedDate datetime, LastUpdatedBy varchar(10));
12
+
13
+ alter table test_table auto_increment = 100000007;
14
+
15
+ ```
16
+
17
+ 以下、投入のしかたは2つ考えられますが、圧倒的に後者の方が速いです
18
+
19
+ (私の環境だと100倍以上速かったです)
20
+
21
+
22
+
23
+ (1)単純な入力
24
+
25
+ ```SQL
26
+
27
+ drop procedure if exists test_procedure1;
28
+
29
+ delimiter //
30
+
31
+ create procedure test_procedure1()
32
+
33
+ begin
34
+
35
+ declare i int default 0;
36
+
37
+ while i<1000 do
38
+
39
+ set @sql="insert ignore into test_table(Name,CreatedDate,CreatedBy,LastUpdatedDate,LastUpdatedBy) ";
40
+
41
+ set @sql=concat(@sql,"values('テスト', now(), '佐藤', now(), '佐藤')");
42
+
43
+ prepare stmt from @sql;
44
+
45
+ execute stmt;
46
+
47
+ set i=i+1;
48
+
49
+ end while;
50
+
51
+ end
52
+
53
+ //
54
+
55
+ delimiter ;
56
+
57
+ call test_procedure1
58
+
59
+ ```
60
+
61
+
62
+
63
+ (2)まとめて入力
64
+
65
+ ```SQL
66
+
67
+ drop procedure if exists test_procedure2;
68
+
69
+ delimiter //
70
+
71
+ create procedure test_procedure2()
72
+
73
+ begin
74
+
75
+ declare i int default 0;
76
+
77
+ set @sql="insert ignore into test_table(Name,CreatedDate,CreatedBy,LastUpdatedDate,LastUpdatedBy) ";
78
+
79
+ while i<1000 do
80
+
81
+ if i=0 then
82
+
83
+ set @sql=concat(@sql,"values");
84
+
85
+ else
86
+
87
+ set @sql=concat(@sql,",");
88
+
89
+ end if;
90
+
91
+ set @sql=concat(@sql,"('テスト', now(), '佐藤', now(), '佐藤')");
92
+
93
+ set i=i+1;
94
+
95
+ end while;
96
+
97
+ prepare stmt from @sql;
98
+
99
+ execute stmt;
100
+
101
+ end
102
+
103
+ //
104
+
105
+ delimiter ;
106
+
107
+ call test_procedure2
108
+
109
+ ```
110
+
111
+
112
+
113
+ ただし、後者のように膨大な文字列を処理する場合はメモリなどの制約もあるので
114
+
115
+ 経験上1万程度を上限と考えたほうがよいでしょう。
116
+
117
+ 例えば100万件のデータを投入する場合は、1万件のデータ投入を100回やるイメージです