简体   繁体   English

从 MySQL 5.7 迁移到 MARIADB 10.6 后查询慢。 执行计划指标问题

[英]Slow query after migrating from MySQL 5.7 to MARIADB 10.6. Execution plan index problem

We want to migrate a LAMP server from MySQL 5.7 to MariaDB 10.6.我们希望将 LAMP 服务器从 MySQL 5.7 迁移到 MariaDB 10.6。 We cloned the server, installed MariaDB and migrated the database.我们克隆了服务器,安装了 MariaDB 并迁移了数据库。 I was expecting some speed improvements with SQL queries, but my surprise is that our app is slower with the MariaDB.我期待 SQL 查询的速度有所提高,但令我惊讶的是,我们的应用程序使用 MariaDB 时速度较慢。 I'm trying to analyze some queries, I expose you a specific case.我正在尝试分析一些查询,我向您展示一个具体案例。

We've a VIEW that we use in many other queries, something like this:我们有一个在许多其他查询中使用的 VIEW,如下所示:

ALTER ALGORITHM = MERGE DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `GT_VWCalendariProfessionalInc` AS
   SELECT cpInc.prof, cpInc.start_date, cpInc.end_date
   FROM GT_CalendariProfessional cpInc
   JOIN CTR_Motius ON cpInc.colA = CTR_Motius.colA
   WHERE cpInc.necessitat IS NULL AND ...

Key indexes in cpInc table: ix_necessitat (column necessitat), ix_professional (column prof), ix_outerjoin_nec_dates (columns necessitat, start_date, end_date) cpInc 表中的关键索引:ix_necessitat (column necessitat), ix_professional (column prof), ix_outerjoin_nec_dates (columns necessitat, start_date, end_date)

Using this view from external query with MySQL is faster than with MariaDB.使用 MySQL 的外部查询视图比使用 MariaDB 更快。 The query is like this:查询是这样的:

SELECT kal.prof, kal.start_date, kal.end_date
FROM GT_CalendariProfessional kal
LEFT JOIN GT_VWCalendariProfessionalInc AS cpInc ON cpInc.prof = kal.prof AND kal.start_date < cpInc.end_date AND cpInc.start_date < kal.end_date
WHERE kal.prof=3143 AND kal.start_date BETWEEN '2021-01-01 00:00' AND '2022-01-01 00:00'

With EXPLAIN I see that MariaDB is using an inefficient index (ix_professional).通过 EXPLAIN,我看到 MariaDB 使用的索引效率低下(ix_professional)。 I can't FORCE INDEX inside the view because we use the view in other queries with different execution plans, and an index inefficient here can be the best in other queries.我不能在视图中强制索引,因为我们在具有不同执行计划的其他查询中使用视图,并且这里的索引效率低下可能是其他查询中最好的。

Execution plan with MySQL (Fast): MySQL(快速)的执行计划: MySQL的执行计划(快速)

Execution plan with MariaDB (Slow): MariaDB的执行计划(慢): MariaDB 的执行计划(慢)

In general, many queries with MariaDB are slower than with MySQL通常,使用 MariaDB 的许多查询比使用 MySQL 的查询要慢

First, let's add these indexes;首先,让我们添加这些索引; they may speed up both versions:他们可能会加快这两个版本:

kal:  INDEX(prof, start_date,  end_date)
cpInc:  INDEX(prof, end_date, start_date)
cpInc:  INDEX(prof, start_date, end_date)

And remove the current index on just (prof)并删除仅(prof)上的当前索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM