【发布时间】:2010-12-10 07:29:17
【问题描述】:
在我们的服务出现一些预期的增长之后,突然一些更新需要很长时间,这些在表格达到大约 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)
【问题讨论】:
-
field1和id字段的类型是什么? -
您可以尝试分析此队列。见dev.mysql.com/tech-resources/articles/…
-
看起来很奇怪 - 1 行匹配,但零行受到影响?
-
没错,我实际上是用 field1 = field1 运行的
-
选择需要那么长时间,还是很快?另外,硬编码新的 field1 值需要那么长时间吗?
标签: sql mysql optimization