MySQL 查询 : Long time spent in 'init' state

标签 mysql query-optimization

我正在对 MySQL 表(myisam 引擎)执行更新,根据分析器,该表在“初始”状态下花费了过多的时间:

mysql> show profile for query 2;
+----------------------+-----------+
| Status               | Duration  |
+----------------------+-----------+
| starting             |  0.000057 |
| checking permissions |  0.000006 |
| Opening tables       |  0.000020 |
| System lock          |  0.000007 |
| Table lock           |  0.000005 |
| init                 | 21.911657 |
| Updating             |  0.002363 |
| end                  |  0.000009 |
| query end            |  0.000004 |
| freeing items        |  0.000051 |
| logging slow query   |  0.000003 |
| logging slow query   |  0.000002 |
| cleaning up          |  0.000005 |
+----------------------+-----------+

查询如下:

mysql> update my_table
    -> set rank = 
    ->    greatest(
    ->       @rank := if(@score = score, @rank, @rank + 1),
    ->       least(0, @score := score)
    ->    )
    -> where game=7 and zone=11 and ladder=2
    -> order by score
    -> limit 100;

Query OK, 100 rows affected (21.92 sec)
Rows matched: 100  Changed: 100  Warnings: 0

我在“where”和“order by”子句中列出的所有列上都有一个复合索引(请参阅下面名为“zone_lad_score”的索引):

mysql> show indexes from my_table;
+--------------------+------------+-----------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| Table              | Non_unique | Key_name        | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------------------+------------+-----------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| my_table           |          1 | indx_e          |            1 | col_e        | A         |     2937401 |     NULL | NULL   |      | BTREE      |         |
| my_table           |          1 | zone_score      |            1 | zone         | A         |         217 |     NULL | NULL   |      | BTREE      |         |
| my_table           |          1 | zone_score      |            2 | score        | A         |    23499213 |     NULL | NULL   | YES  | BTREE      |         |
| my_table           |          1 | zone_d_score    |            1 | zone         | A         |         217 |     NULL | NULL   |      | BTREE      |         |
| my_table           |          1 | zone_d_score    |            2 | col_d        | A         |      123355 |     NULL | NULL   | YES  | BTREE      |         |
| my_table           |          1 | zone_d_score    |            3 | score        | A         |    46998427 |     NULL | NULL   | YES  | BTREE      |         |
| my_table           |          1 | zone_lad_score  |            1 | zone         | A         |         217 |     NULL | NULL   |      | BTREE      |         |
| my_table           |          1 | zone_lad_score  |            2 | ladder       | A         |         868 |     NULL | NULL   | YES  | BTREE      |         |
| my_table           |          1 | zone_lad_score  |            3 | score        | A         |    23499213 |     NULL | NULL   | YES  | BTREE      |         |
+--------------------+------------+-----------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+

我还在“游戏”上对表进行了分区,总共有 10 个分区。表中总共有大约 4700 万条记录。表定义如下:

my_table | CREATE TABLE `my_table` (
  `col_e` bigint(20) NOT NULL,
  `zone` bigint(20) NOT NULL,
  `score` int(11) DEFAULT NULL,
  `game` tinyint(4) DEFAULT NULL,
  `ladder` tinyint(4) DEFAULT NULL,
  `col_d` int(11) DEFAULT NULL,
  `rank` int(11) DEFAULT NULL,
  KEY `indx_e` (`col_e`),
  KEY `zone_score` (`zone`,`score`),
  KEY `zone_d_score` (`zone`,`col_d`,`score`),
  KEY `zone_lad_score` (`zone`,`ladder`,`score`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
/*!50100 PARTITION BY LIST (game)
(PARTITION p1 VALUES IN (1) ENGINE = MyISAM,
 PARTITION p2 VALUES IN (2) ENGINE = MyISAM,
 PARTITION p3 VALUES IN (3) ENGINE = MyISAM,
 PARTITION p4 VALUES IN (4) ENGINE = MyISAM,
 PARTITION p5 VALUES IN (5) ENGINE = MyISAM,
 PARTITION p6 VALUES IN (6) ENGINE = MyISAM,
 PARTITION p7 VALUES IN (7) ENGINE = MyISAM,
 PARTITION p8 VALUES IN (8) ENGINE = MyISAM,
 PARTITION p9 VALUES IN (9) ENGINE = MyISAM,
 PARTITION p10 VALUES IN (10) ENGINE = MyISAM) */

现在,根据 MySQL 文档 (http://dev.mysql.com/doc/refman/5.0/en/general-thread-states.html),“初始化”状态下的操作包括“刷新二进制日志、InnoDB 日志和一些查询缓存清理操作”。好吧……因为我没有使用 InnoDB,所以听起来不像是需要花费很多时间的事情。

我想我想知道为什么这个应该使用索引并且只影响 100 条记录的更新会花费这么长时间?是什么让它在“初始”状态停留了这么长时间?如果我对目标记录执行选择(select * from my_table where game=7 and zone=11 and ladder=2 order by score limit 100),它几乎立即返回。对该表执行类似的更新(使用 zone_d_score 索引)只需不到一秒钟的时间。什么可能会减慢此特定更新的速度?

编辑:添加表定义、相关表中所有索引的完整列表,以及重命名列以使事情更容易理解。

编辑 2:这是对最接近更新的查询的“解释”:

mysql> explain select * from my_table where game=7 and zone=11 and ladder=2 order by score limit 100;
+----+-------------+--------------------+------+------------------------------------------------+-----------------+---------+-------------+-------+-------------+
| id | select_type | table              | type | possible_keys                                  | key             | key_len | ref         | rows  | Extra       |
+----+-------------+--------------------+------+------------------------------------------------+-----------------+---------+-------------+-------+-------------+
|  1 | SIMPLE      | my_table           | ref  | zone_score,zone_d_score,zone_lad_score         | zone_lad_score  | 10      | const,const | 53952 | Using where |
+----+-------------+--------------------+------+------------------------------------------------+-----------------+---------+-------------+-------+-------------+
1 row in set (0.00 sec)

最佳答案

经过更多试验后,我在表上添加了一个索引,该索引还包括我对表进行分区的列:

CREATE INDEX game_zone_ladder_score ON my_table(game,zone,ladder,score)

突然之间,UPDATE 的表现好很多(亚秒级)。我原以为 UPDATE 会像 SELECT 一样利用分区,但显然不是。

仍然很想知道 MySQL 在更新期间的“初始化”状态下到底在做什么,和/或为什么更新不支持分区。

关于MySQL 查询 : Long time spent in 'init' state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3491524/

相关文章:

php - 在表格中的每一行添加一个 php 按钮

php - 从sql获取记录计数并使用php将其传递到xml

mysql - 添加带有 LIMIT 的 ORDER BY 时,简单的 MySQL 查询被阻塞

sql - 将SQL重写为JOINS而不是子查询

mysql:查询返回根据特定用户的分数排名

mysql - 外键困境

php - 根据用户输入按行回显表

mysql - 比较和更新百万行的最快方法

sql - 有没有办法在 MySQL 中选择最大行 id,而无需使用 MAX() 扫描所有行?

sql - 如何使用多个 LIKE 运算符并使用索引