当前位置:网站首页>MySQL Principle and Optimization: Update Optimization
MySQL Principle and Optimization: Update Optimization
2022-08-10 14:54:00 【InfoQ】
< br>< img SRC="/ / img.inotgo.com/imagesLocal/202208/10/202208101431472921_5.jpg" Alt=null loading=lazy>< br>< div> about Update Statement you may not strange, in many cases we can use it to update records in the table.In general we will use innodb Storage engines, innodb engine is based on the row locks, say they specifically targeting the index to lock () to ensure that no lock failure, is not to record locking, if for no indexed field filtering operation, to perform update , can lead to table locks & have spent.< / div>< div> today, just look at how to use when use the innodb update Statements.< / div>< br>< div> existing course Table, its content is as follows: < / div>< br>< img SRC="/ / img.inotgo.com/imagesLocal/202208/10/202208101431472921_3.png" Alt=null loading=lazy>< br>< br>< div> is the table without any index, id  by default;Is the index to the table.< / div>< div> then let us open two transactions respectively (the client), respectively, to perform the following instructions.< / div>< div> open the first transaction < / div>< div> begin;< / div>< div> update Course Set Name = & have spent'Java' & have spentWhere Id = & have spent1;< / div>< br>< img SRC="/ / img.inotgo.com/imagesLocal/202208/10/202208101431472921_4.png" Alt=null loading=lazy>< br>< br>< div> open another transaction < / div>< div> begin;< / div>< div> update Course Set Name = & have spent'Spark' & have spentWhere Id = & have spent4.< / div>< img SRC="/ / img.inotgo.com/imagesLocal/202208/10/202208101431472921_1.png" Alt=null loading=lazy>< br>< div> two transactions can execute, and then respectively perform two transactions & have spentCommit The result of the operation, can see the update.< / div>< br>< img SRC="/ / img.inotgo.com/imagesLocal/202208/10/202208101431472921_0.png" Alt=null loading=lazy>< br>< div> two transactions can be executed in parallel condition are id Is the table course Index, can be due to update Id Corresponding 2 & have spentAnd 4 & have spent, only for these two rows are locked.< / div>< div> then let's look at another example, is still open two transactions, but where Choose use name  conditions;And name Not as course The index of the table.< / div>< div> to start a transaction: < / div>< div> begin;< / div>< div> update Course Set Name = & have spent'the Vue. Js & have spentWhere Name = & have spent'PHP';< / div>< div> and then perform another transaction < / div>< div> begin;< / div>< div> update Course Set Name = & have spent'used to' & have spentWhere Name = & have spent'MySQL';< / div>< div> at this point in the implementation of the second transaction update Statement, will be blocked.Because for name The filter conditions not course The index of the update  at this time;The lock table statement for operation, order must be a transaction commit, release table locks, the second transaction to continue.< / div>< br>< img SRC="/ / img.inotgo.com/imagesLocal/202208/10/202208101431472921_2.png" Alt=null loading=lazy>< br>< br>< div> in order to let the two transactions can be executed in parallel, we will name Join the course The index.< / div>< div> create Index Index_name & have spentOn Course (name);< / div>< div> show Index From Course;< / div>< br>< img SRC="/ / img.inotgo.com/imagesLocal/202208/10/202208101431472921_6.png" Alt=null loading=lazy>< br>< div> then perform just two transactions, the two transactions to update a record different respectively, where The name of the condition of different cases, also won't appear the situation of the lock table, this is because the name As the course The index.< / div>< div> to demonstrate is convenient, we will update the condition here adjust: < / div>< div> to start a transaction: < / div>< div> begin;< / div>< div> update Course Set Name = & have spent'PHP' & have spentWhere Name = & have spent'the Vue. Js';< / div>< div> and then perform another transaction < / div>< div> begin;< / div>< div> update Course Set Name = & have spent'MySQL' & have spentWhere Name = & have spent'used to';< / div>< div> results as well as we had expected, two transactions can be executed in parallel, at the same time be able to see results after a commit.< / div>< br>< div> to summarize, & have spentIf use the innodb storage engines, update When where Conditions, < / div>< div> conditions field is the index < / div>< div> situation can promote the efficiency of the update, avoid the occurring of the lock table.
边栏推荐
猜你喜欢
随机推荐
Second half of 2011 System Architect Afternoon Paper II
Appium进行APP自动化测试
数字藏品平台系统开发实战
自定义picker滚动选择器样式
【Gazebo入门教程】第三讲 SDF文件的静/动态编程建模
【有限元分析】异型密封圈计算泄漏量与参数化优化过程(带分析源文件)
王学岗—————————哔哩哔哩直播-手写哔哩哔哩硬编码录屏推流(硬编)(26节课)
CSP-J1 CSP-S1 初赛 第1轮(2022.08.09)
从全球价值链视角看,京东云数智供应链对未来经济有何影响?
epoll学习:思考一种高性能的服务器处理框架
2022年网络安全培训火了,缺口达95%,揭开网络安全岗位神秘面纱
重要通知 | “移动云杯”算力网络应用创新大赛初赛延期!!
awk的简单使用
阿里五位MySQL封神大佬耗17个月总结出53章性能优化法则
systemui屏蔽通知栏
正则表达式(包含各种括号,echo,正则三剑客以及各种正则工具)
Circle 创始人回应美财政部禁止 Tornado :隐私与安全之间关系紧张
Do not access Object.prototype method ‘hasOwnProperty‘ from target object....
MySQL advanced (thirty-three) MySQL data table adding fields
Go Context基本使用