1

CSVをループして、いくつかのレコードの名前フィールドをテーブルに挿入/更新しています。スクリプトは、レコードを挿入することを意味し、レコードが存在する場合は、名前フィールドのみを更新します。

より大きなCSVファイルにはかなりの時間がかかるため、このコードを、レコードの名前フィールドのみを更新するON DUPLICATEKEYUPDATEコマンドを使用して複数のINSERTクエリに変更できるかどうか疑問に思いました。

CSVには、テーブルのすべてのフィールドが含まれているわけではなく、主キーと名前のフィールドのみが含まれています。そのため、この場合、REPLACEは機能しません。

if (($handle = fopen($_FILES['csv']['tmp_name'], "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $title = 'Import: '.date('d-m-Y').' '.$row;

            # CHECK IF ALREADY EXISTS
            $explode = explode('-',$data[0]);
            $areacode = $explode[0];
            $exchange = $explode[1];
            $number = $explode[2];

            $update = "INSERT INTO ".TBLPREFIX."numbers SET
            area_code = ".$areacode.",
            exchange = ".$exchange.",
            number = ".$number.",
            status = 1,
            name = '".escape($data[1])."'
            ON DUPLICATE KEY UPDATE name = '".escape($data[1])."'";
            mysql_query($update) or die(mysql_error());
            $row++;


        }
        fclose($handle);
        $content .= success($row.' numbers have been imported.');
    }
4

1 に答える 1

0

Open a transaction before you start inserting, and commit it after you are done. This way the database can optimize the write operation on disk, because it takes place in a separate memory space. Without a transaction, all single queries are automatically committed at once and effective for every other query.

At least I hope you are using InnoDB as a storage engine - MyISAM does not support transactions and has other significant drawbacks. You should avoid it if possible.

于 2013-01-26T15:48:47.340 に答える