Skip to content

Performance Improvement: Database Query Log Memory Optimization #56506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: 12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ class Connection implements ConnectionInterface
*/
protected $queryLog = [];

/**
* The maximum number of queries to keep in the query log.
*
* @var int
*/
protected $maxQueryLogSize = 1000;

/**
* Indicates whether queries are being logged.
*
Expand Down Expand Up @@ -858,6 +865,10 @@ public function logQuery($query, $bindings, $time = null)

if ($this->loggingQueries) {
$this->queryLog[] = compact('query', 'bindings', 'time');

if (count($this->queryLog) > $this->maxQueryLogSize) {
$this->queryLog = array_slice($this->queryLog, -$this->maxQueryLogSize, $this->maxQueryLogSize, false);
}
}
}

Expand Down Expand Up @@ -1569,6 +1580,29 @@ public function logging()
return $this->loggingQueries;
}

/**
* Set the maximum query log size.
*
* @param int $size
* @return $this
*/
public function setMaxQueryLogSize(int $size)
{
$this->maxQueryLogSize = max(1, $size);

return $this;
}

/**
* Get the maximum query log size.
*
* @return int
*/
public function getMaxQueryLogSize()
{
return $this->maxQueryLogSize;
}

/**
* Get the name of the connected database.
*
Expand Down
31 changes: 31 additions & 0 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,37 @@ protected function getMockConnection($methods = [], $pdo = null)

return $connection;
}

public function testQueryLogSizeLimit()
{
$connection = $this->getMockConnection();
$connection->enableQueryLog();
$connection->setMaxQueryLogSize(3);

$this->assertEquals(3, $connection->getMaxQueryLogSize());

for ($i = 0; $i < 5; $i++) {
$connection->logQuery('SELECT * FROM test WHERE id = ?', [$i], 10.0);
}

$queryLog = $connection->getQueryLog();

$this->assertCount(3, $queryLog);
$this->assertEquals([2], $queryLog[0]['bindings']);
$this->assertEquals([3], $queryLog[1]['bindings']);
$this->assertEquals([4], $queryLog[2]['bindings']);
}

public function testQueryLogSizeLimitMinimumValue()
{
$connection = $this->getMockConnection();

$connection->setMaxQueryLogSize(0);
$this->assertEquals(1, $connection->getMaxQueryLogSize());

$connection->setMaxQueryLogSize(-5);
$this->assertEquals(1, $connection->getMaxQueryLogSize());
}
}

class DatabaseConnectionTestMockPDO extends PDO
Expand Down
Loading