Mam tablicę 650 wierszy. Wstawienie tego przy użyciu PDO zajmuje od 10 do 15 sekund na moim lokalnym komputerze. To bardzo wolno. Czy to z powodu odczytu/zapisu dysku? Czy może to być coś innego?Wstawianie wartości z multidim. tablica używająca PDO zajmuje dużo czasu. Czy istnieje lepszy sposób?
To mój array (pierwsze 4 rzędy):
Array
(
[0] => Array
(
[0] => 3
[1] => 1
)
[1] => Array
(
[0] => 3
[1] => 2
)
[2] => Array
(
[0] => 3
[1] => 5
)
[3] => Array
(
[0] => 8
[1] => 1
)
)
I to jest mój kod:
$stmt = $this->db->prepare("INSERT INTO sl_link_store_category (item_a_ID, item_b_ID) VALUES (:item_a_ID, :item_b_ID)");
foreach($my_array as $row) {
$stmt->execute(array(':item_a_ID' => $row[0], ':item_b_ID' => $row[1]));
}
ROZWIĄZANIE
Dla tych, którzy zastanawiają się, jej eis mój rozwiązanie do wstawiania wielu wierszy
przy użyciu tylko jednego $stmt->execute
:
$input_arr; // This array one has lots of values
$sql = "INSERT INTO sl_link_store_category (field_a, field_b) VALUES ";
$i = 0;
// I create the query string with unique prepared values
// I could probably have used a for loop since I'm not using any
// values from $row
foreach($input_arr as $row) {
$i++;
$sql .= "(:field_a_$i, :field_a_$i), ";
}
// Remove the last comma (and white space at the end)
$sql = substr(trim($sql), 0, -1);
$stmt = $this->db->prepare($sql);
// I need to create a new associative array with field name
// matching the prepared values in the SQL statement.
$i = 0;
$arr = array();
foreach($input_arr as $row) {
$i++;
$arr[":field_a_$i"] = $row[0];
$arr[":field_b_$i"] = $row[1];
}
$stmt->execute($arr);
}
Co silnik bazy danych (MyISAM, InnoDB ...) używasz? – Matthew
@konforce - Używam InnobDB. – Steven
, możesz rozważyć ustawienie 'innodb_flush_log_at_trx_commit = 2' i' sync_binlog = 0' w konfiguracji MySQL na maszynie programistycznej. Pozwoli to zminimalizować spłukiwanie dysku i może przyspieszyć działanie aplikacji o wiele czynników. – Matthew