MySQL更新(太长时间)了

web*_*ber 8 mysql sql optimization

经过我们服务的一些预期的增长后,所有突然的一些更新都花了很长时间,这些过去非常快,直到表达到大约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
Run Code Online (Sandbox Code Playgroud)

以下是字段类型:

`id` bigint(20) NOT NULL auto_increment,
`field1` int(11) default '0',
Run Code Online (Sandbox Code Playgroud)

对于上下文切换的分析结果,这是唯一一个在结果上看起来具有高数字的切换:

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)
Run Code Online (Sandbox Code Playgroud)

该表大约有250万条记录,id是主键,它在另一个字段上有一个唯一索引(未包含在更新中).

这是一张innodb表.

什么可能是原因的任何指针?

任何可以帮助追踪问题的特定变量?

更新有"解释"吗?

编辑:我也注意到该表还有一个:

`modDate` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
Run Code Online (Sandbox Code Playgroud)

说明:

+----+-------------+---------------+-------+---------------+---------+---------+-------+------+-------+
| 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)
Run Code Online (Sandbox Code Playgroud)

Kei*_*all 9

如果id确实是主键,那么查询应该花费很长时间(除非你有很多很多的id等于2229230?).请运行以下两个sqls并发布结果:

show create table table1;
explain select * from table1 where id = 2229230;
Run Code Online (Sandbox Code Playgroud)

更新:只是为了完成,也做一个

select count(*) from table1 where id = 2229230;
Run Code Online (Sandbox Code Playgroud)


web*_*ber 7

好几小时,在追踪了这个问题之后,似乎原因是“重复索引”,我为回答Keith而创建的create表具有以下奇怪的组合:

  • fieldx上的唯一键
  • Fieldx上的钥匙

第二个显然是多余的并且没有用,但是在我删除密钥之后,所有更新都恢复到了不到1秒的时间。

+1对Keith和Ivan,因为他们的评论帮助我最终找到了问题所在。


Der*_*sed 5

我也可以建议:

OPTIMIZE TABLE table1;
Run Code Online (Sandbox Code Playgroud)

有时候,您的表只需要一点点爱,如果它们快速增长并且您有一段时间没有对其进行优化,那么索引可能会有些疯狂。实际上,如果您有时间(并且有时间),那么投入就不会有任何伤害

REPAIR TABLE table1;
Run Code Online (Sandbox Code Playgroud)

  • 请从[手册](https://dev.mysql.com/doc/refman/8.0/en/repair-table.html)中小心:**重要**在执行表修复之前,请对表进行备份。操作 在某些情况下,该操作可能会导致数据丢失。”。 (2认同)