Created
October 20, 2019 12:46
-
-
Save yudoufu/65752ef2399bcd04b898e51c41e8947d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$rows = 10 * 10000; | |
$host = '127.0.0.1'; | |
$dbname = 'test'; | |
$username = 'root'; | |
$password = ''; | |
$dsn = "mysql:dbname=$dbname;host=$host;charset=utf8mb4"; | |
try { | |
$pdo = new PDO($dsn, $username, $password); | |
$pdo->exec('DROP TABLE IF EXISTS secondry_test'); | |
$pdo->exec('CREATE TABLE secondry_test ( | |
id INT NOT NULL AUTO_INCREMENT, | |
user_id INT NOT NULL, | |
deliver_id INT NOT NULL, | |
flag TINYINT NOT NULL, | |
message VARCHAR(255) NOT NULL, | |
delivered_at DATETIME default NULL, | |
created_at DATETIME NOT NULL, | |
updated_at DATETIME NOT NULL, | |
PRIMARY KEY (id), | |
KEY `user_deliverd` (user_id, delivered_at, deliver_id, message), | |
KEY `user_flag` (user_id, flag, deliver_id, message) | |
) ENGINE=InnoDB'); | |
$stmt = $pdo->prepare('INSERT INTO secondry_test (user_id, deliver_id, flag, message, delivered_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, NOW(), NOW())'); | |
for ($i = 1; $i <= $rows; $i++) { | |
$stmt->execute([ | |
mt_rand(1, 100), | |
mt_rand(1, 100), | |
(mt_rand(0, 10) / 5 ? 0 : 1), | |
sha1(mt_rand(0, 100 * 10000)), | |
mt_rand(0, 10) / 5 ? NULL : date("Y-m-d H:i:s"), | |
]); | |
if ($i % 10000 === 0) { | |
echo $i . "\n"; | |
} | |
} | |
echo 'done'; | |
} catch (PDOException $e) { | |
var_dump($e); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
なお通常のEXPLAINはこう。
delivered_at IS NOT NULLだけtype=rangeになるのがわかる。
ちなみに、delivered_at IS NULLはUsing whereが出ていてattached_conditionに delivered_at IS NULLをlookupに使っている・・・と書いているのだけど、一方でused_key_partsではdeliver_idまで使えているしrefで解決できていて、ちょっと何を信じていいかわからない。
MySQLのマニュアルを信じるなら
IS NULLはindexをつかえる
ということでsecondry indexをlookupするというちょっと変わったアクションなのかもしれない。(想像)