MYSQL query performs very slow

前端 未结 8 1283
一个人的身影
一个人的身影 2020-12-10 01:52

I have developed a user bulk upload module. There are 2 situations, when I do a bulk upload of 20 000 records when database has zero records. Its taking about 5 hours. But w

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 02:19

    For my work, I have to add daily one CSV with 524 Columns and 10k records. When I have try to parse it and add the record with php, it was horrible.

    So, I propose to you to see the documentation about LOAD DATA LOCAL INFILE

    I copy/past my own code for example, but adapt him to your needs

    $dataload = 'LOAD DATA LOCAL INFILE "'.$filename.'"
                    REPLACE
                    INTO TABLE '.$this->csvTable.' CHARACTER SET "utf8"
                    FIELDS TERMINATED BY "\t"
                    IGNORE 1 LINES
                ';
    
    $result = (bool)$this->db->query($dataload);
    

    Where $filename is a local path of your CSV (you can use dirname(__FILE__) for get it )

    This SQL command is very quick (just 1 or 2 second for add/update all the CSV)

    EDIT : read the doc, but of course you need to have an uniq index on your user table for "replace" works. So, you don't need to check if the user exist or not. And you don't need to parse the CSV file with php.

提交回复
热议问题