E
E
emerysh2017-07-09 11:16:32
PHP
emerysh, 2017-07-09 11:16:32

Database write speed?

I get json - I decode it - I update it in the database through foreach - about 2-3k records - it takes 3-5 minutes. What is the problem?
Is it possible to speed up to 1 min?
Is this normal speed?

The code itself
$url = $site;
  $file = file_get_contents($url);
  $jsondate = json_decode($file, true);


  foreach ($jsondate as $k => $value) {
    $keys = array_values($value);
    foreach ($keys as $bo) {
      $name= $bo['name'];
      $price = $bo['price'];
      ///////////////////////
      $name= mysqli_real_escape_string($link, $name);
      ///////////////////////
      $sql = mysqli_query($link,"UPDATE 	`price` SET `price`='$price' WHERE `name`='$name'");
    }
  }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
Boris Korobkov, 2017-07-09
@BorisKorobkov

Abnormal.
1. It is necessary to update by the primary key (ID), not by name
2. It is necessary to execute not 3-4K requests, but 4 pcs. How to form 1 UPDATE query with a thousand values?

A
Arris, 2017-07-09
@Arris

1. READ https://dev.mysql.com/doc/refman/5.7/en/create-ind...
2. USE https://dev.mysql.com/doc/refman/5.7/en/insert- on-...

F
FanatPHP, 2017-07-10
@FanatPHP

As always, a million answers, all not in cash.
The correct answer is to wrap requests in a transaction.
All other changes in the code are not related to the fundamental acceleration, and are made only to make the code look less shameful.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link->begin_transaction();
$stmt = $link->prepare("UPDATE `price` SET `price`=? WHERE `name`=?");
$stmt->bind_param("ss", $price, $name);
foreach ($jsondate as $k => $value) {
    foreach ($value as $bo) {
        $name = $bo['name'];
        $price = $bo['price'];
        $stmt->execute();
    }
}
$link->commit();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question