【问题标题】:Different performance mysql and mariadb不同性能的mysql和mariadb
【发布时间】:2018-05-12 09:40:25
【问题描述】:

我正在将数据库从 mysql 迁移到 mariadb。 ruby on rails 应用程序正在使用 cancan 执行以下查询,因此我无法更改查询:

SELECT  DISTINCT COUNT(DISTINCT `buildings`.`id`)
    FROM  `buildings`
    INNER JOIN  `structure_subtypes`
        ON `structure_subtypes`.`id` = `buildings`.`structure_subtype_id`
    WHERE  `buildings`.`id` IN (33553, 33554, 33555, 33556, 33557,
                33558, 33559, 33560, 33561, 33562, 33563, 33564, 33565,
                33566, 33567, 33568, 33569, 33570, 33571, 33572, 33573,
                33574, 33575, 33576,+ ,..., 439515, 439521, 439600, 439602,
                439604, 440016, 440017, 440019, 440275, 440315, 440379,
                440403, 440405, 412987, 439889, 439969, 439980, 440216,
                440218, 440341, 440485, 440486, 440488, 440491, 440494,
                440501, 441458, 441498, 441519, 441536, 443387, 443389,
                443464, 433752, 440109, 440110, 440464, 443938, 440513,
                440514, 443391, 443394, 443353, 443364, 443401, 443486,
                175036, 175037
                          )
      AND  `buildings`.`client_id` IN (175, 47 , 162, 152, 170,
                104, 90, 127, 101, 51, 163, 81, 164, 165, 166, 172, 137,
                174, 106, 108, 161, 158, 169, 97, 123, 136, 102, 157,
                167, 135, 105, 171, 180, 120, 119, 118, 121, 110, 59,
                57, 178, 140, 138, 176, 141, 168, 126, 96, 117, 103, 133,
                173, 131, 179, 80, 100, 95, 116, 142, 147, 159, 160, 154,
                115, 153, 156, 91, 125, 144, 150, 93, 155, 149, 151, 146
                          )
      AND  `structure_subtypes`.`structure_type_id` IN (11, 12,
                13, 14, 15
                          )
      AND  (buildings.created_at >= '2016-12-31 23:00:00')
      AND  (buildings.created_at <= '2017-12-31 22:59:59');

问题似乎是IN部分中的项目太多

MySQL "IN" operator performance on (large?) number of values

但问题在于 MYSQL 和 mariadb 之间的性能差异

对于 MYSQL,只需不到 10 秒

 $ mysql -h 127.0.0.1 -u root -pPASSWORD database

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [database]> RESET QUERY CACHE;
Query OK, 0 rows affected (0.00 sec)

MySQL [database]> Bye

$ mysql -h 127.0.0.1 -u root -pPASSWORD database < select_query.sql

RESULTS

ouput explain:

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  buildings   range   PRIMARY,fk__buildings_client_id,index_buildings_on_workflow_state,fk__buildings_structure_subtype_id,index_buildings_on_workflow_state_and_created_at,index_buildings_on_client_id_and_created_at
                index_buildings_on_workflow_state_and_created_at    258 NULL    1001    Using index condition; Using where; Using temporary; Using filesort
1   SIMPLE  structure_subtypes  eq_ref  PRIMARY,index_structure_subtypes_on_structure_type_code,fk__structure_subtypes_structure_type_id    PRIMARY 4   antifraud.buildings.structure_subtype_id    1   Using where; Distinct

输出描述表

MySQL [antifraud]> describe buildings;
+--------------------------+---------------+------+-----+---------+----------------+
| Field                    | Type          | Null | Key | Default | Extra          |
+--------------------------+---------------+------+-----+---------+----------------+
| id                       | int(11)       | NO   | PRI | NULL    | auto_increment |
| client_id                | int(11)       | YES  | MUL | NULL    |                |
| observations             | varchar(2000) | YES  |     | NULL    |                |
| created_at               | datetime      | NO   | MUL | NULL    |                |
| updated_at               | datetime      | NO   |     | NULL    |                |
| workflow_state           | varchar(255)  | YES  | MUL | NULL    |                |
| structure_subtype_id        | int(11)       | YES  | MUL | NULL    |                |
| svs                      | tinyint(1)    | YES  |     | NULL    |                |
| parent_id                | int(11)       | YES  | MUL | NULL    |                |
| reference                | varchar(255)  | YES  |     | NULL    |                |
| soc_notify               | tinyint(1)    | YES  |     | NULL    |                |
| origin                   | int(11)       | YES  |     | NULL    |                |
| category                 | int(11)       | YES  |     | NULL    |                |
| ip_filtering             | int(11)       | YES  |     | NULL    |                |
| priority                 | int(11)       | YES  |     | NULL    |                |
| creator_id               | int(11)       | YES  | MUL | NULL    |                |
| external_id              | varchar(255)  | YES  |     | NULL    |                |
| duration_time            | float         | YES  |     | NULL    |                |
| reopening_at             | datetime      | YES  | MUL | NULL    |                |
| closed_at                | datetime      | YES  |     | NULL    |                |
| cbs_detection_id_legacy  | varchar(255)  | YES  |     | NULL    |                |
| cbs_callback_legacy      | varchar(255)  | YES  |     | NULL    |                |
| load_percentage          | float         | YES  |     | NULL    |                |
| items_in_special_domains | tinyint(1)    | NO   |     | 0       |                |
+--------------------------+---------------+------+-----+---------+----------------+
24 rows in set (0.00 sec)

..

MySQL [antifraud]> show index from buildings;
+-----------+------------+--------------------------------------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table     | Non_unique | Key_name                                         | Seq_in_index | Column_name       | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+--------------------------------------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| buildings |          0 | PRIMARY                                          |            1 | id                | A         |       72567 |     NULL | NULL   |      | BTREE      |         |               |
| buildings |          1 | fk__buildings_client_id                          |            1 | client_id         | A         |          82 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | index_buildings_on_workflow_state                |            1 | workflow_state    | A         |           8 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | fk__buildings_structure_subtype_id                  |            1 | structure_subtype_id | A         |          78 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | fk__buildings_parent_id                          |            1 | parent_id         | A         |        4535 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | fk__buildings_creator_id                         |            1 | creator_id        | A         |         168 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | index_buildings_on_workflow_state_and_created_at |            1 | workflow_state    | A         |           8 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | index_buildings_on_workflow_state_and_created_at |            2 | created_at        | A         |       72567 |     NULL | NULL   |      | BTREE      |         |               |
| buildings |          1 | index_buildings_on_created_at                    |            1 | created_at        | A         |       72567 |     NULL | NULL   |      | BTREE      |         |               |
| buildings |          1 | index_buildings_on_reopening_at                  |            1 | reopening_at      | A         |         806 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | index_buildings_on_client_id_and_created_at      |            1 | client_id         | A         |          80 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | index_buildings_on_client_id_and_created_at      |            2 | created_at        | A         |       72567 |     NULL | NULL   |      | BTREE      |         |               |
+-----------+------------+--------------------------------------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
12 rows in set (0.00 sec)

MySQL [antifraud]> Bye

对于 MariaDB,需要 1 分 10 秒

 $ mysql -h 127.0.0.1 -u root -pPASSWORD database



Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.1.26-MariaDB-1~jessie mariadb.org binary distribution

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [database]> RESET QUERY CACHE;
Query OK, 0 rows affected (0.00 sec)

MariaDB [database]> Bye

$ mysql -h 127.0.0.1 -u root -pPASSWORD database < select_query.sql

ouput explain:

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  buildings   range   PRIMARY,fk__buildings_client_id,index_buildings_on_workflow_state,fk__buildings_structure_subtype_id,index_buildings_on_workflow_state_and_created_at,index_buildings_on_client_id_and_created_at   index_buildings_on_workflow_state_and_created_at    258 NULL    1001    Using index condition; Using where; Using temporary; Using filesort
1   SIMPLE  structure_subtypes  eq_ref  PRIMARY,index_structure_subtypes_on_structure_type_code,fk__structure_subtypes_structure_type_id    PRIMARY 4   antifraud.buildings.structure_subtype_id    1   Using where; Distinct

..

output describe table

MariaDB [antifraud]> describe buildings;
+--------------------------+---------------+------+-----+---------+----------------+
| Field                    | Type          | Null | Key | Default | Extra          |
+--------------------------+---------------+------+-----+---------+----------------+
| id                       | int(11)       | NO   | PRI | NULL    | auto_increment |
| client_id                | int(11)       | YES  | MUL | NULL    |                |
| observations             | varchar(2000) | YES  |     | NULL    |                |
| created_at               | datetime      | NO   | MUL | NULL    |                |
| updated_at               | datetime      | NO   |     | NULL    |                |
| workflow_state           | varchar(255)  | YES  | MUL | NULL    |                |
| structure_subtype_id        | int(11)       | YES  | MUL | NULL    |                |
| svs                      | tinyint(1)    | YES  |     | NULL    |                |
| parent_id                | int(11)       | YES  | MUL | NULL    |                |
| reference                | varchar(255)  | YES  |     | NULL    |                |
| soc_notify               | tinyint(1)    | YES  |     | NULL    |                |
| origin                   | int(11)       | YES  |     | NULL    |                |
| category                 | int(11)       | YES  |     | NULL    |                |
| ip_filtering             | int(11)       | YES  |     | NULL    |                |
| priority                 | int(11)       | YES  |     | NULL    |                |
| creator_id               | int(11)       | YES  | MUL | NULL    |                |
| external_id              | varchar(255)  | YES  |     | NULL    |                |
| duration_time            | float         | YES  |     | NULL    |                |
| reopening_at             | datetime      | YES  | MUL | NULL    |                |
| closed_at                | datetime      | YES  |     | NULL    |                |
| cbs_detection_id_legacy  | varchar(255)  | YES  |     | NULL    |                |
| cbs_callback_legacy      | varchar(255)  | YES  |     | NULL    |                |
| load_percentage          | float         | YES  |     | NULL    |                |
| items_in_special_domains | tinyint(1)    | NO   |     | 0       |                |
+--------------------------+---------------+------+-----+---------+----------------+
24 rows in set (0.00 sec)

MariaDB [antifraud]> show index from buildings;
+-----------+------------+--------------------------------------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table     | Non_unique | Key_name                                         | Seq_in_index | Column_name       | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+--------------------------------------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| buildings |          0 | PRIMARY                                          |            1 | id                | A         |       71923 |     NULL | NULL   |      | BTREE      |         |               |
| buildings |          1 | fk__buildings_client_id                          |            1 | client_id         | A         |          82 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | index_buildings_on_workflow_state                |            1 | workflow_state    | A         |           8 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | fk__buildings_structure_subtype_id                  |            1 | structure_subtype_id | A         |          80 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | fk__buildings_parent_id                          |            1 | parent_id         | A         |        4495 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | fk__buildings_creator_id                         |            1 | creator_id        | A         |         170 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | index_buildings_on_workflow_state_and_created_at |            1 | workflow_state    | A         |           8 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | index_buildings_on_workflow_state_and_created_at |            2 | created_at        | A         |       71923 |     NULL | NULL   |      | BTREE      |         |               |
| buildings |          1 | index_buildings_on_created_at                    |            1 | created_at        | A         |       71923 |     NULL | NULL   |      | BTREE      |         |               |
| buildings |          1 | index_buildings_on_reopening_at                  |            1 | reopening_at      | A         |         826 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | index_buildings_on_client_id_and_created_at      |            1 | client_id         | A         |          80 |     NULL | NULL   | YES  | BTREE      |         |               |
| buildings |          1 | index_buildings_on_client_id_and_created_at      |            2 | created_at        | A         |       71923 |     NULL | NULL   |      | BTREE      |         |               |
+-----------+------------+--------------------------------------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
12 rows in set (0.00 sec)

我正在使用显示变量中的变量,但我不知道如何进行

【问题讨论】:

  • 在 MySQL 和 mariadb 上显示来自 explain select ... 的输出,以及每个表的 show create table tablename 的输出(以及 MySQL 和 Maria 之间后者的任何差异)
  • 添加了解释和描述
  • Describe 不会像 show create table ... \G 那样显示索引详细信息。更新:我看到你确实显示了索引,谢谢。不过,这更难阅读
  • @anquegi - DESCRIBE 和 SHOW INDEX 输出有什么不同吗?
  • 请提供SHOW CREATE TABLE structure_subtypes

标签: mysql sql mariadb


【解决方案1】:

试试from buildings use index (primary) inner join ...

【讨论】:

  • 它现在在 maridb 上运行得非常快,¿我应该在 id/key 列中添加一个主索引?
  • 是的,这是真的,但在 mysql 中不需要添加它,而在 mariadb 上它是需要的。我不能更改查询
  • 对不起,我忘了你说过。我不知道为什么它使用 index_buildings_on_workflow_state_and_created_at 而不是主键或 client_id。您可以尝试删除该索引(但我不知道它可能用于哪些其他查询)或添加 (id,created_at) 或 (client_id,created_at) 索引
  • 我尝试添加该索引,但没有效果
  • 你用的是什么引擎? SHOW CREATE TABLEDESCRIBE 更具描述性!
猜你喜欢
  • 1970-01-01
  • 2015-10-11
  • 2021-09-27
  • 2020-09-09
  • 2019-04-09
  • 2018-11-17
  • 1970-01-01
  • 1970-01-01
  • 2013-09-12
相关资源
最近更新 更多