Skip to content
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

FIX Run ANALYZE TABLE before fetching table stats #11334

Merged
Merged
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
32 changes: 15 additions & 17 deletions src/ORM/Connect/MySQLDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,23 +569,21 @@ public function clearTable($table)
// Not simply using "TRUNCATE TABLE \"$table\"" because DELETE is a lot quicker
// than TRUNCATE which is very relevant during unit testing. Using TRUNCATE will lead to an
// approximately 50% increase it the total time of running unit tests.
//
// Using max(ID) to determine if the table should reset its auto-increment, rather than using
// SELECT "AUTO_INCREMENT" FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?
// after deleting from the table, because in MySQL 8, under certain conditions, notably
// when running behat, sometimes the auto-increment was being reset to 2 for unknown reasons
$self = $this;
$fn = function () use ($self, $table) {
$maxID = $self->query("SELECT MAX(ID) FROM \"$table\"")->value();
$self->query("DELETE FROM \"$table\"");
if ($maxID > 0) {
$self->query("ALTER TABLE \"$table\" AUTO_INCREMENT = 1");
}
};
if ($this->supportsTransactions()) {
$this->withTransaction($fn);
} else {
$fn();
$this->query("DELETE FROM \"$table\"");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to keep the comment about not using truncate

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


// Check if resetting the auto-increment is needed

// First run ANALYZE TABLE to reset table stats which are cached for 24 hours by
// default in MySQL 8
$this->query("ANALYZE TABLE \"$table\"");

$autoIncrement = $this->preparedQuery(
'SELECT "AUTO_INCREMENT" FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?',
[ $this->getSelectedDatabase(), $table]
)->value();

if ($autoIncrement > 1) {
$this->query("ALTER TABLE \"$table\" AUTO_INCREMENT = 1");
}
}
}
Loading