PDO: call cost prepare () in a loop?

I am writing a database class for my site with functions such as fetchOne , fetchAll that cook, execute (+ bind) and query all in one, so I do not need to individually name those functions each time. Some cron jobs on my site run thousands or even millions of requests within a loop.

Will using my class result in the operator having to be processed every iteration of the loop, or does the PDO “remember” that the request has already been prepared? Will this significantly affect performance, and if it could be a solution, just provide a function that passes the database instance and do something like $db->getDb()->prepare($query); out of cycle? Or is there a better solution?

Function example:

 public function fetchOne($query, $params = array(), $fetchMode = PDO::FETCH_ASSOC) { $stmt = self::prepareExecute($query, $params); $result = $stmt->fetch($fetchMode); if (count($result) < 1) $result = FALSE; $stmt->closeCursor(); unset($stmt); return($result); } 
+4
source share
1 answer

you don’t want to “re-prepare” the same request multiple times. this is the purpose of the preparatory statement. you prepare it once, bind the variable (s), then simply switch the values ​​of the variables and re-execute each iteration of the loop.

http://www.php.net/manual/en/pdostatement.bindparam.php

doing it this way will significantly increase productivity compared to alternative methods.

+5
source

Source: https://habr.com/ru/post/1395112/


All Articles