Skip to content

Commit

Permalink
Merge pull request #5667 from morozov/issues/5638
Browse files Browse the repository at this point in the history
Optimize MySQLSchemaManager::selectForeignKeyColumns()
morozov authored Sep 18, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents d3530a4 + 45f91ac commit 85c62c4
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Schema/MySQLSchemaManager.php
Original file line number Diff line number Diff line change
@@ -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 = ?'];
@@ -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);
}

0 comments on commit 85c62c4

Please sign in to comment.