Skip to content

Loading constraints from database in framework-foreign-key library takes too long #6005

@maksymhopei

Description

@maksymhopei

In order to check foreign keys in separate databases for "Scalabale OMS" and "Scalable Checkout" modules Magento loads constraints from the database with the following query:

SELECT 
  `constraints`.`CONSTRAINT_NAME` AS `name`,
  `info`.`TABLE_NAME` AS `table_name`,
  `info`.`REFERENCED_TABLE_NAME` AS `reference_table_name`,
  `info`.`COLUMN_NAME` AS `field_name`,
  `info`.`REFERENCED_COLUMN_NAME` AS `reference_field_name`,
  `constraints`.`DELETE_RULE` AS `delete_strategy` 
FROM 
  `information_schema`.`KEY_COLUMN_USAGE` AS `info` 
  INNER JOIN `information_schema`.`REFERENTIAL_CONSTRAINTS` AS `constraints` 
    ON constraints.CONSTRAINT_NAME = info.CONSTRAINT_NAME AND constraints.CONSTRAINT_SCHEMA = info.CONSTRAINT_SCHEMA
WHERE 
  (info.REFERENCED_TABLE_NAME IS NOT NULL)
  AND (info.CONSTRAINT_SCHEMA = '<Database Name>')

(See: \Magento\Framework\ForeignKey\Config\DbReader::getDbConstraints)

This query might take quite a long time (minutes) on a mysql server with many databases in use.
In our setup, which is the following:

> select count(*) from `information_schema`.`KEY_COLUMN_USAGE`;
|    66045 |
> select count(*) from `information_schema`.`REFERENTIAL_CONSTRAINTS`;
|    21222 |

this query takes: 1 min 26.70 sec.

It is also executed twice or more by Magento for each connection.
This makes the page load after clearing caches way too long and the website impossible to work with.

Preconditions

Magento EE 2.1.0 installed.
Mysql server with many databases 30+.

Steps to reproduce

  1. Clear cache in any way (cli, admin panel)
  2. Open any page (i.e. Homepage)

Expected result

The page takes few seconds to load.

Actual result

The page takes minutes to load.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions