Mysql InnoDB data update causes lock table

 First, the data table structure

CREATE TABLE `jx_attach` (
  `attach_id` int(11) NOT NULL AUTO_INCREMENT,
  `feed_id` int(11) DEFAULT NULL ,
  `attach_name` varchar(255) NOT NULL,
  `cycore_file_id` varchar(255) DEFAULT NULL ,
  `attach_size` bigint(20) NOT NULL DEFAULT '0',
  `complete` smallint(6) NOT NULL DEFAULT '0' ,
  PRIMARY KEY (`attach_id`),
  KEY `jx_trend_attach_FK` (`feed_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=394160 DEFAULT CHARSET=utf8;

2. Phenomenon

  When multiple connections update the data of a table at the same time, the speed will become slower and slower, and the data table will be locked after a period of time, which will affect other queries and updates.  

Stored procedure loops 30 update operations

/*30 update operations*/
BEGIN
  DECLARE v1 INT DEFAULT 30;
  WHILE v1 > 0 DO
    update jx_attach set complete=1,attach_size=63100 where cycore_file_id='56677142da502cd8907eb58f';
    SET v1 = v1 - 1;
  END WHILE;
END

Execution result (very slow)

Time: 29.876s

Procedure executed successfully
Affected rows: 0

 

200 data update operations performed simultaneously on three database connections

update jx_attach set complete=1,attach_size=63100 where cycore_file_id='56677142da502cd8907eb58f';
 update jx_attach set complete=1,attach_size=63100 where cycore_file_id='56677142da502cd8907eb58f';
 update jx_attach set complete=1,attach_size=63100 where cycore_file_id='56677142da502cd8907eb58f';
 update jx_attach set complete=1,attach_size=63100 where cycore_file_id='56677142da502cd8907eb58f';
 update jx_attach set complete=1,attach_size=63100 where cycore_file_id='56677142da502cd8907eb58f';
 update jx_attach set complete=1,attach_size=63100 where cycore_file_id='56677142da502cd8907eb58f';
...etc

Execution result (after a period of time, the speed becomes slower and slower, and there is a waiting lock) 

# Time: 151208 22:41:24
# User@Host: zmduan[zmduan] @ [192.168.235.1] Id: 2
# Query_time: 1.848644 Lock_time: 0.780778 Rows_sent: 0 Rows_examined: 393382
SET timestamp=1449643284;
update jx_attach set complete=1,attach_size=63100 where cycore_file_id='56677142da502cd8907eb58f';

.........
........

# User@Host: zmduan[zmduan] @  [192.168.235.1]  Id:     2
# Query_time: 2.868598  Lock_time: 1.558542 Rows_sent: 0  Rows_examined: 393382
SET timestamp=1449643805;
update jx_attach set complete=1,attach_size=63100 where cycore_file_id='56677142da502cd8907eb58f';
[root@localhost log]# tail -f slow_query.log
# User@Host: zmduan[zmduan] @  [192.168.235.1]  Id:    19
# Query_time: 1.356797  Lock_time: 0.000169 Rows_sent: 1  Rows_examined: 393383
SET timestamp=1449643805;
SELECT *
FROM jx_attach ja,jx_feed jf
where ja.feed_id=jf.feed_id and ja.cycore_file_id='56677146da502cd8907eb5b7';
# User@Host: zmduan[zmduan] @  [192.168.235.1]  Id:     2
# Query_time: 2.868598  Lock_time: 1.558542 Rows_sent: 0  Rows_examined: 393382
SET timestamp=1449643805;
update jx_attach set complete=1,attach_size=63100 where cycore_file_id='56677142da502cd8907eb58f';

 三、原因分析

MySQL的innodb存储引擎支持行级锁,innodb的行锁是通过给索引项加锁实现的,这就意味着只有通过索引条件检索数据时,innodb才使用行锁,否则使用表锁。根据当前的数据更新语句(update jx_attach set complete=1,attach_size=63100 where cycore_file_id='56677142da502cd8907eb58f';),该条件字段cycore_file_id并没有添加索引,所以导致数据表被锁。

四、解决办法

为cycore_file_id添加索引

五、最终效果(30次更新操作)

时间: 0.094s

Procedure executed successfully
受影响的行: 0

文章来源:https://www.cnblogs.com/zmduan/p/5033047.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325987056&siteId=291194637