#Shellスクリプト--テスト用SQL文を一括生成
2255 ワード
次の表に1000000行のデータを挿入する、インデックスなどの使用状況をテストする.
100000行挿入sql(key 1 key 2 key 3の値が0~999のランダム整数)を生成します.
mysqlテーブルにインポート:
Create Table: CREATE TABLE `key_t` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`key1` int(11) DEFAULT NULL,
`key2` int(11) DEFAULT NULL,
`key3` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `key1` (`key1`,`key2`,`key3`)
) ENGINE=InnoDB
100000行挿入sql(key 1 key 2 key 3の値が0~999のランダム整数)を生成します.
$ echo "insert into key_t(key1,key2,key3) values(floor((rand()*1000)),floor(rand()*1000), floor(rand()*1000));" | awk '{for(i=0; i<1000000; i++) print $0}' > insert_into_key_t.sql
$ head insert_into_key_t.sql
insert into key_t(key1,key2,key3) values(floor((rand()*1000)),floor(rand()*1000), floor(rand()*1000));
insert into key_t(key1,key2,key3) values(floor((rand()*1000)),floor(rand()*1000), floor(rand()*1000));
insert into key_t(key1,key2,key3) values(floor((rand()*1000)),floor(rand()*1000), floor(rand()*1000));
# awk
$ echo "" | awk '{for(i=0; i<3; i++) print "insert into key_t(key1,key2,key3) values("int((rand()*1000))", "int(rand()*1000)", "int(rand()*1000)");"}'
insert into key_t(key1,key2,key3) values(237, 291, 845);
insert into key_t(key1,key2,key3) values(152, 585, 193);
insert into key_t(key1,key2,key3) values(810, 173, 484);
mysqlテーブルにインポート:
$ mysql -uroot -p test < insert_into_key_t.sql
mysql> explain select count(*) from key_t where key1=1 group by key3;
+----+-------------+-------+------+---------------+------+---------+-------+------+-----------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+-------+------+-----------------------------------------------------------+
| 1 | SIMPLE | key_t | ref | key1 | key1 | 5 | const | 961 | Using where; Using index; Using temporary; Using filesort |
+----+-------------+-------+------+---------------+------+---------+-------+------+-----------------------------------------------------------+
1 row in set (0.00 sec)