主键的MySQL UPDATE查询有时非常慢

时间:2009-07-02 14:41:02

标签: mysql

我们网站上的特定UPDATE查询有时会运行速度非常慢,并检查的行数超出了必要的范围。它按主键过滤,所以我希望MySQL总是只需要检查一行。

以下是MySQL慢速查询日志中的一些示例:

# Time: 090702 12:59:06
# User@Host: XXX[XXX] @ XXX [XXX]
# Query_time: 21  Lock_time: 0  Rows_sent: 0  Rows_examined: 500500
SET timestamp=1246532346;
UPDATE `folders` SET `folder_id` = '1705641', `updated_at` = now() WHERE `folders`.`id` = '1682995';
# Time: 090702 14:13:44
# User@Host: XXX[XXX] @ XXX [XXX]
# Query_time: 17  Lock_time: 0  Rows_sent: 0  Rows_examined: 16816745
SET timestamp=1246536824;
UPDATE `folders` SET `folder_id` = '417997', `updated_at` = now() WHERE `folders`.`id` = '1705956';
# Time: 090702 14:15:42
# User@Host: XXX[XXX] @ XXX [XXX]
# Query_time: 13  Lock_time: 0  Rows_sent: 0  Rows_examined: 16816719
SET timestamp=1246536942;
UPDATE `folders` SET `folder_id` = '1706267', `updated_at` = now() WHERE `folders`.`id` = '1705956';
# Time: 090702 16:07:43
# User@Host: XXX[XXX] @ XXX [XXX]
# Query_time: 499  Lock_time: 0  Rows_sent: 0  Rows_examined: 88668449
SET timestamp=1246543663;
UPDATE `folders` SET `folder_id` = '1707407', `updated_at` = now() WHERE `folders`.`id` = '1706992';

但是,查询的运行频率高于此,因此并不总是会暴露此行为。 此外,如果我手动运行相同的查询,它们运行正常并立即返回。

我也验证了桌子,据我所知,它应该没问题:

mysql> describe folders;
+------------------+-----------------------+------+-----+---------------------+----------------+
| Field            | Type                  | Null | Key | Default             | Extra          |
+------------------+-----------------------+------+-----+---------------------+----------------+
| id               | mediumint(8) unsigned | NO   | PRI | NULL                | auto_increment | 
| user_id          | mediumint(8) unsigned | NO   | MUL | NULL                |                | 
| folder_id        | mediumint(8) unsigned | YES  | MUL | NULL                |                | 
| created_at       | timestamp             | NO   |     | CURRENT_TIMESTAMP   |                | 
| updated_at       | timestamp             | NO   |     | 0000-00-00 00:00:00 |                | 
| modified_at      | timestamp             | NO   |     | 0000-00-00 00:00:00 |                | 
| name             | varchar(255)          | NO   |     | NULL                |                | 
| guest_permission | tinyint(3) unsigned   | NO   |     | 1                   |                | 
+------------------+-----------------------+------+-----+---------------------+----------------+
8 rows in set (0.00 sec)

mysql> show index from folders;
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table   | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| folders |          0 | PRIMARY   |            1 | id          | A         |      760318 |     NULL | NULL   |      | BTREE      |         | 
| folders |          1 | user_id   |            1 | user_id     | A         |       69119 |     NULL | NULL   |      | BTREE      |         | 
| folders |          1 | folder_id |            1 | folder_id   | A         |      380159 |     NULL | NULL   | YES  | BTREE      |         | 
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
3 rows in set (0.00 sec)

另一件事是MySQL服务器有时会锁定并停止接受连接,每次发生这种情况时,我通常会在失败之前立即在日志文件中找到这些慢速查询之一。我在其他日志文件中看不到任何相关的错误消息,但MySQL重启会使其再次响应。

是否有人知道发生了什么,或者我可以检查什么以缩小问题范围?

编辑:我们在专用服务器上使用MySQL 5.0.51a,目前有6个运行PHP 5.2.6的Web服务器并通过PDO连接到MySQL服务器。所有服务器都在运行Debian Lenny。慢查询发生在所有网络服务器上。

编辑:这是一个相关查询的EXPLAIN,有和没有引号:

mysql> explain SELECT * FROM `folders` WHERE `folders`.`id` = '1682995';
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table   | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | folders | const | PRIMARY       | PRIMARY | 3       | const |    1 |       | 
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

mysql> explain SELECT * FROM `folders` WHERE `folders`.`id` = 1682995;
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table   | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | folders | const | PRIMARY       | PRIMARY | 3       | const |    1 |       | 
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

2 个答案:

答案 0 :(得分:1)

这很奇怪,但我的猜测是可以因为你的id字段是int但是你传递了一个字符串(引用)。尽量不要使用字符串,看看它是否有帮助。

为了帮助确定究竟发生了什么,请将查询更改为具有相同WHERE子句的SELECT,并通过EXPLAIN运行它。像这样:

EXPLAIN SELECT * FROM `folders` WHERE `folders`.`id` = '1682995';
EXPLAIN SELECT * FROM `folders` WHERE `folders`.`id` = 1682995;

看看是否存在差异。

More info on EXPLAIN

答案 1 :(得分:1)

傻傻的我。我忘了folders表上有一些触发器,当然这是其中一个触发器内的查询导致我的问题......

还有一个额外的表tree,用于维护文件夹之间的关联,并且当在层次结构中删除或移动文件夹时,触发器会更新这些关联。在UPDATE触发器中,它必须在添加新引用之前删除对该文件夹的所有现有引用。相关的DELETE查询开头如下:

DELETE FROM `tree`
  WHERE `folder_id` IN (
    SELECT `folder_id` FROM `children`
  )
  AND ...

children是一个临时表,我之前存储了我需要的文件夹ID。 现在由于某些原因MySQL无法优化此查询,但如果我使用RIGHT JOIN而不是它完全正常:

DELETE `tree`.* FROM `tree`
  RIGHT JOIN children USING (folder_id)
  WHERE ...

由于我已经更改了此查询,因此MySQL的慢查询日志仍然空白,并且我们没有遇到任何MySQL锁定。