odo*_*llt 5 mysql query-optimization
我正在对MySQL表(myisam引擎)执行更新,根据分析器,它在'init'状态下花费了过多的时间:
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 |
+----------------------+-----------+
Run Code Online (Sandbox Code Playgroud)
查询如下:
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
Run Code Online (Sandbox Code Playgroud)
我在'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 | |
+--------------------+------------+-----------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
Run Code Online (Sandbox Code Playgroud)
我还将表格划分为"游戏",共有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) */
Run Code Online (Sandbox Code Playgroud)
现在,根据MySQL文档(http://dev.mysql.com/doc/refman/5.0/en/general-thread-states.html),'init '状态下的操作包括"刷新二进制日志, InnoDB日志,以及一些查询缓存清理操作." 好的...所以既然我没有使用InnoDB,听起来不像任何需要花费很多时间的东西.
我想我想知道为什么这个应该使用索引的更新,只影响100条记录需要这么长时间?特别是在'init'状态持续这么长时间?如果我对目标记录执行选择(从my_table中选择*,其中game = 7且zone = 11,ladder = 2则按分数限制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)
Run Code Online (Sandbox Code Playgroud)
经过更多实验后,我在表上添加了一个索引,其中还包括我对表进行分区的列:
CREATE INDEX game_zone_ladder_score ON my_table(game,zone,ladder,score)
Run Code Online (Sandbox Code Playgroud)
突然之间,更新的性能变得更好(亚秒)。我本来希望 UPDATE 能够像 SELECT 一样利用分区,但显然不是。
仍然想知道 MySQL 在 UPDATE 期间的“init”状态期间到底在做什么,和/或为什么 UPDATE 不支持分区。
| 归档时间: |
|
| 查看次数: |
3369 次 |
| 最近记录: |