I'm looking for the best way to Insert big data.
Does anyone know how to insert this data the most efficient. I'm looking for the most elegant way. This works but is there a faster way or more elegant way?
function SaveData($bigDataArray) {
foreach ($bigDataArray as $item) { //$bigDataArray items (5000+)
$this->Save($item['name'], $item['link'], $item['id']);
}
}
function Save($name, $link, $id) {
$this->db->beginTransaction();
try {
$result = $this->db->prepare('INSERT INTO table1 (songid, songTitle, youtubeid) VALUES (null, :name, :link);');
$result->execute([':name' => $name, ':link' => $link]);
$lastsId = $this->db->lastInsertId();
$result = $this->db->prepare('INSERT INTO table2 (linkid, id, lastsId) VALUES (null, :playlistid, :lastsId);');
$result->execute([':playlistid' => $id, ':$lastsId' => $lastsId]);
$result = $this->db->prepare('UPDATE table3 SET count = count + 1 WHERE id = :id;');
$result->execute([':id' => $id]);
$this->db->commit();
} catch (PDOException $e) {
$this->db->rollBack();
return false;
}
return true;
}