mysql 性能缓慢

Slow mysql performance

几天前,我的 mysql 非常慢,一些 sql 查询不再工作,我不知道这个问题是从哪里来的。

OS:Centos

MySQL:5.7 - InnoDB

MySQL-V: mysql Ver 14.14 Distrib 5.7.28, for Linux (x86_64) using EditLine wrapper

这是我的查询


SELECT 
  BTCountry.id,
  BTCountry.name,
  BTCountry.code,
  BTCountry.last_data_from,
  BTCountry.year_last_data_from AS current_year,
  (SELECT 
    SUM(cantidad) AS subCount 
  FROM
    `krakente_marketpins`.`bt_countries` AS `subBTCountry` 
    INNER JOIN `krakente_marketpins`.`bus_trucks` AS `subBusTruck` 
      ON (
        `subBusTruck`.`pais_mercado_id` = `subBTCountry`.`id`
      ) 
  WHERE `subBusTruck`.`pais_mercado_id` = `BTCountry`.`id` 
    AND `subBusTruck`.`year_data_from` = 
      `subBTCountry`.`year_last_data_from`

    AND `subBusTruck`.month_data_from <= `subBTCountry`.`month_last_data_from`) AS ytd,
  (SELECT 
    SUM(cantidad) AS subCount 
  FROM
    `krakente_marketpins`.`bt_countries` AS `subBTCountry` 
    INNER JOIN `krakente_marketpins`.`bus_trucks` AS `subBusTruck` 
      ON (
        `subBusTruck`.`pais_mercado_id` = `subBTCountry`.`id`
      ) 
  WHERE `subBusTruck`.`pais_mercado_id` = `BTCountry`.`id` 
    AND `subBusTruck`.`year_data_from` = `subBTCountry`.`year_last_data_from` - 1 
    AND `subBusTruck`.`month_data_from` <= `subBTCountry`.`month_last_data_from`) AS last_year 
FROM
  bt_countries AS BTCountry 
  INNER JOIN `krakente_marketpins`.`bus_trucks` AS `BusTruck` 
    ON (
      `BusTruck`.`pais_mercado_id` = `BTCountry`.`id`
    ) 
WHERE `pais_mercado_id` IN (
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    '10',
    '11',
    '12'
  ) 
  AND mercado_id = ('1') 
GROUP BY `BTCountry`.`id` 
ORDER BY `ytd` DESC 

我知道这个查询可以优化,但我需要知道为什么我的 mysql 现在工作不好。

我使用 mysql调谐器来获得推荐,但没有成功。 这是我的调谐器日志

Variables to adjust:
    query_cache_size (=0)
    query_cache_type (=0)
    query_cache_limit (> 1M, or use smaller result sets)
    join_buffer_size (> 256.0K, or always use indexes with JOINs)
    table_open_cache (> 1024)
    table_definition_cache(1024) > 5155 or -1 (autosizing if supported)
    performance_schema = ON enable PFS
    innodb_log_file_size should be (=1G) if possible, so InnoDB total log files size equals to 25% of buffer pool size.

我之前说的,几天前查询还可以,但突然就不行了。

另一方面,我有多个 wp 站点,这些站点突然变得非常非常慢,我认为它也来自 mysql,因为如果我进入一个没有 sql 站点工作的站点正常。

我可以从哪里开始检查?

编辑

其解释查询

explain query

这几乎是伪代码,但是,我敢打赌您可以在只传递一次数据的同时进行聚合。由于您已经对数据进行了分组,因此您不需要在 select.

中再进行两次子查询
SELECT 
  BTCountry.id,
  BTCountry.name,
  BTCountry.code,
  BTCountry.last_data_from,
  BTCountry.year_last_data_from AS current_year,
  ytd =         SUM(CASE WHEN BusTruck.year_data_from = bt_countries.year_last_data_from - 1 AND bus_trucks.month_data_from <= bt_countries.month_last_data_from THEN cantidad ELSE NULL END),
  last_year =   SUM(CASE WHEN BusTruck.year_data_from = bt_countries.year_last_data_from AND bus_trucks.month_data_from <= bt_countries.month_last_data_from THEN cantidad ELSE NULL END)
FROM
  bt_countries AS BTCountry 
  INNER JOIN krakente_marketpins.bus_trucks AS BusTruck ON BusTruck.pais_mercado_id =BTCountry.id
WHERE
    pais_mercado_id IN (
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    '10',
    '11',
    '12'
  ) 
  AND mercado_id = ('1') 
GROUP 
    BYBTCountry.id,
    BTCountry.name,
    BTCountry.code,
    BTCountry.last_data_from,
    BTCountry.year_last_data_from
ORDER 
    BYytd 
DESC