MySQL/MariaDB InnoDB 索引停止工作

标签 mysql innodb indexing mariadb xtradb

我的 MySQL/MariaDB InnoDB/XtraDB 出现奇怪的行为。最近切换到 MariaDB 5.5。切换使服务器整体性能更高,但我仍然遇到这个问题。

一个特定的表索引似乎时不时地中断。一段时间后,它会自行修复。

SHOW CREATE TABLE article_inventory; 给出

CREATE TABLE `article_inventory` (
    `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    `article_variant_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
    `season_id` BIGINT(20) UNSIGNED NOT NULL,
    `warehouse_id` BIGINT(20) UNSIGNED NOT NULL,
    `quantity` BIGINT(20) NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`),
    UNIQUE INDEX `unique_inventory_idx` (`article_variant_id`, `season_id`, `warehouse_id`),
    INDEX `article_variant_id_idx` (`article_variant_id`),
    INDEX `article_inventory_season_id_idx` (`season_id`),
    INDEX `article_inventory_warehouse_id_idx` (`warehouse_id`),
    CONSTRAINT `article_inventory_article_variant_id_article_variant_id` FOREIGN KEY (`article_variant_id`) REFERENCES `article_variant` (`id`),
    CONSTRAINT `article_inventory_season_id_season_id` FOREIGN KEY (`season_id`) REFERENCES `season` (`id`),
    CONSTRAINT `article_inventory_warehouse_id_warehouse_id` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouse` (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3827622858;

编辑: 大多数 SELECT 查询都是针对此表进行的。每小时进行一次大更新。有时会进行非常大的更新。

运行此查询:

SELECT a.id
FROM article a
    INNER JOIN article_variant a2 
        ON a.style_id = a2.style_id
    INNER JOIN article_block a3 
        ON a2.po = a3.po
    INNER JOIN color c 
        ON a2.color_id = c.id
    INNER JOIN size s 
        ON a2.size_id = s.id
    INNER JOIN article_group a4 
        ON a2.id = a4.article_variant_id AND (a4.season_id = 6)
    INNER JOIN article_inventory a5 
        ON a2.id = a5.article_variant_id AND (((a5.warehouse_id = 5 OR a5.warehouse_id = 1) AND a5.season_id = 6))
    INNER JOIN article_date a6 
        ON a.style_id = a6.style_id AND ((a6.pricelist_id = 5 AND a6.season_id = 6))
    INNER JOIN article_price a7 
        ON a.style_id = a7.style_id AND ((a7.pricelist_id = 5 AND a7.season_id = 6))
    INNER JOIN pricelist p 
        ON a7.pricelist_id = p.id
    INNER JOIN concept c2 
        ON a4.concept_id = c2.id
    INNER JOIN category c3 
        ON a4.category_id = c3.id
    LEFT JOIN order_cart_row o 
        ON a2.id = o.article_variant_id AND (o.order_id = 17035)
    LEFT JOIN shortlist s2 
        ON a.id = s2.article_id AND (s2.order_id = 17035)
WHERE ((a2.is_canceled <> 1 AND a4.is_canceled <> 1) OR o.quantity IS NOT NULL) AND c2.id = 2
GROUP BY a.id

...应该在大约 0.5-1.0 秒内执行,并给我一个类似这样的解释:

id select_type table  type     possible_keys                                                                                               key                                 key_len    ref                                  rowsExtra
1  SIMPLE      p      const    PRIMARY                                                                                                     PRIMARY                             8          const                                1   Using index; Using temporary; Using filesort
1  SIMPLE      c2     const    PRIMARY                                                                                                     PRIMARY                             8          const                                1   Using index                            
1  SIMPLE      a3     index    PRIMARY                                                                                                     PRIMARY                             98         NULL                                 1031Using where                            
1  SIMPLE      a2     ref      PRIMARY,unique_variant_idx,color_id_idx,style_id_idx,size_id_idx,article_variant_po_idx                     article_variant_po_idx              98         wsp_stage.a3.po                      14  Using where                            
1  SIMPLE      s      eq_ref   PRIMARY                                                                                                     PRIMARY                             11         wsp_stage.a2.size_id                 1   Using index                            
1  SIMPLE      c      eq_ref   PRIMARY                                                                                                     PRIMARY                             11         wsp_stage.a2.color_id                1                                          
1  SIMPLE      o      eq_ref   unique_rows_idx,article_variant_id_idx,order_id_idx                                                         unique_rows_idx                     16         const,wsp_stage.a2.id                1   Using index                            
1  SIMPLE      a      eq_ref   unique_style_idx                                                                                            unique_style_idx                    767        wsp_stage.a2.style_id                1   Using index                            
1  SIMPLE      a6     ref      article_season_pricelist_unique_idx,season_id_idx,pricelist_id_idx,style_id_idx                             article_season_pricelist_unique_idx 784        wsp_stage.a2.style_id,const,const    1   Using index                            
1  SIMPLE      a7     ref      article_season_pricelist_unique_idx,season_id_idx,pricelist_id_idx,style_id_idx                             article_season_pricelist_unique_idx 784        wsp_stage.a2.style_id,const,const    1   Using index                            
1  SIMPLE      a4     eq_ref   unique_group_idx,one_per_season_idx,category_id_idx,concept_id_idx,season_id_idx,article_variant_id_idx     one_per_season_idx                  16         wsp_stage.a2.id,const                1   Using index                            
1  SIMPLE      c3     eq_ref   PRIMARY                                                                                                     PRIMARY                             8          wsp_stage.a4.category_id             1   Using index                            
1  SIMPLE      s2     ref      shortlist_article_id_idx                                                                                    shortlist_article_id_idx            8          wsp_stage.a.id                       10  Using where                            
1  SIMPLE      a5     ref      unique_inventory_idx,article_variant_id_idx,article_inventory_season_id_idx,article_inventory_warehouse_id_iunique_inventory_idx                17         wsp_stage.a2.id,const                8   Using where 

当一切正常时,article_inventory(别名 a5)使用 unique_inventory_idxarticle_variant_id_idx。两者都应该给我大约 5-100 个检查的行。

但时不时会发生一些事情,同样的查询需要大约 30 秒,并给我这个解释:

id select_type table  type     possible_keys                                                                                               key                                 key_len  ref                                  rows    Extra
1  SIMPLE      p      const    PRIMARY                                                                                                     PRIMARY                             8        const                                1       Using index; Using temporary; Using filesort
1  SIMPLE      c2     const    PRIMARY                                                                                                     PRIMARY                             8        const                                1       Using index
1  SIMPLE      a5     ref      unique_inventory_idx,article_variant_id_idx,article_inventory_season_id_idx,article_inventory_warehouse_id_iarticle_inventory_season_id_idx     8        const                                6718732 Using where
1  SIMPLE      a4     eq_ref   unique_group_idx,one_per_season_idx,category_id_idx,concept_id_idx,season_id_idx,article_variant_id_idx     one_per_season_idx                  16       wsp_stage.a5.article_variant_id,const1       Using where
1  SIMPLE      c3     eq_ref   PRIMARY                                                                                                     PRIMARY                             8        wsp_stage.a4.category_id             1       Using index
1  SIMPLE      a2     eq_ref   PRIMARY,unique_variant_idx,color_id_idx,style_id_idx,size_id_idx,article_variant_po_idx                     PRIMARY                             8        wsp_stage.a5.article_variant_id      1       
1  SIMPLE      c      eq_ref   PRIMARY                                                                                                     PRIMARY                             11       wsp_stage.a2.color_id                1       Using index
1  SIMPLE      a      eq_ref   unique_style_idx                                                                                            unique_style_idx                    767      wsp_stage.a2.style_id                1       Using index
1  SIMPLE      a6     ref      article_season_pricelist_unique_idx,season_id_idx,pricelist_id_idx,style_id_idx                             article_season_pricelist_unique_idx 784      wsp_stage.a2.style_id,const,const    1       Using index
1  SIMPLE      a7     ref      article_season_pricelist_unique_idx,season_id_idx,pricelist_id_idx,style_id_idx                             article_season_pricelist_unique_idx 784      wsp_stage.a2.style_id,const,const    1       Using index
1  SIMPLE      s      eq_ref   PRIMARY                                                                                                     PRIMARY                             11       wsp_stage.a2.size_id                 1       Using index
1  SIMPLE      a3     eq_ref   PRIMARY                                                                                                     PRIMARY                             98       wsp_stage.a2.po                      1       Using index
1  SIMPLE      o      eq_ref   unique_rows_idx,article_variant_id_idx,order_id_idx                                                         unique_rows_idx                     16       const,wsp_stage.a5.article_variant_id1       Using where
1  SIMPLE      s2     ref      shortlist_article_id_idx                                                                                    shortlist_article_id_idx            8        wsp_stage.a.id                       7       Using where

article_inventory (a5) 现在使用 article_inventory_season_id_idx。一个非常非常糟糕的索引,因为它是所有索引中第二个最不具体的。给我 6718732 个已检查的行。

我的.ini:

[mysqld]
datadir="W:/mariadb/data/"
port=3306
sql_mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
default_storage_engine=innodb
innodb_buffer_pool_size=5000M
innodb_log_file_size=52428800
innodb_file_per_table
innodb_file_format=Barracuda

[client]
port=3307

最佳答案

好吧,首先索引没有被破坏。这可能与表上的 MySQL 统计信息有关,建议查询优化器使用错误的索引。现在,在我们转向可能的解决方案之前,让我们先了解导致它的原因。

当 MySQL 运行查询时,它会查看该表的统计信息以确定哪些索引适合该查询,然后根据建议选择正确的索引。表统计信息包含索引基数和与使用索引相关的成本等信息。每次运行查询以确定最佳执行路径时,MySQL 都会查看这些统计信息。

现在,由于索引是存储在磁盘上的实际数据结构,当您更新、插入和删除这些索引时,它们的统计信息会发生变化。这可能是问题的根本原因。 InnoDB 通过对索引结构进行 8 次随机深入研究来即时更新统计信息。 MyISAM 的做法不同。有关此问题的更多信息,请参阅此链接:https://dba.stackexchange.com/questions/3398/from-where-does-the-mysql-query-optimizer-read-index-statistics

您提到您偶尔会在重复更新时进行大量插入。我怀疑在插入期间或插入发生之后,有一小段时间表的 innodb 统计信息已过时或正在编译。这可能就是您看到从一个指数到感染指数的零星变化的原因。此时您的统计信息不正确,查询优化器做出了错误的选择。

谷歌以下内容:

mysql statistics update

有一整套链接提供了更多详细信息,这是一本很好的读物。

我以前在数据库上看到过这种情况,这不是 BUG,只是需要注意的事情。

可能的解决方案:

  1. 在使用重复更新语句进行批量插入后,在相关表上显式调用 ANALYZE TABLE。在更新后直接运行此命令可能会使您的统计信息变为正确的形式,因此它会建议正确的索引。不利的一面是您的系统实际上可能会重新编译统计信息两次,这是一种资源浪费。请记住,我不确定这个问题是在插入语句之后还是期间发生的。
  2. 强制在您的选择语句中使用正确的索引。您可以强制 MySQL 始终使用正确的索引。然而,这是一个坏主意。在某些时候,另一个索引可能会变得更适合您的查询,并且由于您现在正在有效地硬编码索引以在查询中使用,这将在以后成为一个问题。
  3. 保持原样,这可能听起来很奇怪,但是运行 30 秒的查询是一场灾难吗?这可能取决于您的要求,但如果 30 秒可以让查询运行,为什么要尝试修复它?请记住,如果它没坏就不要修它的理念。

如果您需要更多说明,我希望这是有道理的发表评论......

关于MySQL/MariaDB InnoDB 索引停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14810004/

相关文章:

MySQL 按某些值排序,即使表中不存在

php - 使用经纬度和 MySQL 查找最近的 10 个城市?

php - 将每个文本文件的内容保存到数据库表 mysql 中

mysql - InnoDB 二级索引包括值而不是指向 PK 的指针,这怎么够了?

python - pandas 从日期时间索引中删除秒

mysql - 当order by col有索引时,为什么mysql需要filesort?

mysql - 比较 "for"循环内的数据

MySQL 崩溃 -> innodb 恢复不起作用 -> 尝试另一种方法

mysql - 从具有 30M 行的 Innodb 表中删除 5K 行的最佳方法

python - 使用 MultiIndex 时,在 Pandas 中按年份进行索引/选择