duanmeng2842 2017-10-03 11:40
浏览 74
已采纳

如何更快地使用nullif插入Mysql?

i am loading a CSV file through PHP and inserting into a temporary table, this is what my loading PHP script and MySQL query.

    $i=0;
    while (($data = fgetcsv($source, 1000, ",")) !== FALSE)
        {   
            if($i!=0) {
                $column1=$data[0];
                $column2=$data[1];
                $column3=$data[2];
                $column4=$data[3];
                $column5=$data[4];
                $column6=$data[5];
                $query= "INSERT INTO temp_table(column1,column2,column3,column4,column5,column6,load_datetime)
    VALUES (nullif('$column1',''), nullif('$column2',''), nullif('$column3',''), nullif('$column4',''), nullif('$column5',''), nullif('$column6',''),now())";
                mysql_query($query) or die(mysql_error());
            }
            $i++;
        }

and my CSV file has 25000 records, this is a sample row from my CSV

        column1,column2,column3,column4,column5,column6
        ,,,13:57:18,,23:00:19

the problem i face is, it takes more than 10 minutes when i load the CSV and in between the page goes blank. My PHP settings: upload_max_filesize: 100M post_max_size:100M max_execution_time:1000 max_input_time:1000.

this my table detail

   CREATE TABLE temp_table (
       id int(11) NOT NULL,
       column1 time DEFAULT NULL,
       column2 time DEFAULT NULL,
       column3 time DEFAULT NULL,
       column4 time DEFAULT NULL,
       column5 time DEFAULT NULL,
       column6 time DEFAULT NULL,
       load_datetime datetime DEFAULT NULL
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

and when i check the temp_table it has inserted around 8000 records. please help me with an approach to insert faster.

  • 写回答

2条回答 默认 最新

  • dongyong2063 2017-10-03 12:01
    关注

    I would be recommended to you stop use mysql extension (mysql_*)
    and start use mysqli or PDO.

    Try to group data of a few rowns into one query.

    Example:

    $i  = 0;
    $i2 = 0;
    $sql = "INSERT INTO temp_table(column1,column2,column3,column4,column5,column6,load_datetime) VALUES ";
    $values = [];
    while (($data = fgetcsv($source, 1000, ",")) !== FALSE) {
        if(++$i == 1)
            continue;
        else {
            $data = array_map(function($item) {
                return $item == '' ? 'NULL' : "'$item'";
            }, $data);
            $values[] = "(" . implode(", ", $data) . ', now()' . ")";
            if (++$i2 == 500) {
                mysql_query ($sql . implode(', ', $values)) or die(mysql_error());
                $values = [];
                $i2 = 0;
            }
        }
    }
    if (!empty($values)) {
        mysql_query ($sql . implode(', ', $values)) or die(mysql_error());
        $values = [];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号