doqp87012 2016-02-11 18:03
浏览 53

一次运行多个mysql命令而不是在代码点火器中顺序执行?

So I am working on an existing codebase and the function takes each row from a csv, formats the data and then inserts it into the db. This whole process takes about ~20 minutes with the rows in the CSV. However if I keep everything the same and remove the line that actually does the database entry

$this->db->replace('my_table', $myObject);

The whole code runs in about 2 seconds. So I am wondering is it possible to store all of the objects and then assign them all to the DB at once? I am thinking of doing 1 sql call instead of thousands and thousands. Thoughts ?

  • 写回答

1条回答 默认 最新

  • dream07769 2016-02-11 19:57
    关注

    Have you looked into insert_batch() ? See Documentation here.

    You can parse your csv and build an array like this

    $insert_array = array();
    foreach($myCSV as $item)
    {
         $insert_array[] = array('db_field1' => $item->value1, 
                                 'db_field2' => $item->value2);
    }
    
    $this->db->insert_batch('mytable', $insert_array);
    

    This will run a query whitch looks like this :

    INSERT INTO mytable (db_field1, db_field2) VALUES ('item1value1', 'item1value2'),  ('item2value1', 'item2value2'); 
    
    评论

报告相同问题?

悬赏问题

  • ¥20 simulink单相桥式整流电路
  • ¥35 问问51单片机流水灯的代码该怎么写
  • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
  • ¥15 stata webuse报错
  • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
  • ¥15 如何利用AI去除图片中的竹架子
  • ¥15 python 写个基金爬取的代码,自动卖出功能
  • ¥15 Linux系统启动不起来
  • ¥15 为什么运行仿真数码管不亮(语言-c语言)
  • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导