???????
?????????????????????????????????????????????????????????????Load Data?????????????????鷳??????漰????д??????????????????????
???????????
<?php
$dsn = 'mysql:host=localhost;dbname=test';
$db = new PDO($dsn??'root'??''??array(PDO::ATTR_PERSISTENT => true));
//?????ε????????
$db->query('delete from `test`');
//??????
$start_time = time();
$sum = 1000000;
// ???????
$num = 1;
if ($num == 1){
// ????????
for($i = 0; $i < $sum; $i++){
$db->query("insert into `test` (`id`??`name`) values ($i??'tsetssdf')");
}
} elseif ($num == 2) {
// ????????????????max_allowed_packet??????10????????
for ($i = 0; $i < $sum; $i++) {
if ($i == $sum - 1) { //?????
if ($i%100000 == 0){
$values = "($i?? 'testtest')";
$db->query("insert into `test` (`id`?? `name`) values $values");
} else {
$values .= "??($i?? 'testtest')";
$db->query("insert into `test` (`id`?? `name`) values $values");
}
break;
}
if ($i%100000 == 0) { //???????????????2????
if ($i == 0){
$values = "($i?? 'testtest')";
} else {
$db->query("insert into `test` (`id`?? `name`) values $values");
$values = "($i?? 'testtest')";
}
} else {
$values .= "??($i?? 'testtest')";
}
}
} elseif ($num == 3) {
// ???????
$db->beginTransaction();
for($i = 0; $i < $sum; $i++){
$db->query("insert into `test` (`id`??`name`) values ($i??'tsetssdf')");
}
$db->commit();
} elseif ($num == 4) {
// ???load data
$filename = dirname(__FILE__).'/test.sql';
$fp = fopen($filename?? 'w');
for($i = 0; $i < $sum; $i++){
fputs($fp?? "$i??'testtest' ");
}
$db->exec("load data infile '$filename' into table test fields terminated by '??'");
}
$end_time = time();
echo "????"?? ($end_time - $start_time)?? "?? ";
echo "??????"?? round(memory_get_peak_usage()/1000)?? "KB ";
?>