Best practice: Import mySQL file in PHP; split queries

后端 未结 13 612
Happy的楠姐
Happy的楠姐 2020-11-29 00:08

I have a situation where I have to update a web site on a shared hosting provider. The site has a CMS. Uploading the CMS\'s files is pretty straightforward using FTP.

<
13条回答
  •  無奈伤痛
    2020-11-29 00:27

    When StackOverflow released their monthly data dump in XML format, I wrote PHP scripts to load it into a MySQL database. I imported about 2.2 gigabytes of XML in a few minutes.

    My technique is to prepare() an INSERT statement with parameter placeholders for the column values. Then use XMLReader to loop over the XML elements and execute() my prepared query, plugging in values for the parameters. I chose XMLReader because it's a streaming XML reader; it reads the XML input incrementally instead of requiring to load the whole file into memory.

    You could also read a CSV file one line at a time with fgetcsv().

    If you're inporting into InnoDB tables, I recommend starting and committing transactions explicitly, to reduce the overhead of autocommit. I commit every 1000 rows, but this is arbitrary.

    I'm not going to post the code here (because of StackOverflow's licensing policy), but in pseudocode:

    connect to database
    open data file
    PREPARE parameterizes INSERT statement
    begin first transaction
    loop, reading lines from data file: {
        parse line into individual fields
        EXECUTE prepared query, passing data fields as parameters
        if ++counter % 1000 == 0,
            commit transaction and begin new transaction
    }
    commit final transaction
    

    Writing this code in PHP is not rocket science, and it runs pretty quickly when one uses prepared statements and explicit transactions. Those features are not available in the outdated mysql PHP extension, but you can use them if you use mysqli or PDO_MySQL.

    I also added convenient stuff like error checking, progress reporting, and support for default values when the data file doesn't include one of the fields.

    I wrote my code in an abstract PHP class that I subclass for each table I need to load. Each subclass declares the columns it wants to load, and maps them to fields in the XML data file by name (or by position if the data file is CSV).

提交回复
热议问题