0

我有一个包含 26,000 行的 csv 文件,我正在遍历行并更新包含 250,000 多条记录的表中的记录(有时是多个)。目前,它的年龄!我想知道是否有另一种方法可以更快地做到这一点(在代码或 mysql/etc 中)

$row = 1;
if (($handle = fopen("zip-codes-database-DELUXE-BUSINESS2.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            if($row> 1){


                # GET THE AREACODE FROM data 20

                # name is: 21

                $insert = "UPDATE ".TBLPREFIX."cities SET data = '".escape(serialize($data))."' WHERE area_code = ".$data[20]." AND title = '".trim(strtoupper($data[21]))."'";
                mysql_query($insert) or die(mysql_error());
            }
            $row++;
    }
    fclose($handle);
}
4

1 回答 1

3

Based on nothing I might try:

  1. get the csv into a table via cmd line or 'load data infile'
  2. update the records into a temp table using a 'insert ... select' where you join the old and new
  3. move the temp table back onto the original (delete/rename)

Seems like it would be faster.. if a bit kludgy.

于 2013-01-02T21:00:31.987 回答