MySQL小ネタ／複数行インサート時のLAST_INSERT_ID()

# 1. お題

PRIMARY KEYがAUTO_INCREMENTな親テーブルと子テーブルのレコードを外部キーの関連付けをしつつまとめてINSERTしたいとき、LAST_INSERT_ID()関数を使いますが、複数行インサートではどうするのか？というお話です。

# 2. 答え

ちゃんとマニュアルに書いてあります。ちょっと読み取りづらいですが。

 * **[14.6.5.2 構成可能な InnoDB の自動インクリメントロック](https://dev.mysql.com/doc/refman/5.6/ja/innodb-auto-increment-configurable.html)（MySQL 5.6 リファレンスマニュアル）**

**「innodb_autoinc_lock_mode」**が**「0」**または**「1」（デフォルト）**の場合、

 * LAST_INSERT_ID()は、（直前に）複数行インサートした最初の行のAUTO_INCREMENT値を返す
 * 複数行インサートした単位で連番が振られる

ということでした。

# 3. 実験

ここまでの話は英語のQ&Aサイトでいくつか見つかるので答えは見つけやすいのですが、本当にそうなるのか、実験してみます。

親テーブルへの複数行インサートの次に子テーブルへの複数行インサートをするときに、**「LAST_INSERT_ID()+【先頭行からのオフセット】」**を使って連番を振ります。

```mysql:テーブル定義
mysql> USE bulk_test;
Database changed
mysql> SHOW CREATE TABLE test1\G
*************************** 1. row ***************************
       Table: test1
Create Table: CREATE TABLE `test1` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `val1` int(11) NOT NULL,
  `val2` int(11) NOT NULL,
  `str` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=324501 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> SHOW CREATE TABLE test2\G
*************************** 1. row ***************************
       Table: test2
Create Table: CREATE TABLE `test2` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `key` int(11) NOT NULL,
  `val1` int(11) NOT NULL,
  `val2` int(11) NOT NULL,
  `str` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=324501 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
```

```bash:複数行インサート用SQLファイル1
$ cat bulk_test1.sql
SET AUTOCOMMIT=0;
BEGIN;
INSERT INTO bulk_test.test1 (val1,val2,str) VALUES (1,1,REPEAT('_',138)),(1,2,REPEAT('_',180)),(1,3,REPEAT('_',24)),(1,4,REPEAT('_',75)),(1,5,REPEAT('_',197)),(1,6,REPEAT('_',99)),(1,7,REPEAT('_',104)),(1,8,REPEAT('_',30)),(1,9,REPEAT('_',103)),(1,10,REPEAT('_',119)),(1,11,REPEAT('_',121)),(1,12,REPEAT('_',46)),(1,13,REPEAT('_',45)),(1,14,REPEAT('_',45)),(1,15,REPEAT('_',36)),(1,16,REPEAT('_',151)),(1,17,REPEAT('_',5)),(1,18,REPEAT('_',200)),(1,19,REPEAT('_',112)),(1,20,REPEAT('_',97));
INSERT INTO bulk_test.test2 (`key`,val1,val2,str) VALUES (LAST_INSERT_ID(),1,1,REPEAT('_',138)),(LAST_INSERT_ID()+1,1,2,REPEAT('_',180)),(LAST_INSERT_ID()+2,1,3,REPEAT('_',24)),(LAST_INSERT_ID()+3,1,4,REPEAT('_',75)),(LAST_INSERT_ID()+4,1,5,REPEAT('_',197)),(LAST_INSERT_ID()+5,1,6,REPEAT('_',99)),(LAST_INSERT_ID()+6,1,7,REPEAT('_',104)),(LAST_INSERT_ID()+7,1,8,REPEAT('_',30)),(LAST_INSERT_ID()+8,1,9,REPEAT('_',103)),(LAST_INSERT_ID()+9,1,10,REPEAT('_',119)),(LAST_INSERT_ID()+10,1,11,REPEAT('_',121)),(LAST_INSERT_ID()+11,1,12,REPEAT('_',46)),(LAST_INSERT_ID()+12,1,13,REPEAT('_',45)),(LAST_INSERT_ID()+13,1,14,REPEAT('_',45)),(LAST_INSERT_ID()+14,1,15,REPEAT('_',36)),(LAST_INSERT_ID()+15,1,16,REPEAT('_',151)),(LAST_INSERT_ID()+16,1,17,REPEAT('_',5)),(LAST_INSERT_ID()+17,1,18,REPEAT('_',200)),(LAST_INSERT_ID()+18,1,19,REPEAT('_',112)),(LAST_INSERT_ID()+19,1,20,REPEAT('_',97));
INSERT INTO bulk_test.test1 (val1,val2,str) VALUES (2,1,REPEAT('_',97)),(2,2,REPEAT('_',142)),(2,3,REPEAT('_',72)),(2,4,REPEAT('_',101));
INSERT INTO bulk_test.test2 (`key`,val1,val2,str) VALUES (LAST_INSERT_ID(),2,1,REPEAT('_',97)),(LAST_INSERT_ID()+1,2,2,REPEAT('_',142)),(LAST_INSERT_ID()+2,2,3,REPEAT('_',72)),(LAST_INSERT_ID()+3,2,4,REPEAT('_',101));
COMMIT;
BEGIN;
INSERT INTO bulk_test.test1 (val1,val2,str) VALUES (3,1,REPEAT('_',103)),(3,2,REPEAT('_',178)),(3,3,REPEAT('_',23)),(3,4,REPEAT('_',15)),(3,5,REPEAT('_',72)),(3,6,REPEAT('_',25)),(3,7,REPEAT('_',6)),(3,8,REPEAT('_',51)),(3,9,REPEAT('_',177)),(3,10,REPEAT('_',14)),(3,11,REPEAT('_',188)),(3,12,REPEAT('_',71)),(3,13,REPEAT('_',138)),(3,14,REPEAT('_',152)),(3,15,REPEAT('_',20)),(3,16,REPEAT('_',105)),(3,17,REPEAT('_',33)),(3,18,REPEAT('_',52));
INSERT INTO bulk_test.test2 (`key`,val1,val2,str) VALUES (LAST_INSERT_ID(),3,1,REPEAT('_',103)),(LAST_INSERT_ID()+1,3,2,REPEAT('_',178)),(LAST_INSERT_ID()+2,3,3,REPEAT('_',23)),(LAST_INSERT_ID()+3,3,4,REPEAT('_',15)),(LAST_INSERT_ID()+4,3,5,REPEAT('_',72)),(LAST_INSERT_ID()+5,3,6,REPEAT('_',25)),(LAST_INSERT_ID()+6,3,7,REPEAT('_',6)),(LAST_INSERT_ID()+7,3,8,REPEAT('_',51)),(LAST_INSERT_ID()+8,3,9,REPEAT('_',177)),(LAST_INSERT_ID()+9,3,10,REPEAT('_',14)),(LAST_INSERT_ID()+10,3,11,REPEAT('_',188)),(LAST_INSERT_ID()+11,3,12,REPEAT('_',71)),(LAST_INSERT_ID()+12,3,13,REPEAT('_',138)),(LAST_INSERT_ID()+13,3,14,REPEAT('_',152)),(LAST_INSERT_ID()+14,3,15,REPEAT('_',20)),(LAST_INSERT_ID()+15,3,16,REPEAT('_',105)),(LAST_INSERT_ID()+16,3,17,REPEAT('_',33)),(LAST_INSERT_ID()+17,3,18,REPEAT('_',52));
INSERT INTO bulk_test.test1 (val1,val2,str) VALUES (4,1,REPEAT('_',146)),(4,2,REPEAT('_',5)),(4,3,REPEAT('_',147));
INSERT INTO bulk_test.test2 (`key`,val1,val2,str) VALUES (LAST_INSERT_ID(),4,1,REPEAT('_',146)),(LAST_INSERT_ID()+1,4,2,REPEAT('_',5)),(LAST_INSERT_ID()+2,4,3,REPEAT('_',147));
SET AUTOCOMMIT=1;
INSERT INTO bulk_test.test1 (val1,val2,str) VALUES (5,1,REPEAT('_',188));
INSERT INTO bulk_test.test2 (`key`,val1,val2,str) VALUES (LAST_INSERT_ID(),5,1,REPEAT('_',188));
INSERT INTO bulk_test.test1 (val1,val2,str) VALUES (6,1,REPEAT('_',118)),(6,2,REPEAT('_',165));
INSERT INTO bulk_test.test2 (`key`,val1,val2,str) VALUES (LAST_INSERT_ID(),6,1,REPEAT('_',118)),(LAST_INSERT_ID()+1,6,2,REPEAT('_',165));
SET AUTOCOMMIT=0;
BEGIN;
INSERT INTO bulk_test.test1 (val1,val2,str) VALUES (7,1,REPEAT('_',10));
INSERT INTO bulk_test.test2 (`key`,val1,val2,str) VALUES (LAST_INSERT_ID(),7,1,REPEAT('_',10));
INSERT INTO bulk_test.test1 (val1,val2,str) VALUES (8,1,REPEAT('_',63)),(8,2,REPEAT('_',105)),(8,3,REPEAT('_',67)),(8,4,REPEAT('_',33)),(8,5,REPEAT('_',147)),(8,6,REPEAT('_',190)),(8,7,REPEAT('_',152)),(8,8,REPEAT('_',48)),(8,9,REPEAT('_',137)),(8,10,REPEAT('_',121)),(8,11,REPEAT('_',52)),(8,12,REPEAT('_',168)),(8,13,REPEAT('_',6)),(8,14,REPEAT('_',60)),(8,15,REPEAT('_',75)),(8,16,REPEAT('_',172)),(8,17,REPEAT('_',57)),(8,18,REPEAT('_',185)),(8,19,REPEAT('_',176)),(8,20,REPEAT('_',96)),(8,21,REPEAT('_',182)),(8,22,REPEAT('_',111)),(8,23,REPEAT('_',58)),(8,24,REPEAT('_',65)),(8,25,REPEAT('_',63)),(8,26,REPEAT('_',96)),(8,27,REPEAT('_',20)),(8,28,REPEAT('_',166)),(8,29,REPEAT('_',146)),(8,30,REPEAT('_',93)),(8,31,REPEAT('_',18)),(8,32,REPEAT('_',21)),(8,33,REPEAT('_',46)),(8,34,REPEAT('_',135)),(8,35,REPEAT('_',188)),(8,36,REPEAT('_',196)),(8,37,REPEAT('_',137)),(8,38,REPEAT('_',42)),(8,39,REPEAT('_',144)),(8,40,REPEAT('_',145)),(8,41,REPEAT('_',7)),(8,42,REPEAT('_',94)),(8,43,REPEAT('_',22)),(8,44,REPEAT('_',26)),(8,45,REPEAT('_',156)),(8,46,REPEAT('_',22)),(8,47,REPEAT('_',128)),(8,48,REPEAT('_',136)),(8,49,REPEAT('_',4)),(8,50,REPEAT('_',137));
INSERT INTO bulk_test.test2 (`key`,val1,val2,str) VALUES (LAST_INSERT_ID(),8,1,REPEAT('_',63)),(LAST_INSERT_ID()+1,8,2,REPEAT('_',105)),(LAST_INSERT_ID()+2,8,3,REPEAT('_',67)),(LAST_INSERT_ID()+3,8,4,REPEAT('_',33)),(LAST_INSERT_ID()+4,8,5,REPEAT('_',147)),(LAST_INSERT_ID()+5,8,6,REPEAT('_',190)),(LAST_INSERT_ID()+6,8,7,REPEAT('_',152)),(LAST_INSERT_ID()+7,8,8,REPEAT('_',48)),(LAST_INSERT_ID()+8,8,9,REPEAT('_',137)),(LAST_INSERT_ID()+9,8,10,REPEAT('_',121)),(LAST_INSERT_ID()+10,8,11,REPEAT('_',52)),(LAST_INSERT_ID()+11,8,12,REPEAT('_',168)),(LAST_INSERT_ID()+12,8,13,REPEAT('_',6)),(LAST_INSERT_ID()+13,8,14,REPEAT('_',60)),(LAST_INSERT_ID()+14,8,15,REPEAT('_',75)),(LAST_INSERT_ID()+15,8,16,REPEAT('_',172)),(LAST_INSERT_ID()+16,8,17,REPEAT('_',57)),(LAST_INSERT_ID()+17,8,18,REPEAT('_',185)),(LAST_INSERT_ID()+18,8,19,REPEAT('_',176)),(LAST_INSERT_ID()+19,8,20,REPEAT('_',96)),(LAST_INSERT_ID()+20,8,21,REPEAT('_',182)),(LAST_INSERT_ID()+21,8,22,REPEAT('_',111)),(LAST_INSERT_ID()+22,8,23,REPEAT('_',58)),(LAST_INSERT_ID()+23,8,24,REPEAT('_',65)),(LAST_INSERT_ID()+24,8,25,REPEAT('_',63)),(LAST_INSERT_ID()+25,8,26,REPEAT('_',96)),(LAST_INSERT_ID()+26,8,27,REPEAT('_',20)),(LAST_INSERT_ID()+27,8,28,REPEAT('_',166)),(LAST_INSERT_ID()+28,8,29,REPEAT('_',146)),(LAST_INSERT_ID()+29,8,30,REPEAT('_',93)),(LAST_INSERT_ID()+30,8,31,REPEAT('_',18)),(LAST_INSERT_ID()+31,8,32,REPEAT('_',21)),(LAST_INSERT_ID()+32,8,33,REPEAT('_',46)),(LAST_INSERT_ID()+33,8,34,REPEAT('_',135)),(LAST_INSERT_ID()+34,8,35,REPEAT('_',188)),(LAST_INSERT_ID()+35,8,36,REPEAT('_',196)),(LAST_INSERT_ID()+36,8,37,REPEAT('_',137)),(LAST_INSERT_ID()+37,8,38,REPEAT('_',42)),(LAST_INSERT_ID()+38,8,39,REPEAT('_',144)),(LAST_INSERT_ID()+39,8,40,REPEAT('_',145)),(LAST_INSERT_ID()+40,8,41,REPEAT('_',7)),(LAST_INSERT_ID()+41,8,42,REPEAT('_',94)),(LAST_INSERT_ID()+42,8,43,REPEAT('_',22)),(LAST_INSERT_ID()+43,8,44,REPEAT('_',26)),(LAST_INSERT_ID()+44,8,45,REPEAT('_',156)),(LAST_INSERT_ID()+45,8,46,REPEAT('_',22)),(LAST_INSERT_ID()+46,8,47,REPEAT('_',128)),(LAST_INSERT_ID()+47,8,48,REPEAT('_',136)),(LAST_INSERT_ID()+48,8,49,REPEAT('_',4)),(LAST_INSERT_ID()+49,8,50,REPEAT('_',137));
ROLLBACK;
BEGIN;
INSERT INTO bulk_test.test1 (val1,val2,str) VALUES (9,1,REPEAT('_',26)),(9,2,REPEAT('_',52)),(9,3,REPEAT('_',17)),(9,4,REPEAT('_',1)),(9,5,REPEAT('_',192)),(9,6,REPEAT('_',30)),(9,7,REPEAT('_',149)),(9,8,REPEAT('_',141)),(9,9,REPEAT('_',89)),(9,10,REPEAT('_',1)),(9,11,REPEAT('_',136)),(9,12,REPEAT('_',128));
INSERT INTO bulk_test.test2 (`key`,val1,val2,str) VALUES (LAST_INSERT_ID(),9,1,REPEAT('_',26)),(LAST_INSERT_ID()+1,9,2,REPEAT('_',52)),(LAST_INSERT_ID()+2,9,3,REPEAT('_',17)),(LAST_INSERT_ID()+3,9,4,REPEAT('_',1)),(LAST_INSERT_ID()+4,9,5,REPEAT('_',192)),(LAST_INSERT_ID()+5,9,6,REPEAT('_',30)),(LAST_INSERT_ID()+6,9,7,REPEAT('_',149)),(LAST_INSERT_ID()+7,9,8,REPEAT('_',141)),(LAST_INSERT_ID()+8,9,9,REPEAT('_',89)),(LAST_INSERT_ID()+9,9,10,REPEAT('_',1)),(LAST_INSERT_ID()+10,9,11,REPEAT('_',136)),(LAST_INSERT_ID()+11,9,12,REPEAT('_',128));
INSERT INTO bulk_test.test1 (val1,val2,str) VALUES (10,1,REPEAT('_',111));
INSERT INTO bulk_test.test2 (`key`,val1,val2,str) VALUES (LAST_INSERT_ID(),10,1,REPEAT('_',111));
COMMIT;
```

```bash:複数行インサート用.shファイル1
$ cat bulk_test1.sh
#!/bin/sh
for((i=0;i<500;i++));do
  /bin/mysql -u root --password=XXXXXXXX bulk_test < /home/centos/bulk_test1.sql
done
```

※それぞれ8個作りました（SQLは少しずつパターンを変えてあります）。

8個のシェルスクリプトをcrontabに登録し、同時並行で実行してみます。

```bash:crontab
$ crontab -l
51 15 * * * /bin/sh /home/centos/bulk_test1.sh
51 15 * * * /bin/sh /home/centos/bulk_test2.sh
51 15 * * * /bin/sh /home/centos/bulk_test3.sh
51 15 * * * /bin/sh /home/centos/bulk_test4.sh
51 15 * * * /bin/sh /home/centos/bulk_test5.sh
51 15 * * * /bin/sh /home/centos/bulk_test6.sh
51 15 * * * /bin/sh /home/centos/bulk_test7.sh
51 15 * * * /bin/sh /home/centos/bulk_test8.sh
```

※タイムゾーンがずれているのは内緒です。

実行後、結果を見てみます。

```mysql:結果確認1
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8026
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> USE bulk_test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> SELECT COUNT(*) FROM test1;
+----------+
| COUNT(*) |
+----------+
|   244000 |
+----------+
1 row in set (0.04 sec)

mysql> SELECT COUNT(*) FROM test2;
+----------+
| COUNT(*) |
+----------+
|   244000 |
+----------+
1 row in set (0.05 sec)
```

LAST_INSERT_ID()が連番でない場合、test1テーブルのid・val1・val2とtest2テーブルのkey・val1・val2の値の対応関係がずれるため、等結合すると件数が減るはずですが、

```mysql:結果確認2
mysql> SELECT COUNT(*) FROM bulk_test.test1 t1, bulk_test.test2 t2 WHERE t1.id=t2.key AND t1.val1=t2.val1 AND t1.val2=t2.val2;
+----------+
| COUNT(*) |
+----------+
|   244000 |
+----------+
1 row in set (0.33 sec)
```

大丈夫でした。
中身も一部覗いてみます。

```mysql:結果確認3
mysql> SELECT t1.id, t1.val1, t2.val2, t2.id, t2.key, t2.val1, t2.val2 FROM bulk_test.test1 t1, bulk_test.test2 t2 WHERE t1.id=t2.key AND t1.val1=t2.val1 AND t1.val2=t2.val2 ORDER BY t1.id LIMIT 200;
+-----+------+------+-----+-----+------+------+
| id  | val1 | val2 | id  | key | val1 | val2 |
+-----+------+------+-----+-----+------+------+
|   1 | 1001 |    1 |   1 |   1 | 1001 |    1 |
|   2 | 1001 |    2 |   2 |   2 | 1001 |    2 |
|   3 | 1001 |    3 |   3 |   3 | 1001 |    3 |
|   4 | 1001 |    4 |   4 |   4 | 1001 |    4 |
|   5 | 1001 |    5 |   5 |   5 | 1001 |    5 |
|   6 | 1001 |    6 |   6 |   6 | 1001 |    6 |
|   7 | 1001 |    7 |   7 |   7 | 1001 |    7 |
|   8 | 1001 |    8 |   8 |   8 | 1001 |    8 |
|   9 | 1001 |    9 |   9 |   9 | 1001 |    9 |
|  10 | 1001 |   10 |  10 |  10 | 1001 |   10 |
|  11 | 1001 |   11 |  11 |  11 | 1001 |   11 |
|  12 | 1001 |   12 |  12 |  12 | 1001 |   12 |
|  13 | 1001 |   13 |  13 |  13 | 1001 |   13 |
|  14 | 1001 |   14 |  14 |  14 | 1001 |   14 |
|  15 | 6001 |    1 |  34 |  15 | 6001 |    1 |
|  16 | 6001 |    2 |  35 |  16 | 6001 |    2 |
|  17 | 6001 |    3 |  36 |  17 | 6001 |    3 |
|  18 | 6001 |    4 |  37 |  18 | 6001 |    4 |
|  19 | 6001 |    5 |  38 |  19 | 6001 |    5 |
|  20 | 6001 |    6 |  39 |  20 | 6001 |    6 |
|  21 | 6001 |    7 |  40 |  21 | 6001 |    7 |
|  22 | 6001 |    8 |  41 |  22 | 6001 |    8 |
|  23 | 6001 |    9 |  42 |  23 | 6001 |    9 |
|  24 | 6001 |   10 |  43 |  24 | 6001 |   10 |
|  25 | 6001 |   11 |  44 |  25 | 6001 |   11 |
|  26 | 6001 |   12 |  45 |  26 | 6001 |   12 |
|  27 | 6001 |   13 |  46 |  27 | 6001 |   13 |
|  28 | 6001 |   14 |  47 |  28 | 6001 |   14 |
|  48 | 1003 |    1 |  48 |  48 | 1003 |    1 |
|  49 | 1003 |    2 |  49 |  49 | 1003 |    2 |
|  50 | 1003 |    3 |  50 |  50 | 1003 |    3 |
|  51 | 1003 |    4 |  51 |  51 | 1003 |    4 |
|  52 | 1003 |    5 |  52 |  52 | 1003 |    5 |
|  53 | 1003 |    6 |  53 |  53 | 1003 |    6 |
|  76 | 6003 |    1 |  76 |  76 | 6003 |    1 |
|  77 | 6003 |    2 |  77 |  77 | 6003 |    2 |
|  78 | 6003 |    3 |  78 |  78 | 6003 |    3 |
|  79 | 6003 |    4 |  79 |  79 | 6003 |    4 |
|  80 | 6003 |    5 |  80 |  80 | 6003 |    5 |
|  81 | 6003 |    6 |  81 |  81 | 6003 |    6 |
|  82 | 1005 |    1 |  82 |  82 | 1005 |    1 |
|  83 | 1005 |    2 |  83 |  83 | 1005 |    2 |
|  84 | 1005 |    3 |  84 |  84 | 1005 |    3 |
|  85 | 1005 |    4 |  85 |  85 | 1005 |    4 |
|  86 | 1005 |    5 |  86 |  86 | 1005 |    5 |
|  87 | 1005 |    6 |  87 |  87 | 1005 |    6 |
|  88 | 1005 |    7 |  88 |  88 | 1005 |    7 |
|  89 | 1005 |    8 |  89 |  89 | 1005 |    8 |
|  90 | 1005 |    9 |  90 |  90 | 1005 |    9 |
|  91 | 1005 |   10 |  91 |  91 | 1005 |   10 |
|  92 | 1005 |   11 |  92 |  92 | 1005 |   11 |
|  93 | 1005 |   12 |  93 |  93 | 1005 |   12 |
|  94 | 1006 |    1 |  94 |  94 | 1006 |    1 |
|  95 | 1006 |    2 |  95 |  95 | 1006 |    2 |
|  96 | 1007 |    1 |  96 |  96 | 1007 |    1 |
|  97 | 1007 |    2 |  97 |  97 | 1007 |    2 |
|  98 | 1007 |    3 |  98 |  98 | 1007 |    3 |
|  99 | 1008 |    1 |  99 |  99 | 1008 |    1 |
| 100 | 1008 |    2 | 100 | 100 | 1008 |    2 |
| 101 | 1008 |    3 | 101 | 101 | 1008 |    3 |
| 102 | 1008 |    4 | 102 | 102 | 1008 |    4 |
| 103 | 1008 |    5 | 103 | 103 | 1008 |    5 |
| 104 | 1008 |    6 | 104 | 104 | 1008 |    6 |
| 105 | 1008 |    7 | 105 | 105 | 1008 |    7 |
| 106 | 1008 |    8 | 106 | 106 | 1008 |    8 |
| 107 | 1009 |    1 | 107 | 107 | 1009 |    1 |
| 108 | 1009 |    2 | 108 | 108 | 1009 |    2 |
| 109 | 1009 |    3 | 109 | 109 | 1009 |    3 |
| 110 | 1009 |    4 | 110 | 110 | 1009 |    4 |
| 111 | 1009 |    5 | 111 | 111 | 1009 |    5 |
| 112 | 1009 |    6 | 112 | 112 | 1009 |    6 |
| 113 | 1009 |    7 | 113 | 113 | 1009 |    7 |
| 114 | 1009 |    8 | 114 | 114 | 1009 |    8 |
| 115 | 1009 |    9 | 115 | 115 | 1009 |    9 |
| 116 | 1009 |   10 | 116 | 116 | 1009 |   10 |
| 117 | 1009 |   11 | 117 | 117 | 1009 |   11 |
| 118 | 1009 |   12 | 118 | 118 | 1009 |   12 |
| 119 | 1010 |    1 | 119 | 119 | 1010 |    1 |
| 120 | 1010 |    2 | 120 | 120 | 1010 |    2 |
| 121 | 1010 |    3 | 121 | 121 | 1010 |    3 |
| 122 | 1010 |    4 | 122 | 122 | 1010 |    4 |
| 126 | 6005 |    1 | 126 | 126 | 6005 |    1 |
| 127 | 6005 |    2 | 127 | 127 | 6005 |    2 |
| 128 | 6005 |    3 | 128 | 128 | 6005 |    3 |
| 129 | 6005 |    4 | 129 | 129 | 6005 |    4 |
| 130 | 6005 |    5 | 130 | 130 | 6005 |    5 |
| 131 | 6005 |    6 | 131 | 131 | 6005 |    6 |
| 132 | 6005 |    7 | 132 | 132 | 6005 |    7 |
| 133 | 6005 |    8 | 133 | 133 | 6005 |    8 |
| 134 | 6005 |    9 | 134 | 134 | 6005 |    9 |
| 135 | 6005 |   10 | 135 | 135 | 6005 |   10 |
| 136 | 6005 |   11 | 136 | 136 | 6005 |   11 |
| 137 | 6005 |   12 | 137 | 137 | 6005 |   12 |
| 138 | 6006 |    1 | 138 | 138 | 6006 |    1 |
| 139 | 6006 |    2 | 139 | 139 | 6006 |    2 |
| 140 | 6007 |    1 | 140 | 140 | 6007 |    1 |
| 141 | 6007 |    2 | 141 | 141 | 6007 |    2 |
| 142 | 6007 |    3 | 142 | 142 | 6007 |    3 |
| 143 | 6008 |    1 | 143 | 143 | 6008 |    1 |
| 144 | 6008 |    2 | 144 | 144 | 6008 |    2 |
| 145 | 6008 |    3 | 145 | 145 | 6008 |    3 |
| 146 | 6008 |    4 | 146 | 146 | 6008 |    4 |
| 147 | 6008 |    5 | 147 | 147 | 6008 |    5 |
| 148 | 6008 |    6 | 148 | 148 | 6008 |    6 |
| 149 | 6008 |    7 | 149 | 149 | 6008 |    7 |
| 150 | 6008 |    8 | 150 | 150 | 6008 |    8 |
| 151 | 6009 |    1 | 151 | 151 | 6009 |    1 |
| 152 | 6009 |    2 | 152 | 152 | 6009 |    2 |
| 153 | 6009 |    3 | 153 | 153 | 6009 |    3 |
| 154 | 6009 |    4 | 154 | 154 | 6009 |    4 |
| 155 | 6009 |    5 | 155 | 155 | 6009 |    5 |
| 156 | 6009 |    6 | 156 | 156 | 6009 |    6 |
| 157 | 6009 |    7 | 157 | 157 | 6009 |    7 |
| 158 | 6009 |    8 | 158 | 158 | 6009 |    8 |
| 159 | 6009 |    9 | 159 | 159 | 6009 |    9 |
| 160 | 6009 |   10 | 160 | 160 | 6009 |   10 |
| 161 | 6009 |   11 | 161 | 161 | 6009 |   11 |
| 162 | 6009 |   12 | 162 | 162 | 6009 |   12 |
| 163 | 6010 |    1 | 163 | 163 | 6010 |    1 |
| 164 | 6010 |    2 | 164 | 164 | 6010 |    2 |
| 165 | 6010 |    3 | 165 | 165 | 6010 |    3 |
| 166 | 6010 |    4 | 166 | 166 | 6010 |    4 |
| 167 | 1001 |    1 | 167 | 167 | 1001 |    1 |
| 168 | 1001 |    2 | 168 | 168 | 1001 |    2 |
| 169 | 1001 |    3 | 169 | 169 | 1001 |    3 |
| 170 | 1001 |    4 | 170 | 170 | 1001 |    4 |
| 171 | 1001 |    5 | 171 | 171 | 1001 |    5 |
| 172 | 1001 |    6 | 172 | 172 | 1001 |    6 |
| 173 | 1001 |    7 | 173 | 173 | 1001 |    7 |
| 174 | 1001 |    8 | 174 | 174 | 1001 |    8 |
| 175 | 1001 |    9 | 175 | 175 | 1001 |    9 |
| 176 | 1001 |   10 | 176 | 176 | 1001 |   10 |
| 177 | 1001 |   11 | 177 | 177 | 1001 |   11 |
| 178 | 1001 |   12 | 178 | 178 | 1001 |   12 |
| 179 | 1001 |   13 | 179 | 179 | 1001 |   13 |
| 180 | 1001 |   14 | 180 | 180 | 1001 |   14 |
| 181 | 5001 |    1 | 181 | 181 | 5001 |    1 |
| 182 | 5001 |    2 | 182 | 182 | 5001 |    2 |
| 183 | 5001 |    3 | 183 | 183 | 5001 |    3 |
| 184 | 5001 |    4 | 184 | 184 | 5001 |    4 |
| 185 | 5001 |    5 | 185 | 185 | 5001 |    5 |
| 186 | 5001 |    6 | 186 | 186 | 5001 |    6 |
| 187 | 5001 |    7 | 187 | 187 | 5001 |    7 |
| 188 | 5001 |    8 | 188 | 188 | 5001 |    8 |
| 189 | 5001 |    9 | 189 | 189 | 5001 |    9 |
| 190 | 5001 |   10 | 190 | 190 | 5001 |   10 |
| 191 | 5001 |   11 | 191 | 191 | 5001 |   11 |
| 192 | 5001 |   12 | 192 | 192 | 5001 |   12 |
| 193 | 5001 |   13 | 193 | 193 | 5001 |   13 |
| 194 | 5001 |   14 | 194 | 194 | 5001 |   14 |
| 233 |    1 |    1 | 233 | 233 |    1 |    1 |
| 234 |    1 |    2 | 234 | 234 |    1 |    2 |
| 235 |    1 |    3 | 235 | 235 |    1 |    3 |
| 236 |    1 |    4 | 236 | 236 |    1 |    4 |
| 237 |    1 |    5 | 237 | 237 |    1 |    5 |
| 238 |    1 |    6 | 238 | 238 |    1 |    6 |
| 239 |    1 |    7 | 239 | 239 |    1 |    7 |
| 240 |    1 |    8 | 240 | 240 |    1 |    8 |
| 241 |    1 |    9 | 241 | 241 |    1 |    9 |
| 242 |    1 |   10 | 242 | 242 |    1 |   10 |
| 243 |    1 |   11 | 243 | 243 |    1 |   11 |
| 244 |    1 |   12 | 244 | 244 |    1 |   12 |
| 245 |    1 |   13 | 245 | 245 |    1 |   13 |
| 246 |    1 |   14 | 246 | 246 |    1 |   14 |
| 247 |    1 |   15 | 247 | 247 |    1 |   15 |
| 248 |    1 |   16 | 248 | 248 |    1 |   16 |
| 249 |    1 |   17 | 249 | 249 |    1 |   17 |
| 250 |    1 |   18 | 250 | 250 |    1 |   18 |
| 251 |    1 |   19 | 251 | 251 |    1 |   19 |
| 252 |    1 |   20 | 252 | 252 |    1 |   20 |
| 253 | 4001 |    1 | 291 | 253 | 4001 |    1 |
| 254 | 4001 |    2 | 292 | 254 | 4001 |    2 |
| 255 | 4001 |    3 | 293 | 255 | 4001 |    3 |
| 256 | 4001 |    4 | 294 | 256 | 4001 |    4 |
| 257 | 4001 |    5 | 295 | 257 | 4001 |    5 |
| 258 | 4001 |    6 | 296 | 258 | 4001 |    6 |
| 259 | 4001 |    7 | 297 | 259 | 4001 |    7 |
| 260 | 4001 |    8 | 298 | 260 | 4001 |    8 |
| 261 | 4001 |    9 | 299 | 261 | 4001 |    9 |
| 262 | 4001 |   10 | 300 | 262 | 4001 |   10 |
| 263 | 4001 |   11 | 301 | 263 | 4001 |   11 |
| 264 | 4001 |   12 | 302 | 264 | 4001 |   12 |
| 265 | 4001 |   13 | 303 | 265 | 4001 |   13 |
| 266 | 4001 |   14 | 304 | 266 | 4001 |   14 |
| 267 | 2001 |    1 | 305 | 267 | 2001 |    1 |
| 268 | 2001 |    2 | 306 | 268 | 2001 |    2 |
| 269 | 2001 |    3 | 307 | 269 | 2001 |    3 |
| 270 | 2001 |    4 | 308 | 270 | 2001 |    4 |
| 271 | 2001 |    5 | 309 | 271 | 2001 |    5 |
| 272 | 2001 |    6 | 310 | 272 | 2001 |    6 |
| 273 | 2001 |    7 | 311 | 273 | 2001 |    7 |
| 274 | 2001 |    8 | 312 | 274 | 2001 |    8 |
| 275 | 2001 |    9 | 313 | 275 | 2001 |    9 |
| 276 | 2001 |   10 | 314 | 276 | 2001 |   10 |
| 277 | 2001 |   11 | 315 | 277 | 2001 |   11 |
| 278 | 2001 |   12 | 316 | 278 | 2001 |   12 |
| 279 | 2001 |   13 | 317 | 279 | 2001 |   13 |
| 280 | 2001 |   14 | 318 | 280 | 2001 |   14 |
| 281 |    2 |    1 | 253 | 281 |    2 |    1 |
| 282 |    2 |    2 | 254 | 282 |    2 |    2 |
+-----+------+------+-----+-----+------+------+
200 rows in set (0.36 sec)

mysql> SELECT t1.id, t1.val1, t2.val2, t2.id, t2.key, t2.val1, t2.val2 FROM bulk_test.test1 t1, bulk_test.test2 t2 WHERE t1.id=t2.key AND t1.val1=t2.val1 AND t1.val2=t2.val2 ORDER BY t1.id LIMIT 243800,200;
+--------+------+------+--------+--------+------+------+
| id     | val1 | val2 | id     | key    | val1 | val2 |
+--------+------+------+--------+--------+------+------+
| 324301 | 4001 |    6 | 324304 | 324301 | 4001 |    6 |
| 324302 | 4001 |    7 | 324305 | 324302 | 4001 |    7 |
| 324303 | 4001 |    8 | 324306 | 324303 | 4001 |    8 |
| 324304 | 4001 |    9 | 324307 | 324304 | 4001 |    9 |
| 324305 | 4001 |   10 | 324308 | 324305 | 4001 |   10 |
| 324306 | 4001 |   11 | 324309 | 324306 | 4001 |   11 |
| 324307 | 4001 |   12 | 324310 | 324307 | 4001 |   12 |
| 324308 | 4001 |   13 | 324311 | 324308 | 4001 |   13 |
| 324309 | 4001 |   14 | 324312 | 324309 | 4001 |   14 |
| 324310 | 2004 |    1 | 324296 | 324310 | 2004 |    1 |
| 324311 | 2004 |    2 | 324297 | 324311 | 2004 |    2 |
| 324312 | 2004 |    3 | 324298 | 324312 | 2004 |    3 |
| 324313 | 2005 |    1 | 324313 | 324313 | 2005 |    1 |
| 324314 | 2005 |    2 | 324314 | 324314 | 2005 |    2 |
| 324315 | 2005 |    3 | 324315 | 324315 | 2005 |    3 |
| 324316 | 2005 |    4 | 324316 | 324316 | 2005 |    4 |
| 324317 | 2005 |    5 | 324317 | 324317 | 2005 |    5 |
| 324318 | 2005 |    6 | 324318 | 324318 | 2005 |    6 |
| 324319 | 2005 |    7 | 324319 | 324319 | 2005 |    7 |
| 324320 | 2005 |    8 | 324320 | 324320 | 2005 |    8 |
| 324321 | 2005 |    9 | 324321 | 324321 | 2005 |    9 |
| 324322 | 2005 |   10 | 324322 | 324322 | 2005 |   10 |
| 324323 | 2005 |   11 | 324323 | 324323 | 2005 |   11 |
| 324324 | 2005 |   12 | 324324 | 324324 | 2005 |   12 |
| 324325 | 4002 |    1 | 324325 | 324325 | 4002 |    1 |
| 324326 | 4002 |    2 | 324326 | 324326 | 4002 |    2 |
| 324327 | 4002 |    3 | 324327 | 324327 | 4002 |    3 |
| 324328 | 4002 |    4 | 324328 | 324328 | 4002 |    4 |
| 324329 | 4002 |    5 | 324329 | 324329 | 4002 |    5 |
| 324330 | 4002 |    6 | 324330 | 324330 | 4002 |    6 |
| 324331 | 4002 |    7 | 324331 | 324331 | 4002 |    7 |
| 324332 | 4002 |    8 | 324332 | 324332 | 4002 |    8 |
| 324333 | 4002 |    9 | 324333 | 324333 | 4002 |    9 |
| 324334 | 4002 |   10 | 324334 | 324334 | 4002 |   10 |
| 324335 | 4002 |   11 | 324335 | 324335 | 4002 |   11 |
| 324336 | 4002 |   12 | 324336 | 324336 | 4002 |   12 |
| 324337 | 4002 |   13 | 324337 | 324337 | 4002 |   13 |
| 324338 | 4002 |   14 | 324338 | 324338 | 4002 |   14 |
| 324339 | 4002 |   15 | 324339 | 324339 | 4002 |   15 |
| 324340 | 4002 |   16 | 324340 | 324340 | 4002 |   16 |
| 324341 | 4002 |   17 | 324341 | 324341 | 4002 |   17 |
| 324342 | 4002 |   18 | 324342 | 324342 | 4002 |   18 |
| 324343 | 4002 |   19 | 324343 | 324343 | 4002 |   19 |
| 324344 | 2006 |    1 | 324344 | 324344 | 2006 |    1 |
| 324345 | 2007 |    1 | 324345 | 324345 | 2007 |    1 |
| 324346 | 2007 |    2 | 324346 | 324346 | 2007 |    2 |
| 324347 | 2007 |    3 | 324347 | 324347 | 2007 |    3 |
| 324348 | 2007 |    4 | 324348 | 324348 | 2007 |    4 |
| 324349 | 2007 |    5 | 324349 | 324349 | 2007 |    5 |
| 324350 | 2007 |    6 | 324350 | 324350 | 2007 |    6 |
| 324351 | 4003 |    1 | 324351 | 324351 | 4003 |    1 |
| 324352 | 4003 |    2 | 324352 | 324352 | 4003 |    2 |
| 324353 | 4003 |    3 | 324353 | 324353 | 4003 |    3 |
| 324354 | 4003 |    4 | 324354 | 324354 | 4003 |    4 |
| 324355 | 4003 |    5 | 324355 | 324355 | 4003 |    5 |
| 324356 | 4003 |    6 | 324356 | 324356 | 4003 |    6 |
| 324357 | 4004 |    1 | 324357 | 324357 | 4004 |    1 |
| 324358 | 4004 |    2 | 324358 | 324358 | 4004 |    2 |
| 324359 | 4004 |    3 | 324359 | 324359 | 4004 |    3 |
| 324360 | 4005 |    1 | 324360 | 324360 | 4005 |    1 |
| 324361 | 4005 |    2 | 324361 | 324361 | 4005 |    2 |
| 324362 | 4005 |    3 | 324362 | 324362 | 4005 |    3 |
| 324363 | 4005 |    4 | 324363 | 324363 | 4005 |    4 |
| 324364 | 4005 |    5 | 324364 | 324364 | 4005 |    5 |
| 324365 | 4005 |    6 | 324365 | 324365 | 4005 |    6 |
| 324366 | 4005 |    7 | 324366 | 324366 | 4005 |    7 |
| 324367 | 4005 |    8 | 324367 | 324367 | 4005 |    8 |
| 324368 | 4005 |    9 | 324368 | 324368 | 4005 |    9 |
| 324369 | 4005 |   10 | 324369 | 324369 | 4005 |   10 |
| 324370 | 4005 |   11 | 324370 | 324370 | 4005 |   11 |
| 324371 | 4005 |   12 | 324371 | 324371 | 4005 |   12 |
| 324372 | 4006 |    1 | 324372 | 324372 | 4006 |    1 |
| 324373 | 4007 |    1 | 324373 | 324373 | 4007 |    1 |
| 324374 | 4007 |    2 | 324374 | 324374 | 4007 |    2 |
| 324375 | 4007 |    3 | 324375 | 324375 | 4007 |    3 |
| 324376 | 4007 |    4 | 324376 | 324376 | 4007 |    4 |
| 324377 | 4007 |    5 | 324377 | 324377 | 4007 |    5 |
| 324378 | 4007 |    6 | 324378 | 324378 | 4007 |    6 |
| 324379 | 4001 |    1 | 324379 | 324379 | 4001 |    1 |
| 324380 | 4001 |    2 | 324380 | 324380 | 4001 |    2 |
| 324381 | 4001 |    3 | 324381 | 324381 | 4001 |    3 |
| 324382 | 4001 |    4 | 324382 | 324382 | 4001 |    4 |
| 324383 | 4001 |    5 | 324383 | 324383 | 4001 |    5 |
| 324384 | 4001 |    6 | 324384 | 324384 | 4001 |    6 |
| 324385 | 4001 |    7 | 324385 | 324385 | 4001 |    7 |
| 324386 | 4001 |    8 | 324386 | 324386 | 4001 |    8 |
| 324387 | 4001 |    9 | 324387 | 324387 | 4001 |    9 |
| 324388 | 4001 |   10 | 324388 | 324388 | 4001 |   10 |
| 324389 | 4001 |   11 | 324389 | 324389 | 4001 |   11 |
| 324390 | 4001 |   12 | 324390 | 324390 | 4001 |   12 |
| 324391 | 4001 |   13 | 324391 | 324391 | 4001 |   13 |
| 324392 | 4001 |   14 | 324392 | 324392 | 4001 |   14 |
| 324393 | 4002 |    1 | 324393 | 324393 | 4002 |    1 |
| 324394 | 4002 |    2 | 324394 | 324394 | 4002 |    2 |
| 324395 | 4002 |    3 | 324395 | 324395 | 4002 |    3 |
| 324396 | 4002 |    4 | 324396 | 324396 | 4002 |    4 |
| 324397 | 4002 |    5 | 324397 | 324397 | 4002 |    5 |
| 324398 | 4002 |    6 | 324398 | 324398 | 4002 |    6 |
| 324399 | 4002 |    7 | 324399 | 324399 | 4002 |    7 |
| 324400 | 4002 |    8 | 324400 | 324400 | 4002 |    8 |
| 324401 | 4002 |    9 | 324401 | 324401 | 4002 |    9 |
| 324402 | 4002 |   10 | 324402 | 324402 | 4002 |   10 |
| 324403 | 4002 |   11 | 324403 | 324403 | 4002 |   11 |
| 324404 | 4002 |   12 | 324404 | 324404 | 4002 |   12 |
| 324405 | 4002 |   13 | 324405 | 324405 | 4002 |   13 |
| 324406 | 4002 |   14 | 324406 | 324406 | 4002 |   14 |
| 324407 | 4002 |   15 | 324407 | 324407 | 4002 |   15 |
| 324408 | 4002 |   16 | 324408 | 324408 | 4002 |   16 |
| 324409 | 4002 |   17 | 324409 | 324409 | 4002 |   17 |
| 324410 | 4002 |   18 | 324410 | 324410 | 4002 |   18 |
| 324411 | 4002 |   19 | 324411 | 324411 | 4002 |   19 |
| 324412 | 4003 |    1 | 324412 | 324412 | 4003 |    1 |
| 324413 | 4003 |    2 | 324413 | 324413 | 4003 |    2 |
| 324414 | 4003 |    3 | 324414 | 324414 | 4003 |    3 |
| 324415 | 4003 |    4 | 324415 | 324415 | 4003 |    4 |
| 324416 | 4003 |    5 | 324416 | 324416 | 4003 |    5 |
| 324417 | 4003 |    6 | 324417 | 324417 | 4003 |    6 |
| 324418 | 4004 |    1 | 324418 | 324418 | 4004 |    1 |
| 324419 | 4004 |    2 | 324419 | 324419 | 4004 |    2 |
| 324420 | 4004 |    3 | 324420 | 324420 | 4004 |    3 |
| 324421 | 4005 |    1 | 324421 | 324421 | 4005 |    1 |
| 324422 | 4005 |    2 | 324422 | 324422 | 4005 |    2 |
| 324423 | 4005 |    3 | 324423 | 324423 | 4005 |    3 |
| 324424 | 4005 |    4 | 324424 | 324424 | 4005 |    4 |
| 324425 | 4005 |    5 | 324425 | 324425 | 4005 |    5 |
| 324426 | 4005 |    6 | 324426 | 324426 | 4005 |    6 |
| 324427 | 4005 |    7 | 324427 | 324427 | 4005 |    7 |
| 324428 | 4005 |    8 | 324428 | 324428 | 4005 |    8 |
| 324429 | 4005 |    9 | 324429 | 324429 | 4005 |    9 |
| 324430 | 4005 |   10 | 324430 | 324430 | 4005 |   10 |
| 324431 | 4005 |   11 | 324431 | 324431 | 4005 |   11 |
| 324432 | 4005 |   12 | 324432 | 324432 | 4005 |   12 |
| 324433 | 4006 |    1 | 324433 | 324433 | 4006 |    1 |
| 324434 | 4007 |    1 | 324434 | 324434 | 4007 |    1 |
| 324435 | 4007 |    2 | 324435 | 324435 | 4007 |    2 |
| 324436 | 4007 |    3 | 324436 | 324436 | 4007 |    3 |
| 324437 | 4007 |    4 | 324437 | 324437 | 4007 |    4 |
| 324438 | 4007 |    5 | 324438 | 324438 | 4007 |    5 |
| 324439 | 4007 |    6 | 324439 | 324439 | 4007 |    6 |
| 324440 | 4001 |    1 | 324440 | 324440 | 4001 |    1 |
| 324441 | 4001 |    2 | 324441 | 324441 | 4001 |    2 |
| 324442 | 4001 |    3 | 324442 | 324442 | 4001 |    3 |
| 324443 | 4001 |    4 | 324443 | 324443 | 4001 |    4 |
| 324444 | 4001 |    5 | 324444 | 324444 | 4001 |    5 |
| 324445 | 4001 |    6 | 324445 | 324445 | 4001 |    6 |
| 324446 | 4001 |    7 | 324446 | 324446 | 4001 |    7 |
| 324447 | 4001 |    8 | 324447 | 324447 | 4001 |    8 |
| 324448 | 4001 |    9 | 324448 | 324448 | 4001 |    9 |
| 324449 | 4001 |   10 | 324449 | 324449 | 4001 |   10 |
| 324450 | 4001 |   11 | 324450 | 324450 | 4001 |   11 |
| 324451 | 4001 |   12 | 324451 | 324451 | 4001 |   12 |
| 324452 | 4001 |   13 | 324452 | 324452 | 4001 |   13 |
| 324453 | 4001 |   14 | 324453 | 324453 | 4001 |   14 |
| 324454 | 4002 |    1 | 324454 | 324454 | 4002 |    1 |
| 324455 | 4002 |    2 | 324455 | 324455 | 4002 |    2 |
| 324456 | 4002 |    3 | 324456 | 324456 | 4002 |    3 |
| 324457 | 4002 |    4 | 324457 | 324457 | 4002 |    4 |
| 324458 | 4002 |    5 | 324458 | 324458 | 4002 |    5 |
| 324459 | 4002 |    6 | 324459 | 324459 | 4002 |    6 |
| 324460 | 4002 |    7 | 324460 | 324460 | 4002 |    7 |
| 324461 | 4002 |    8 | 324461 | 324461 | 4002 |    8 |
| 324462 | 4002 |    9 | 324462 | 324462 | 4002 |    9 |
| 324463 | 4002 |   10 | 324463 | 324463 | 4002 |   10 |
| 324464 | 4002 |   11 | 324464 | 324464 | 4002 |   11 |
| 324465 | 4002 |   12 | 324465 | 324465 | 4002 |   12 |
| 324466 | 4002 |   13 | 324466 | 324466 | 4002 |   13 |
| 324467 | 4002 |   14 | 324467 | 324467 | 4002 |   14 |
| 324468 | 4002 |   15 | 324468 | 324468 | 4002 |   15 |
| 324469 | 4002 |   16 | 324469 | 324469 | 4002 |   16 |
| 324470 | 4002 |   17 | 324470 | 324470 | 4002 |   17 |
| 324471 | 4002 |   18 | 324471 | 324471 | 4002 |   18 |
| 324472 | 4002 |   19 | 324472 | 324472 | 4002 |   19 |
| 324473 | 4003 |    1 | 324473 | 324473 | 4003 |    1 |
| 324474 | 4003 |    2 | 324474 | 324474 | 4003 |    2 |
| 324475 | 4003 |    3 | 324475 | 324475 | 4003 |    3 |
| 324476 | 4003 |    4 | 324476 | 324476 | 4003 |    4 |
| 324477 | 4003 |    5 | 324477 | 324477 | 4003 |    5 |
| 324478 | 4003 |    6 | 324478 | 324478 | 4003 |    6 |
| 324479 | 4004 |    1 | 324479 | 324479 | 4004 |    1 |
| 324480 | 4004 |    2 | 324480 | 324480 | 4004 |    2 |
| 324481 | 4004 |    3 | 324481 | 324481 | 4004 |    3 |
| 324482 | 4005 |    1 | 324482 | 324482 | 4005 |    1 |
| 324483 | 4005 |    2 | 324483 | 324483 | 4005 |    2 |
| 324484 | 4005 |    3 | 324484 | 324484 | 4005 |    3 |
| 324485 | 4005 |    4 | 324485 | 324485 | 4005 |    4 |
| 324486 | 4005 |    5 | 324486 | 324486 | 4005 |    5 |
| 324487 | 4005 |    6 | 324487 | 324487 | 4005 |    6 |
| 324488 | 4005 |    7 | 324488 | 324488 | 4005 |    7 |
| 324489 | 4005 |    8 | 324489 | 324489 | 4005 |    8 |
| 324490 | 4005 |    9 | 324490 | 324490 | 4005 |    9 |
| 324491 | 4005 |   10 | 324491 | 324491 | 4005 |   10 |
| 324492 | 4005 |   11 | 324492 | 324492 | 4005 |   11 |
| 324493 | 4005 |   12 | 324493 | 324493 | 4005 |   12 |
| 324494 | 4006 |    1 | 324494 | 324494 | 4006 |    1 |
| 324495 | 4007 |    1 | 324495 | 324495 | 4007 |    1 |
| 324496 | 4007 |    2 | 324496 | 324496 | 4007 |    2 |
| 324497 | 4007 |    3 | 324497 | 324497 | 4007 |    3 |
| 324498 | 4007 |    4 | 324498 | 324498 | 4007 |    4 |
| 324499 | 4007 |    5 | 324499 | 324499 | 4007 |    5 |
| 324500 | 4007 |    6 | 324500 | 324500 | 4007 |    6 |
+--------+------+------+--------+--------+------+------+
200 rows in set (0.40 sec)
```

複数行インサートした単位（val1が同じ範囲）で連番が振られていることが分かります。
また、test1テーブルのidでソートすると、必ずしもtest2テーブルのidが同じ順番で振られているわけではないものの（他のトランザクションと順番が入れ替わったりしています）、test1テーブルのidとtest2テーブルのkeyが正しく対応していることがわかります。

# 4. おまけ：Connector/J経由でまとめて（batch）INSERTするときは

**「rewriteBatchedStatements」**を**「true」**にするのを忘れないようにしましょう。