Skip to content

Instantly share code, notes, and snippets.

@mbeccati
Forked from inoric/bench_pdo.php
Last active August 28, 2020 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbeccati/6f2cd094c38b094cb4bfc4c0245a23ab to your computer and use it in GitHub Desktop.
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)
<?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;
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