In Laravel, Is there any way to prepare the query for DB::selectOne?

Viewed 10

I'm trying to insert and return data in a loop using DB::selectOne The below code works fine but is not efficient. It runs faster if I'm using PDO to prepare the query first and then just loop thru to pass in the data to be inserted. Is there any way to prepare the query for selectOne then looping thru to insert the data?

Not efficient

$query = DB::raw($sql);
$result = [];
for ($i=0 ; $i<10000 ; $i++) {
  $result[] = DB::selectOne($query, array_values($rows[i]));
}

More efficient

$pdo = DB::connection()->getPdo();
$query = $pdo->prepare($sql);
for ($i=0 ; $i<10000 ; $i++) {
 $query->execute(array_values($rows[i]));
 $result[] = $query->fetch();
}
0 Answers
Related