[DATABASE] Add explicit indices for all foreign keys

This adds a requirement for all definitions that have foreign keys to also
require indices for all source (local) attributes mentioned in foreign keys.

MariaDB/MySQL creates indices for source attributes automatically, so this
serves as a way to get rid of those automatic indices and create clean explicit
ones instead.

In PostgreSQL, most of the time, indices on the source are necessary to
decrease performance penalty of foreign keys (like in MariaDB), but they aren't
created automatically, so this serves to remove that difference between
PostgreSQL and MariaDB.
This commit is contained in:
Alexei Sorokin
2020-07-31 16:36:40 +03:00
parent 0bfa747382
commit 92e8c40c55
22 changed files with 142 additions and 58 deletions

View File

@@ -259,12 +259,9 @@ class MysqlSchema extends Schema
<<<END
SELECT INDEX_NAME AS `key_name`, INDEX_TYPE AS `key_type`, COLUMN_NAME AS `col`
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = '{$schema}' AND TABLE_NAME = '{$table}'
WHERE TABLE_SCHEMA = '{$schema}'
AND TABLE_NAME = '{$table}'
AND NON_UNIQUE IS TRUE
AND INDEX_NAME NOT IN (
SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME IS NOT NULL
)
ORDER BY SEQ_IN_INDEX;
END
);