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

Optimize MySQLSchemaManager::selectForeignKeyColumns() #5667

Merged
merged 1 commit into from
Sep 18, 2022
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
12 changes: 9 additions & 3 deletions src/Schema/MySQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,7 @@ protected function selectForeignKeyColumns(string $databaseName, ?string $tableN
FROM information_schema.key_column_usage k /*!50116
INNER JOIN information_schema.referential_constraints c
ON c.CONSTRAINT_NAME = k.CONSTRAINT_NAME
AND c.TABLE_NAME = k.TABLE_NAME
AND c.CONSTRAINT_SCHEMA = k.TABLE_SCHEMA */
AND c.TABLE_NAME = k.TABLE_NAME */
SQL;

$conditions = ['k.TABLE_SCHEMA = ?'];
Expand All @@ -495,7 +494,14 @@ protected function selectForeignKeyColumns(string $databaseName, ?string $tableN

$conditions[] = 'k.REFERENCED_COLUMN_NAME IS NOT NULL';

$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY k.ORDINAL_POSITION';
$sql .= ' WHERE ' . implode(' AND ', $conditions)
// The schema name is passed multiple times in the WHERE clause instead of using a JOIN condition
// in order to avoid performance issues on MySQL older than 8.0 and the corresponding MariaDB versions
// caused by https://bugs.mysql.com/bug.php?id=81347.
// Use a string literal for the database name since the internal PDO SQL parser
// cannot recognize parameter placeholders inside conditional comments
. ' /*!50116 AND c.CONSTRAINT_SCHEMA = ' . $this->_conn->quote($databaseName) . ' */'
. ' ORDER BY k.ORDINAL_POSITION';

return $this->_conn->executeQuery($sql, $params);
}
Expand Down