我想插入数千条记录,我需要这个速度很快。我阅读了很多关于这个问题的信息,所以我决定放弃我的旧方法(mysql_connect)并使用准备语句和 mysqli。所以为了测试这个的速度,我写了以下内容。
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);
然后我用旧方法做了同样的事情(循环和插入)
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);
我做了很多测试,简单的 mysql 方法总是快 1 秒。所以我很困惑。我能做些什么?使用加载数据?