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?
I would recommend you use
LOAD DATA LOCAL INFILEunless you need to do some processing per row as you insert.It’s often recommended that preparing an SQL statement and binding parameters is faster on the database side, because it eliminates the need to re-parse the SQL query thousands of times.
But the PHP extension itself contains a bit of code that needs to run on the application side. The mysqli extension may be more complex and thus run with slightly more overhead than the old mysql extension for repetitive operations.
In PHP, the overhead of a single function call can be noticeable if you repeat it enough times in a loop. I’ve had people tell me there’s a measurable difference between these two statements:
The second line is a function call, so it’s slower (though by a very small margin).