mysql - Insert data in database fast php? -


this question has answer here:

i have list of 10.000 records i'd insert mysql database.

i have tried insert them foreach , simple insert took lot of time insert.

is there method insert these rows faster?

$contents = file_get_contents("table.txt.001"); $pollfields = explode(',', $contents);  foreach ($pollfields $key) {     $query    = "insert `table` (`id`, `name`) values (null, '$key')";     mysqli_query($conn, $query) or trigger_error(mysqli_error()." in ".$query); } 

i have read somehting puting more values

"insert `table` (`id`, `name`) values (null, '$key'),(null, '$key'),(null, '$key')"; 

the problem how knows each time insert next 5 example if use foreach.

create tempory table same main table

then insert entries in tempory table .

$contents = file_get_contents("table.txt.001"); $pollfields = explode(',', $contents); $sql = ""; foreach ($pollfields $key) {    $sql .= " (null , ".$key.") , "; }  $sql = trim($sql,',');  $query = "insert `temp_table` (`id`, `name`) values " . $sql ;  mysqli_query($conn, $query) or trigger_error(mysqli_error()." in ".$query); 

then copy whole data main table ,

insert `table` (`id`, `name`) select `id` , `name` `temp_table` 

and need truncate temp_table .

above process faster if want insert millions of records .


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -