Skip to content

Instantly share code, notes, and snippets.

@kentatogashi
Created July 24, 2018 01:33
Show Gist options
  • Save kentatogashi/601722e8b76ea360cbdbc7909cf1a243 to your computer and use it in GitHub Desktop.
Save kentatogashi/601722e8b76ea360cbdbc7909cf1a243 to your computer and use it in GitHub Desktop.
MySQLで、ランダムな文字列を挿入するだけのプロシージャ ref: https://qiita.com/kentatogashi/items/6c24a5460bb7f3f9a930
CREATE TABLE `test` (id INT(11) NOT NULL AUTO_INCREMENT, string VARCHAR(32), created_date TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DELIMITER //
CREATE PROCEDURE f(n INT)
BEGIN
DECLARE i int DEFAULT 0;
WHILE i < n DO
INSERT INTO `test` (`string`) SELECT MD5(RAND());
SET i = i + 1;
END WHILE;
END //
DELIMITER ;
CALL f(1000);
@kentatogashi
Copy link
Author

インデックスをあり、なしの際の速度検証

MariaDB [dev]> SELECT * FROM test WHERE string = '4001b73c63742787c4f45ed89cb4b3f7';
+----+----------------------------------+---------------------+
| id | string                           | created_date        |
+----+----------------------------------+---------------------+
|  1 | 4001b73c63742787c4f45ed89cb4b3f7 | 2018-07-24 10:32:35 |
+----+----------------------------------+---------------------+
1 row in set (0.07 sec)

MariaDB [dev]> ALTER TABLE `test` ADD INDEX index_string(string);
Query OK, 0 rows affected (1.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [dev]> SELECT * FROM test WHERE string = '4001b73c63742787c4f45ed89cb4b3f7';
+----+----------------------------------+---------------------+
| id | string                           | created_date        |
+----+----------------------------------+---------------------+
|  1 | 4001b73c63742787c4f45ed89cb4b3f7 | 2018-07-24 10:32:35 |
+----+----------------------------------+---------------------+
1 row in set (0.00 sec)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment