-
-
Save mbeccati/6f2cd094c38b094cb4bfc4c0245a23ab to your computer and use it in GitHub Desktop.
PHP PDO slow fetch with a lot of bind parameters benchmark (SELECT IN)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
$db = new PDO('pgsql:host=localhost;dbname=test', 'test', 'test'); | |
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$N = 65535; | |
$vals = range(1, $N); | |
$valSql = \implode(', ', \array_fill(0, \count($vals), '?')); | |
$sql = <<<SQL | |
SELECT * FROM thingies WHERE id IN ({$valSql}) | |
SQL; | |
$prepStart = \microtime(true); | |
$query = $db->prepare($sql); | |
$execStart = \microtime(true); | |
$query->execute($vals); | |
$fetchStart = \microtime(true); | |
$ret = $query->fetchAll(); | |
echo 'Got rows: '.\count($ret).PHP_EOL; | |
$end = \microtime(true); | |
$prepDuration = \number_format($execStart - $prepStart, 3); | |
$execDuration = \number_format($fetchStart - $execStart, 3); | |
$fetchDuration = \number_format($end - $fetchStart, 3); | |
//var_dump($ret); | |
echo "[Timings] prep: {$prepDuration}, exec: {$execDuration}, fetch: {$fetchDuration}".PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE thingies ( | |
id SERIAL PRIMARY KEY, | |
str1 TEXT NOT NULL, | |
str2 TEXT NOT NULL, | |
num INTEGER NOT NULL | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment