在我们的服务出现一些预期的增长之后,突然间一些更新花费了非常长的时间,这些过去非常快,直到表达到大约 2MM 记录,现在它们每个需要大约 40-60 秒。
update table1 set field1=field1+1 where id=2229230;
Query OK, 0 rows affected (42.31 sec)
Rows matched: 1 Changed: 0 Warnings: 0
字段类型如下:
`id` bigint(20) NOT NULL auto_increment,
`field1` int(11) default '0',
分析结果,对于上下文切换,这是唯一一个似乎在结果上有很高数字的:
mysql> show profile context switches
-> ;
+----------------------+-----------+-------------------+---------------------+
| Status | Duration | Context_voluntary | Context_involuntary |
+----------------------+-----------+-------------------+---------------------+
| (initialization) | 0.000007 | 0 | 0 |
| checking permissions | 0.000009 | 0 | 0 |
| Opening tables | 0.000045 | 0 | 0 |
| System lock | 0.000009 | 0 | 0 |
| Table lock | 0.000008 | 0 | 0 |
| init | 0.000056 | 0 | 0 |
| Updating | 46.063662 | 75487 | 14658 |
| end | 2.803943 | 5851 | 857 |
| query end | 0.000054 | 0 | 0 |
| freeing items | 0.000011 | 0 | 0 |
| closing tables | 0.000008 | 0 | 0 |
| logging slow query | 0.000265 | 2 | 1 |
+----------------------+-----------+-------------------+---------------------+
12 rows in set (0.00 sec)
该表大约有250万条记录,id为主键,在另一个字段上有一个唯一索引(不包括在更新中)。
这是一个 innodb 表。
关于可能是什么原因的任何指示?
是否有任何特定变量可以帮助追踪问题?
是否有更新的“解释”?
编辑:我还注意到该表还有一个:
`modDate` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
解释:
+----+-------------+---------------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | table1 | const | PRIMARY | PRIMARY | 8 | const | 1 | |
+----+-------------+---------------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.02 sec)
如果 id 真的是主键(除非你有很多很多等于 2229230 的 ids?),查询不可能花费很长时间。请运行以下两个sql并发布结果:
show create table table1;
explain select * from table1 where id = 2229230;
更新:为了完整起见,还做一个
select count(*) from table1 where id = 2229230;
Tôi là một lập trình viên xuất sắc, rất giỏi!