I want to insert many thousand records and I need this to be fast. I read a lot about this issue so I decided to drop my old approach (mysql_connect) and use prepare statements and mysqli. So In order to test the speed of this I write the following.
function load_data2db($sms_id){
$mysqli = new mysqli('localhost', 'root', 'cr3at1ve', 'tmp-clicknsend');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// Create statement object
$stmt = $mysqli->stmt_init();
if ($stmt = $mysqli->prepare("INSERT INTO isec_test (sms_id, status,msgid,prmsgid,mob,sms_history_id) values (?, ?, ?, ?, ?, ?)")) {
/* Bind our params */
$stmt->bind_param('ssssss',$sms_id,$status,$msgid,$prmsgid,$mob,$sms_history_id);
for($i=0;$i<100000;$i++)
{
/* Set our params */
$sms_id = "110";
$status = "OK";
$msgid = "msgid";
$prmsgid = "100-0";
$mob = "306974386068";
$sms_history_id = 102;
/* Execute the prepared Statement */
$stmt->execute();
}
/* Close the statement */
$stmt->close();
}
else {
/* Error */
printf("Prepared Statement Error: %s\n", $mysqli->error);
}
}
load_data2db(10);
Then I did the same with the old way ( looping and inserting one by one)
include("mysql_connect.php");
for($i=0;$i<100000;$i++)
{
$query = "INSERT INTO isec_test(sms_id,status,msgid) values ('1','OK','123-123')";
$query = @mysql_query($query);
}
mysql_close($dbc);
I made a lot of tests and always the simple mysql way was 1 sec faster. So I am puzzled. What can I do? Use LOAD DATA?