mysql锁表更新_Mysql InnoDB 数据更新导致锁表
一、數(shù)據(jù)表結構
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;
二、現(xiàn)象
當多個連接同時對一個表的數(shù)據(jù)進行更新操作,那么速度將會越來越慢,持續(xù)一段時間后將出現(xiàn)數(shù)據(jù)表被鎖,從而影響到其它的查詢及更新。
存儲過程循環(huán)30次更新操作
/*30次更新操作*/
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
執(zhí)行結果(速度非常慢)
時間: 29.876s
Procedure executed successfully
受影響的行: 0
200個數(shù)據(jù)更新操作,三個數(shù)據(jù)庫連接同時執(zhí)行
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';
...等等
執(zhí)行結果(持續(xù)一段時間后速度越來越慢,出現(xiàn)等待鎖)
# 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 *
FROMjx_attach ja,jx_feed jfwhere 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的行鎖是通過給索引項加鎖實現(xiàn)的,這就意味著只有通過索引條件檢索數(shù)據(jù)時,innodb才使用行鎖,否則使用表鎖。根據(jù)當前的數(shù)據(jù)更新語句(update jx_attach set complete=1,attach_size=63100 where cycore_file_id='56677142da502cd8907eb58f';),該條件字段cycore_file_id并沒有添加索引,所以導致數(shù)據(jù)表被鎖。
四、解決辦法
為cycore_file_id添加索引
五、最終效果(30次更新操作)
時間: 0.094s
Procedure executed successfully
受影響的行: 0
總結
以上是生活随笔為你收集整理的mysql锁表更新_Mysql InnoDB 数据更新导致锁表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4阶范德蒙德行列式例题_线性代数入门——
- 下一篇: spi 动态加载、卸载_理解 Servi