使用此php脚本插入超过1000条记录的最快方法是什么[关闭]

I want to insert more than 1000 records in once with this php code and it takes a long time to insert more than 1000 records. Can this faster?

$original = $_POST['contact'];
$output = array();
reset($original);

foreach($original as $value) {
    if(strpos($value, ',') === false)
        $output[] = $value; // add value straight away if it doesn't need splitting
    else {

        //split string by comma and trim whitespace from pieces
        $bits = explode(',', $value);
        foreach($bits as $bit) {
        $output[] = trim($bit);
    }
}

//now loop through $output array to get desired result:

foreach($output as $value) {

    echo $value."<br>$msg";
    $query = "INSERT INTO inbox (
        check_file,
        onderwerp,
        fromm,
        post,
        owner,
        date
    ) VALUES ( 
        '$check',
        '$onderwerp',
        '$session->username',
        '$msg',
        '$value',
        '$date'
    )";
    mysqli_query($database->connection,$query) or die (mysqli_error());

    $query = "INSERT INTO outbox (  
        check_file,
        onderwerp,
        owner,
        post,
        ontvanger,
        date
    ) VALUES (
        '$check',
        '$onderwerp',
        '$session->username',
        '$msg',
        '$value',
        '$date'
    )";
    mysqli_query($database->connection,$query) or die (mysqli_error());

}

Use mysqls multi insert functions. You can, instead of executing each query independently, compile all the INSERTS into a single string:

insert into XXX VALUES (1, 2, 'query_1'), (3, 4, 'query_2'), (3, 5, 'query_3')

This will greatly increase insert speed and reduce unneeded network traffic.

Here are my two cents:

There are many ways to load a lot of data into MYSQL, here are two!

Method 1

Add each record programmaticaly like you are doing. What you can do to make this better, at least use a MySQLi statement (prepare one) and then bind your variables, it'll be a whole lot faster.

Method 2

Read the input into a formatted output file that is processed and use a mysql LOAD statement to load the file directly into MySQL.

These are many other ways, but i'd try #1 first since you probably don't want to rewrite everything, and #2 after that if you still need more speed.

Good luck