php - foreach INSERT 到 Mysql DB 中非常慢,特别是在插入超过 100K 条记录时

标签 php mysql codeigniter

我怎样才能让这段代码更快。当我向数据库中插入 100 条记录时,这很好,但当我插入 500K 条记录时,需要很长时间。 我尝试在我的代码中使用 implode 但它不起作用。 代码似乎有两个 foreach 循环,一个在另一个循环内,但我找不到一种方法让它工作,有人有想法吗? 我的框架是codeigniter。

代码如下:

<?php  
 function Add_multiple_users($values)
{
    $err = '';
    foreach($values as $rows)
    {
        $clientQuery = 'INSERT INTO
                            client
                            (
                                admin_id,
                                create_time
                            )
                            VALUES
                            (
                                "'.$this -> session -> userdata('user_id').'",
                                "'.date('Y-m-d H:i:s').'"
                            )';
        $clientResult = @$this -> db -> query($clientQuery);
        if($clientResult)
        {
            $client_id = $this -> db -> insert_id();
            foreach($rows as $row)
            {
                $attrQuery = 'INSERT INTO
                                    client_attribute_value
                                    (
                                        attribute_id,
                                        client_id,
                                        value
                                    )
                                    VALUES
                                    (
                                        "'.$row['attribute_id'].'",
                                        "'.$client_id.'",
                                        "'.addslashes(trim($row['value'])).'"
                                    )';
                $attrResult = @$this -> db -> query($attrQuery);
                if(!$attrResult)
                {
                    $err .= '<p class="box error">Could not add attribute for<br>
                            Attribute ID: '.$row['attribute_id'].'<br>
                            Client ID: '.$client_id.'<br>
                            Attribute Value: '.trim($row['value']).'</p>';
                }
            }
        }
    }
    return $err;
}
?>

这是我尝试过的:

                $attrQuery = "INSERT INTO client_attribute_value (attribute_id, client_id, value) VALUES ";

                $vls = array();
                foreach($rows as $row) {
                    $myattribute_id = $row['attribute_id'];
                    $myclient_id = $row[$client_id];
                    $myvalue = addslashes(trim($row['value']));
                    $vls[] = " ( '$myattribute_id ', '$myclient_id ', '$myvalue ')";


                  $attrQuery .= implode(', ', $vls);

                  $attrResult = @$this -> db -> query($attrQuery);

客户端表示例: client table sample

client_attribute_value 表示例: client_attribute_value

最佳答案

回答我自己的问题,希望这对将来的其他人有帮助。

在 codeigniter/framework 的 model.php 文件中添加以下内容:

$this->db->trans_start();

   MY CODE

$this->db->trans_complete();

问题已解决。 :) 它加快了我将记录插入数据库的速度大约。 30 秒内 15K 条记录。

关于php - foreach INSERT 到 Mysql DB 中非常慢,特别是在插入超过 100K 条记录时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23568930/

相关文章:

php - 单例模式如何在程序的整个生命周期中保留在内存中?

javascript - 如何更改 HTML.Twig 文件中隐藏文件的值

php - 使用 PHP foreach 代码填充日历

MySQL 双左连接返回与预期结果相反的结果

php - 获取数组中的最后一个值

php - 其中加入 1 : Many relationship?

php - 预测 : Passing value to anonymous transaction function

php - PDF裁剪PHP库

php - 无法从数据库中选择列,常量已定义

php - 带有获取参数的codeigniter分页url