-
-
Save nikic/a55b70d6b7c1acf6785c7e50c37754dc to your computer and use it in GitHub Desktop.
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 | |
class PDOConnection extends PDO { | |
public function prepare($prepareString, $driverOptions = []): PDOStatement|false | |
{ | |
return parent::prepare($prepareString, $driverOptions); | |
} | |
} | |
print(memory_get_usage() . "\n"); | |
$dbh = new PDOConnection('sqlite:memory'); | |
//$dbh->exec('CREATE TABLE user (active BOOL, admin BOOL, created_at DATE, updated_at DATE)'); | |
print(memory_get_usage() . "\n"); | |
for($i = 0; $i < 100; $i++) { | |
// Obviously we normally would not generate a new identical prepared statement inside | |
// of a loop, but this demonstrates the problem well, and an ORM using PDOConnection | |
// (e.g. Laravel Eloquent) does actually generate a new prepared statement for each | |
// query, expecting that the statement will be properly destroyed once all references | |
// to it are destroyed. | |
$stmt = $dbh->prepare( | |
"INSERT INTO user (active, admin, created_at, updated_at) VALUES (?, ?, ?, ?)"); | |
if($i == 0) { | |
print(memory_get_usage() . "\n"); | |
} | |
$stmt->execute([1, 1, '2018-01-01', '2018-01-01']); | |
if($i == 0) { | |
print(memory_get_usage() . "\n"); | |
} | |
} | |
$stmt = null; | |
print(memory_get_usage() . "\n"); | |
$dbh = null; | |
print(memory_get_usage() . "\n"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment