I normally don't use a lot of SQL stuff in PHP but recently have been forced into it by a friend as to help him debug something.
I am using PDO with PHP to insert some data but the insert performance is abysmal. A simple loop of 151 inserts takes nearly 6 seconds! and I am lost as to why.
Here is my code:
<?php
$database='XXXXXX';
$username='XXXXXX';
$password='XXXXXX';
$hostname='127.0.0.1';
$inserted=0;
$counted=0;
$dsn = 'mysql:dbname='.$database.';host='.$hostname.'';
$start=microtime(true);
try {
$data = new PDO($dsn, $username, $password, array(PDO::ATTR_EMULATE_PREPARES => false));
} catch (PDOException $e) {
echo('Connection failed: ' . $e->getMessage());
}
for($i=1;$i<=150;$i++) {
$time=time();
$query=$data->prepare("INSERT INTO `tbl_temp` (aid, bid) VALUES (?, ?)");
$query->execute(array($i, $time));
}
$data=null;
print "Took: ".(microtime(true)-$start)." S to insert $i records\n";
// Took: 5.569482088089 S to insert 151 records <--- result
?>
I have tried the same code using bindParam also and the speed is roughly the same. The server has an 8 core Xeon processor and 64gb of RAM. The script is being run from the command line (php-cgi) and the database and table are new and empty. The database type is InnoDB. Can anyone point me in the right direction of where to look as to why it's so slow? Because I am sure MySQL never used to be this slow!