0

最近我们将一个数据库从 MariaDB 10.2 切换到 Percona Server (Mysql 5.7),我们有一个查询大约需要 15 秒(之前大约是 0.5),因为查询优化器没有使用主表上的任何索引。因为app逻辑,我们不能改变查询格式,需要让DB使用索引。

查询结构很简单:

EXPLAIN SELECT `clients`.`id` AS `t0_c0`,
       `client`.`name1` AS `t0_c1`,
       `client`.`name2` AS `t0_c2`,
       `users`.`id` AS `t1_c0`,
       `users`.`suffix` AS `t1_c1`,
       `users`.`position` AS `t1_c2`,
       `users`.`first_name` AS `t1_c3`,
       `users`.`last_name` AS `t1_c4`,
       `privateData`.`id` AS `t2_c0`,
       `privateData`.`first_name` AS `t2_c1`,
       `privateData`.`last_name` AS `t2_c2`,
       `tariff`.`id` AS `t3_c0`,
       `tariff`.`provider_id` AS `t3_c1`,
       `tariff`.`tariff_type` AS `t3_c2`,
       `tariff`.`name` AS `t3_c3`,
       `providers`.`id` AS `t4_c0`,
       `providers`.`name1` AS `t4_c1`,
       `providers`.`name2` AS `t4_c2`,
       `addresses`.`id` AS `t5_c0`,
       `addresses`.`zipcode` AS `t5_c1`,
       `addresses`.`country` AS `t5_c2`,
       `addresses`.`city` AS `t5_c3`,
       `private`.`id` AS `t6_c0`,
       `private`.`first_name` AS `t6_c1`,
       `private`.`last_name` AS `t6_c2`,
       `commercial`.`id` AS `t7_c0`,
       `commercial`.`name1` AS `t7_c1`,
       `commercial`.`name2` AS `t7_c2`,
       `commercial`.`name_on_invoice` AS `t7_c3`,
       `commercial`.`organization_type` AS `t7_c4`,
       `organizations`.`id` AS `t8_c0`,
       `organizations`.`person_id` AS `t8_c1`,
       `organizations`.`address_id` AS `t8_c2`,
       `organizations`.`status` AS `t8_c3`,
       `shaddresses`.`id` AS `t9_c0`,
       `shaddresses`.`zipcode` AS `t9_c1`,
       `shaddresses`.`country` AS `t9_c2`,
       `shaddresses`.`city` AS `t9_c3`,
       `shprivate`.`id` AS `t10_c0`,
       `shprivate`.`first_name` AS `t10_c1`,
       `shprivate`.`last_name` AS `t10_c2`,
       `coraddresses`.`id` AS `t11_c0`,
       `coraddresses`.`zipcode` AS `t11_c1`,
       `coraddresses`.`country` AS `t11_c2`,
       `corprivate`.`id` AS `t12_c0`,
       `corprivate`.`first_name` AS `t12_c1`,
       `corprivate`.`last_name` AS `t12_c2`,
FROM `client` `client`
LEFT OUTER JOIN `users` `users` ON (`client`.`user_id`=`users`.`id`)
AND (users.status!=5)
LEFT OUTER JOIN `private` `privateData` ON (`users`.`person_id`=`privateData`.`id`)
LEFT OUTER JOIN `tariff` `tariff` ON (`client`.`rate_id`=`tariff`.`id`)
LEFT OUTER JOIN `providers` `providers` ON (`client`.`provider_id`=`providers`.`id`)
LEFT OUTER JOIN `addresses` `addresses` ON (`client`.`main_address_id`=`addresses`.`id`)
LEFT OUTER JOIN `private` `private` ON (`client`.`main_person_id`=`private`.`id`)
LEFT OUTER JOIN `commercial` `commercial` ON (`client`.`main_organization_id`=`commercial`.`id`)
LEFT OUTER JOIN `organizations` `organizations` ON (`client`.`id_organization`=`organizations`.`id`)
AND (organizations.status!=5)
LEFT OUTER JOIN `addresses` `shaddresses` ON (`client`.`shipping_address_id`=`shaddresses`.`id`)
LEFT OUTER JOIN `private` `shprivate` ON (`client`.`shipping_person_id`=`shprivate`.`id`)
LEFT OUTER JOIN `addresses` `coraddresses` ON (`client`.`correspondense_address_id`=`coraddresses`.`id`)
LEFT OUTER JOIN `private` `corprivate` ON (`client`.`correspondense_person_id`=`corprivate`.`id`)
WHERE (client.status!=5)
ORDER BY client.id DESC
LIMIT 10
OFFSET 10

我可以更改任何索引,但是,我不能更改查询。在旧主机上,它在 0.2 秒内运行,但是,它使用客户端表中的索引的优化器。使用 Percona Server (mysql 5.7) 需要 15 秒。优化器没有使用客户表中的任何索引。使用来自客户表的 FORCE INCEX() 不到 1 秒(复合索引在大约 0.2 秒内进行)。表“提供者”只有 1 行。我已经在“客户”表上设置了索引,但是,在解释中它们没有显示为可能的键。

我试图将 MySql 变量 'max_seeks_for_key' 设置为 1,但是,它仍然没有使用索引。

我认为我缺少一些基本的东西,但我不知道是什么。

这个查询的解释是:

在此处输入图像描述

ORDER BY 正在生成 TEMPORARY TABLE 并且正在使用所有资源(即使没有 INDEX,也没有 order by 在不到一秒的时间内运行)。

任何想法都值得赞赏。

4

4 回答 4

1

这应该比使用以下方法更有效FORCE

SELECT  ...
    FROM ( SELECT  id
              FROM  client
              WHERE  status != 5
              ORDER BY  id DESC
              LIMIT  10 OFFSET 10 
          ) AS ids
    JOIN  client USING(id)
    LEFT OUTER JOIN  `users` `users`  ON (`client`.`user_id`=`users`.`id`)
      AND  (users.status!=5)
    LEFT OUTER JOIN  `private` `privateData`  ON (`users`.`person_id`=`privateData`.`id`)
    LEFT OUTER JOIN  `tariff` `tariff`  ON (`client`.`rate_id`=`tariff`.`id`)
    LEFT OUTER JOIN  `providers` `providers`  ON (`client`.`provider_id`=`providers`.`id`)
    LEFT OUTER JOIN  `addresses` `addresses`  ON (`client`.`main_address_id`=`addresses`.`id`)
    LEFT OUTER JOIN  `private` `private`  ON (`client`.`main_person_id`=`private`.`id`)
    LEFT OUTER JOIN  `commercial` `commercial`  ON (`client`.`main_organization_id`=`commercial`.`id`)
    LEFT OUTER JOIN  `organizations` `organizations`  ON (`client`.`id_organization`=`organizations`.`id`)
      AND  (organizations.status!=5)
    LEFT OUTER JOIN  `addresses` `shaddresses`  ON (`client`.`shipping_address_id`=`shaddresses`.`id`)
    LEFT OUTER JOIN  `private` `shprivate`  ON (`client`.`shipping_person_id`=`shprivate`.`id`)
    LEFT OUTER JOIN  `addresses` `coraddresses`  ON (`client`.`correspondense_address_id`=`coraddresses`.`id`)
    LEFT OUTER JOIN  `private` `corprivate`  ON (`client`.`correspondense_person_id`=`corprivate`.`id`)
    ORDER BY  client.id DESC -- Yes, repeated here 
于 2018-05-20T18:48:10.020 回答
0

对 ORDER BY ... LIMIT 查询使用索引是否有益取决于表上条件的选择性。如果大多数行不满足条件,则使用表扫描可能比扫描大部分索引更快。

因此,如果对选择性的估计不正确,则查询优化器可能会选择非最佳计划。对于此查询,如果状态列上没有索引,则估计将不准确,因为优化器不会对此列的分布进行任何统计。要获得更准确的估计,您可以尝试在此列上创建索引。另一个尝试的选择是关闭 MySQL 5.7 中引入的过滤估计的使用。为此,请执行:

SET optimizer_switch='condition_fanout_filter=off'

但是,即使对条件选择性的估计是正确的,如果列是相关的,优化器也可能无法选择最佳计划。在您的情况下,我怀疑状态和身份之间可能存在高度相关性。总体上可能只有很少的行 status!=5,但在高 ID 的行中却很常见。如果是这种情况,那么我担心查询优化器将无法检测到这一点,而获得最佳计划的唯一方法是使用索引提示。

如果您可以为此查询提供优化器跟踪,我将能够了解更多正在发生的事情。有关如何获取优化器跟踪的建议,请参阅 https://oysteing.blogspot.no/2016/01/how-to-get-optimizer-trace-for-query.html 。

于 2018-05-28T07:39:31.180 回答
0

我没有找到任何解决方案,我只是使用了... FORCE INDEX(状态)。

于 2018-05-14T12:46:02.070 回答
0

MySQL 应该能够使用 ORDER BY 子句上的索引来快速跟踪client表中所需的记录。从那里,将几行连接到其他表以获取更多信息应该很快。

尝试添加此索引:

ALTER TABLE `client` ADD INDEX `client_idx_id` (`id`);

如果 MySQL 选择不使用这个索引(有时它会做出错误的决定),尝试强制它并比较这个选项的执行持续时间。

于 2018-05-20T19:53:01.793 回答