Magento 在 mariadb 中导致死锁

Magento causes deadlock in mariadb

我的 Magento(Vesion 1.8.0.0) 网站 运行 在我使用 MySQL5.5 数据库多年时没有任何问题。最近移动到 mariadb-server-10.2 后(使用 sqldump 导出数据,然后在新的 mariadb 服务器上导入),多次 PHP 子进程处于等待状态,直到 max_execution_time 到期,最后 php -fpm 停止响应。

经过调查,我发现所有 php 子进程都在等待数据库查询。最近遇到问题的时候查看了mariadb上的processlist,输出如下。知道我该如何解决这个问题吗?提前致谢。

    MariaDB [(none)]> show processlist;
+--------+-------------+-------------------------------+---------+---------+------+------------------------------+----------------------------------------------------------------------                            --------------------------------+----------+
| Id     | User        | Host                          | db      | Command | Time | State                        | Info                                                                                                                             | Progress |
+--------+-------------+-------------------------------+---------+---------+------+------------------------------+----------------------------------------------------------------------                            --------------------------------+----------+
|      1 | system user |                               | NULL    | Daemon  | NULL |                              | NULL                                                                                                                             |    0.000 |
|      2 | system user |                               | NULL    | Daemon  | NULL |                              | NULL                                                                                                                             |    0.000 |
|      4 | system user |                               | NULL    | Daemon  | NULL |                              | NULL                                                                                                                             |    0.000 |
|      3 | system user |                               | NULL    | Daemon  | NULL |                              | NULL                                                                                                                             |    0.000 |
|      5 | system user |                               | NULL    | Daemon  | NULL | InnoDB shutdown handler      | NULL                                                                                                                             |    0.000 |
| 109822 | catalog     | mymagentositexxxxxx.com:56405 | catalog | Query   |  985 | update                       | INSERT INTO `cataloginventory_stock_status` (`product_id`,`website_id                            `,`stock_id`,`qty`,`stock_statu |    0.000 |
| 109850 | catalog     | mymagentositexxxxxx.com:56436 | catalog | Query   |  985 | update                       | INSERT INTO `cataloginventory_stock_status` (`product_id`,`website_id                            `,`stock_id`,`qty`,`stock_statu |    0.000 |
| 109859 | catalog     | mymagentositexxxxxx.com:56448 | catalog | Query   |  985 | update                       | INSERT INTO `cataloginventory_stock_status` (`product_id`,`website_id                            `,`stock_id`,`qty`,`stock_statu |    0.000 |
| 109931 | catalog     | mymagentositexxxxxx.com:56554 | catalog | Query   |  986 | Waiting for table level lock | DELETE FROM `catalogsearch_fulltext` WHERE (store_id=1) AND (product_                            id IN ('26515'))                |    0.000 |
| 109966 | catalog     | mymagentositexxxxxx.com:56606 | catalog | Query   |  984 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 33218 AS `query_id`, `s`.`p                            roduct_id`, 0 AS `relevance` FR |    0.000 |
| 109993 | catalog     | mymagentositexxxxxx.com:56645 | catalog | Query   |  934 | update                       | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat                            ed_at`, `product_id`, `store_id |    0.000 |
| 109997 | catalog     | mymagentositexxxxxx.com:56650 | catalog | Query   |  987 | Sending data                 | INSERT INTO `catalogsearch_result` SELECT 33215 AS `query_id`, `s`.`p                            roduct_id`, 0 AS `relevance` FR |    0.000 |
| 110040 | catalog     | mymagentositexxxxxx.com:56709 | catalog | Query   |  983 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 33215 AS `query_id`, `s`.`p                            roduct_id`, 0 AS `relevance` FR |    0.000 |
| 110218 | catalog     | mymagentositexxxxxx.com:56925 | catalog | Query   |  940 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 6656 AS `query_id`, `s`.`pr                            oduct_id`, 0 AS `relevance` FRO |    0.000 |
| 110248 | catalog     | mymagentositexxxxxx.com:56956 | catalog | Query   |  932 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 33220 AS `query_id`, `s`.`p                            roduct_id`, 0 AS `relevance` FR |    0.000 |
| 110380 | catalog     | mymagentositexxxxxx.com:57095 | catalog | Query   |  906 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 33214 AS `query_id`, `s`.`p                            roduct_id`, 0 AS `relevance` FR |    0.000 |
| 110392 | catalog     | mymagentositexxxxxx.com:57107 | catalog | Query   |  905 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 33215 AS `query_id`, `s`.`p                            roduct_id`, 0 AS `relevance` FR |    0.000 |
| 110810 | catalog     | mymagentositexxxxxx.com:57554 | catalog | Query   |  825 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 6656 AS `query_id`, `s`.`pr                            oduct_id`, 0 AS `relevance` FRO |    0.000 |
| 110865 | catalog     | mymagentositexxxxxx.com:57616 | catalog | Query   |  808 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 21581 AS `query_id`, `s`.`p                            roduct_id`, 0 AS `relevance` FR |    0.000 |
| 110968 | catalog     | mymagentositexxxxxx.com:57782 | catalog | Query   |   48 | update                       | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat                            ed_at`, `product_id`, `store_id |    0.000 |
| 110969 | catalog     | mymagentositexxxxxx.com:57783 | catalog | Query   |   48 | update                       | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat                            ed_at`, `product_id`, `store_id |    0.000 |
| 110989 | catalog     | mymagentositexxxxxx.com:57803 | catalog | Query   |   39 | update                       | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat                            ed_at`, `product_id`, `store_id |    0.000 |
| 110999 | catalog     | mymagentositexxxxxx.com:57813 | catalog | Query   |   34 | update                       | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat                            ed_at`, `product_id`, `store_id |    0.000 |
| 111012 | catalog     | mymagentositexxxxxx.com:57867 | catalog | Query   |   32 | update                       | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat                            ed_at`, `product_id`, `store_id |    0.000 |
| 111067 | catalog     | mymagentositexxxxxx.com:57959 | catalog | Query   |   18 | update                       | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat                            ed_at`, `product_id`, `store_id |    0.000 |
| 111077 | catalog     | mymagentositexxxxxx.com:57974 | catalog | Query   |   15 | update                       | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat                            ed_at`, `product_id`, `store_id |    0.000 |
| 111078 | root        | localhost                     | NULL    | Query   |    0 | init                         | show processlist                                                                                                                 |    0.000 |
| 111117 | catalog     | mymagentositexxxxxx.com:58015 | catalog | Query   |   10 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 13226 AS `query_id`, `s`.`p                            roduct_id`, 0 AS `relevance` FR |    0.000 |
| 111147 | moodle      | mymagentositexxxxxx.com:58046 | moodle  | Sleep   |    0 |                              | NULL                                                                                                                             |    0.000 |
+--------+-------------+-------------------------------+---------+---------+------+------------------------------+----------------------------------------------------------------------                            --------------------------------+----------+
29 rows in set (0.00 sec)

#update2 'Show create table output' 的 table 处于等待状态如下

MariaDB [catalog]> SHOW CREATE TABLE catalogsearch_fulltext;
+------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table                  | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
+------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| catalogsearch_fulltext | CREATE TABLE `catalogsearch_fulltext` (
  `product_id` int(10) unsigned NOT NULL COMMENT 'Product ID',
  `store_id` smallint(5) unsigned NOT NULL COMMENT 'Store ID',
  `data_index` longtext DEFAULT NULL COMMENT 'Data index',
  `fulltext_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entity ID',
  PRIMARY KEY (`fulltext_id`),
  UNIQUE KEY `UNQ_CATALOGSEARCH_FULLTEXT_PRODUCT_ID_STORE_ID` (`product_id`,`store_id`),
  FULLTEXT KEY `FTI_CATALOGSEARCH_FULLTEXT_DATA_INDEX` (`data_index`)
) ENGINE=MyISAM AUTO_INCREMENT=912741 DEFAULT CHARSET=utf8 COMMENT='Catalog search result table' |
+------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [catalog]> SHOW CREATE TABLE catalogsearch_result;
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table                | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| catalogsearch_result | CREATE TABLE `catalogsearch_result` (
  `query_id` int(10) unsigned NOT NULL COMMENT 'Query ID',
  `product_id` int(10) unsigned NOT NULL COMMENT 'Product ID',
  `relevance` decimal(20,4) NOT NULL DEFAULT 0.0000 COMMENT 'Relevance',
  PRIMARY KEY (`query_id`,`product_id`),
  KEY `IDX_CATALOGSEARCH_RESULT_QUERY_ID` (`query_id`),
  KEY `IDX_CATALOGSEARCH_RESULT_PRODUCT_ID` (`product_id`),
  CONSTRAINT `FK_CATALOGSEARCH_RESULT_QUERY_ID_CATALOGSEARCH_QUERY_QUERY_ID` FOREIGN KEY (`query_id`) REFERENCES `catalogsearch_query` (`query_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `FK_CATSRCH_RESULT_PRD_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Catalog search result table' |
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


用时最多的进程 (#109997) 正在接触 MyISAM table?完成 SHOW FULL PROCESSLIST 后,我看到

INSERT
     INTO  `catalogsearch_result` 
SELECT  11474 AS `query_id`, `s`.`product_id`, 0 AS `relevance`
    FROM  `catalogsearch_fulltext` AS `s`
    INNER JOIN  `catalog_product_entity` AS `e`  ON e.entity_id = s.product_id
    WHERE  (s.store_id = 1)
      AND  ((`s`.`data_index` LIKE '%Informaatika%'
         OR  `s`.`data_index` LIKE '%8.c%')
           )  ON DUPLICATE KEY 
UPDATE  `relevance` = VALUES(`relevance`)

那是最近才开始的缓慢 SELECT。由于我们正在谈论 MyISAM,并且正在进行写入,因此查询将一次发生一个(大部分)。

因为 ORLIKE 测试中的 leading 通配符,它​​很慢。您有什么方法可以避免 both 这些问题吗?有了它们,SELECT 必须扫描整个 table,这显然需要几分钟。 (table有多大?)

更改超时可能会让您超过一个实例,但只会让您处于一个永无止境的查询序列中,等待其他查询完成。

最好的解决方案是远离 MyISAM。更多提示:http://mysql.rjweb.org/doc.php/myisam2innodb

(即使转换后,也可能存在其他问题。)