MySQL/MariaDB主键更新缓慢
查询: 0行受影响,但耗时1210毫秒。按id选择此行的时间始终为0毫秒。 表大小为6354行MySQL/MariaDB主键更新缓慢,mysql,sql,query-optimization,innodb,mariadb,Mysql,Sql,Query Optimization,Innodb,Mariadb,查询: 0行受影响,但耗时1210毫秒。按id选择此行的时间始终为0毫秒。 表大小为6354行 UPDATE `cart` SET `user_id` = NULL, `completed` = 0 WHERE `id` = 6948; Query OK, 0 rows affected (1.21 sec) Rows matched: 1 Changed: 0 Warnings: 0 以下是此查询的ShowProfile all摘录 > show create table cart
UPDATE `cart` SET `user_id` = NULL, `completed` = 0 WHERE `id` = 6948;
Query OK, 0 rows affected (1.21 sec)
Rows matched: 1 Changed: 0 Warnings: 0
以下是此查询的ShowProfile all摘录
> show create table cart;
CREATE TABLE `cart` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`completed` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'флаг, указывающий на оформление заказа из данной корзины',
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6964 DEFAULT CHARSET=utf8
> show index from cart;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| cart | 0 | PRIMARY | 1 | id | A | 6386 | NULL | NULL | | BTREE | | |
| cart | 1 | user_id | 1 | user_id | A | 2128 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
为什么阻塞操作如此不一致,从0到400,需要0毫秒到2500毫秒???如何找到其偏高的根本原因
服务器版本:10.0.17-MariaDB-1~喘息。VPS根本没有任何明显的负载
UPD:在查询后添加状态变量:
| Status | Duration | CPU_user | CPU_system | Context_voluntary | Context_involuntary | Block_ops_in | Block_ops_out | Messages_sent | Messages_received | Page_faults_major | Page_faults_minor | Swaps | Source_function | Source_file | Source_line |
| query end | 2.502555 | 0.003000 | 0.000000 | 88 | 8 | 0| 136 | 0 | 0 | 0 | 0 | 0 | mysql_execute_command | sql_parse.cc | 5093 |
我只能建议您尝试将表和数据迁移到Mysql MyIsam引擎中,并执行相同的查询。可能您只是发现了MariaDB的一个弱点,或者是某些配置错误或过度调整。问题不在于MySQL/MariaDB,而在于OpenVZ和服务器上的IO调度。迁移到生产率更高的磁盘后,问题消失了。我的第一个猜测是:您设置了属于索引的用户id,因此必须重建索引。但对于糟糕的6k记录,1秒是很奇怪的。可能有一个更新触发器在后面运行?@juergend没有更新触发器,一般整个数据库都没有触发器,请提供SHOW CREATE TABLE;这比描述更具描述性@RickJames完成了,更新了问题。发生了一些奇怪的事情。您可以这样重新运行:刷新状态;使现代化显示会话状态,如“处理程序%”;处理器计数可能会给我们一些令人惊讶的信息。MyISAM很可能比InnoDB更糟糕。这是因为MyISAM的表锁定。这种锁定不应该显著影响单行更新的执行时间。我只是想排除测试中可能出现的瓶颈。我想我也能找到同样的解决办法。
MariaDB> flush status; UPDATE `cart` SET `user_id` = NULL, `completed` = 0 WHERE `id` = 6948; SHOW SESSION STATUS LIKE 'Handler%';
Query OK, 0 rows affected (0.54 sec)
^[[A^[[BQuery OK, 0 rows affected (3.88 sec)
Rows matched: 1 Changed: 0 Warnings: 0
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Handler_commit | 2 |
| Handler_delete | 0 |
| Handler_discover | 0 |
| Handler_external_lock | 0 |
| Handler_icp_attempts | 0 |
| Handler_icp_match | 0 |
| Handler_mrr_init | 0 |
| Handler_mrr_key_refills | 0 |
| Handler_mrr_rowid_refills | 0 |
| Handler_prepare | 2 |
| Handler_read_first | 0 |
| Handler_read_key | 1 |
| Handler_read_last | 0 |
| Handler_read_next | 0 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_deleted | 0 |
| Handler_read_rnd_next | 0 |
| Handler_rollback | 0 |
| Handler_savepoint | 0 |
| Handler_savepoint_rollback | 0 |
| Handler_tmp_update | 0 |
| Handler_tmp_write | 0 |
| Handler_update | 1 |
| Handler_write | 0 |
+----------------------------+-------+
25 rows in set (0.00 sec)