Posted shysheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
鍡紝棣栧厛鎵胯锛屽仛浜嗗洖鏍囬鍏氾紙鍙﹀涔熸槸鍥犱负纭疄娌℃兂鍒版洿濂界殑鏍囬锛屾眰杞绘媿锛夈€傜敱浜庝慨澶嶅強鏃讹紝鏈€缁堝苟娌℃湁瀵圭嚎涓婄敤鎴蜂骇鐢熶换浣曞奖鍝嶏紝琛€妗堜篃骞舵病鏈夊彂鐢熴€?/p>
宸ヤ綔涓夊勾鍗婁簡锛岀涓€娆$鍒颁簡mysql姝婚攣锛岃€佸疄璇村綋鏃惰繕鎸哄紑蹇冪殑馃槅
1.妗堜欢鍥為【
鍘诲勾骞冲畨澶滐紝绗旇€呭弬涓庣殑涓€涓」鐩鏈熶笂绾裤€備綔涓轰竴鍚嶈€佸徃鏈猴紝涓€鍒囬兘鏄偅涔堣交杞︾啛璺€傚彂甯冨畬鎴愮殑閭d竴鍒伙紝鍢磋涓嶈嚜瑙夋诞璧蜂簡涓€涓濅笉鏄撳療瑙夌殑寰瑧锛岀劧鑰屾垜涓嶇煡閬撶殑鏄紝鍗遍櫓姝e湪涓€姝ユ闈犺繎銆?/p>
鍦ㄩ殢鍚嶲A鍚屽鍜屼骇鍝佸悓瀛︾殑绾夸笂楠屾敹杩囩▼涓紝鍙戠幇鏈夋瀬楂樼殑姒傜巼浼氬嚭鐜版敮浠樺け璐ョ殑鍦烘櫙銆備粠褰撴椂鐨勬棩蹇楁潵鐪嬶紝杩欐槸鐢变簬鏁版嵁搴撴閿佸紩璧风殑锛屽涓嬪浘鎵€绀猴細
妗堝彂鐜板満姝婚攣鏃ュ織.png
棰勫彂鐜楠岃瘉寰楀ソ濂界殑锛屼负浠€涔堜竴鍒扮嚎涓婂氨浼氭湁鏋侀珮鐨勬鐜囧嚭鐜版閿佸憿锛熺瑪鑰呬粩缁嗗垎鏋愪簡鍑虹幇姝婚攣鐨剆ql锛屽彂鐜板湪涓婄嚎鍓嶅璇ql鏈夊井灏忕殑鏀瑰姩锛屽綋鏃朵篃鏄负浜嗕慨澶嶄竴涓湁鏋佷綆姒傜巼鍑虹幇鐨勯棶棰樸€傚彧鏄綋鏃朵慨鏀逛互鍚庡彧瀵圭浉鍏虫祦绋嬭繘琛屼簡涓€娆¢獙璇侊紝鑰屽垰濂介偅涓€娆℃病鏈夊嚭鐜版閿侊紝鎵€浠ヨ繖涓湁闂sql灏辫繖鏍疯閿欒鍦板彂甯冨埌浜嗙嚎涓婄幆澧冦€?/p>
浣滀负涓€涓編涓歋AAS浜у搧锛屽紑鍗曟敮浠橀摼璺槸缁濆鐨勬牳蹇冨満鏅紝淇濊瘉璇ユ祦绋嬬殑绋冲畾鏄竴绛変竴鐨勫ぇ浜嬨€傝€冭檻鍒扮煭鏃堕棿鍐呭交搴曞畾浣嶄慨澶嶈闂涓嶅お鐜板疄锛屼负浜嗕繚璇佺嚎涓婄幆澧冪殑绋冲畾锛岀瑪鑰呭綋鏃剁揣鎬ュ杩欎釜鏈夐棶棰樼殑sql杩涜浜嗗洖婊氾紝鐢变簬褰撴椂宸叉帴杩戝崍澶滐紝鍐嶅姞涓婂彂甯冪殑鏄竴涓柊鍔熻兘锛屾墍浠ュ苟娌℃湁瀵圭嚎涓婄敤鎴烽€犳垚浠讳綍褰卞搷锛岀瑪鑰呭氨杩欐牱涓庝竴鍦鸿妗堟摝鑲╄€岃繃...
绾夸笂闂澶勭悊棣栬鍘熷垯锛氱涓€鏃堕棿瑙e喅闂锛屼繚鐣欑幇鍦轰簨鍚庡垎鏋愩€?/p>
2.妗堟儏鍒嗘瀽
绗簩澶╀竴鏃╋紝绗旇€呰揩涓嶅強寰呬粩缁嗗垎鏋愪簡鏃ュ織锛屾壘鍒颁簡鍙戠敓姝婚攣鐨勪袱涓簨鍔★紝鐩稿叧sql绠€鍖栧鐞嗗悗濡備笅锛屼簨鍔¢殧绂荤骇鍒负RC锛?br class="mq-12">浜嬪姟1锛?/p>
update
table_name
set colume1 = 2
where kdt_id = 6666 and
order_no = 'M1' and
pay_no = 'P1' and
colume1 in (1, 2, 3) limit 1;
浜嬪姟2锛?/p>
update
table_name
set colume1 = 3
where kdt_id = 6666 and
order_no = 'M1' and
pay_no = 'P1' limit 1;
鍏朵腑琛╰able_name鐨勭储寮曚俊鎭涓嬶細
PRIMARY KEY (`id`),
KEY `idx_kdt_order` (`kdt_id`,`order_no`),
KEY `idx_payno_kdt` (`pay_no`,`kdt_id`),
KEY `idx_kdt_asset_detail_no` (`kdt_id`,`asset_detail_no`)
鍚屾椂鏌ョ湅涓や釜update璇彞鐨勬墽琛岃鍒掞紝鍙戠幇浜岃€呰蛋鐨勬槸鍚屼竴涓猧dx_payno_kdt杩欎釜浜岀骇绱㈠紩銆傛棦鐒惰蛋鐨勬槸鍚屼竴涓簩绾х储寮曪紝鎸夌収甯歌鍔犻攣濂楄矾锛宮ysql浼氶鍏堝杩欎釜浜岀骇绱㈠紩鍔犻攣锛岀劧鍚庡涓婚敭鍔犻攣銆備袱涓猻ql鐨勫姞閿侀『搴忓簲璇ュ畬鍏ㄤ竴鏍凤紝鏃㈢劧濡傛锛屼綍鏉ユ閿侊紵姝ゆ椂锛岀瑪鑰呴櫡鍏ヤ簡婕暱鐨勬矇鎬濄€?/p>
3.杩炵幆浣滄锛?/h4>
姣曠珶鏄涓€娆″鐞嗘閿佹渚嬶紝鍑犲ぉ涓嬫潵姣棤杩涘睍銆傛褰撴垜鎵嬭冻鏃犳帾涔嬫椂锛屼竴灏佸憡璀﹂偖浠舵槧鍏ョ溂甯樸€傜瑪鑰呭鐐殑鐩厜琚偅涓啛鎮夌殑鏈嬪弸缁欐繁娣卞湴鍚稿紩浣忎簡鈥斺€擠eadLock銆傛垜鍕掍釜涔栦箹锛岃繕鏄釜杩炵幆妗堝晩銆傛湁鎰忔€濅簡锛屼簩璇濅笉璇达紝鍏堟嬁鍒拌Е鍙戞閿佺殑sql銆?br class="mq-45">浜嬪姟1锛?/p>
UPDATE
table_name
SET`asset_detail_no`='xx',`ext_pay_info`='xx'`updated_at`='2019-01-02 17:02:57'where
`kdt_id`=6666 and `order_no`='M1' and `pay_no`='P1' limit 1;
浜嬪姟2锛?/p>
UPDATE
table_name
SET`pay_result`=1,`pay_status`=3,`updated_at`='2019-01-02 17:02:57'where
`kdt_id`=6666 and `order_no`='M1' and `pay_no`='P1' limit 1
鍚屾牱锛屼袱涓猽pdate璇彞閮借蛋鐨勬槸idx_payno_kdt杩欎釜浜岀骇绱㈠紩銆傚悓鏃剁敱浜庝笂涓€娆$殑缁忛獙锛岃繖娆¢┈涓婅仈绯籇BA鎷垮埌浜嗘閿佹棩蹇楋細
------------------------
LATEST DETECTED DEADLOCK
------------------------2019-01-02 17:02:57 0x7f8ed339d700*** (1) TRANSACTION:TRANSACTION 154905880096, ACTIVE 0 sec starting index read
mysql tables in use 1, locked 1LOCK WAIT 3 lock struct(s), heap size 1136, 2 row lock(s)
MySQL thread id 10519814, OS thread handle 140251427940096, query id 37831367279 10.96.0.7 beauty updating
UPDATE
table_name
SET `asset_detail_no`='xx', `ext_pay_info`='xx', `updated_at`='2019-01-02 17:02:57'
where `kdt_id`=6666 and `order_no`='M1' and `pay_no`='P1' limit 1*** (1) WAITING FOR THIS LOCK TO BE GRANTED:RECORD LOCKS space id 3208 page no 28442 n bits 112 index PRIMARY of table `db_name`.`table_name` trx id 154905880096 lock_mode X locks rec but not gap waiting
*** (2) TRANSACTION:TRANSACTION 154905880094, ACTIVE 0 sec starting index read, thread declared inside InnoDB 5000mysql tables in use 1, locked 15 lock struct(s), heap size 1136, 3 row lock(s)
MySQL thread id 10521136, OS thread handle 140251405866752, query id 37831367285 10.96.0.16 beauty updating
UPDATE `table_name`
SET `pay_result`=1, `pay_status`=30, `pay_at`='2019-01-02 17:02:57', `updated_at`='2019-01-02 17:02:57'
where `kdt_id`=6666 and `order_no`='M1' and `pay_no`='P1' limit 1*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 3208 page no 28442 n bits 112 index PRIMARY of table `db_name`.`table_name` trx id 154905880094 lock_mode X locks rec but not gap
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:RECORD LOCKS space id 3208 page no 28233 n bits 160 index idx_payno_kdt of table `db_name`.`table_name` trx id 154905880094 lock_mode X locks rec but not gap waiting
*** WE ROLL BACK TRANSACTION (1)
鏈変簡姝婚攣鏃ュ織锛屽緢澶氫笢瑗块兘涓€鐩簡鐒讹紝鍏朵綔鐢ㄧ畝鐩村牚姣旇溅绁哥幇鍦虹殑琛岃溅璁板綍浠晩锛?/p>
浠庝互涓婃閿佹棩蹇椾腑鎴戜滑鍙互鐪嬪埌姝婚攣绌剁珶鏄浣曞彂鐢熺殑:
浜嬪姟1鍦ㄧ瓑绛夎〃鐨勪富閿攣锛岃€屼簨鍔?鎸佹湁涓婚敭閿侊紝鍦ㄧ瓑寰呬簩绾х储寮昳dx_payno_kdt涓婄殑閿併€傛棦鐒跺彂鐢熶簡姝婚攣锛岃鏄庝簨鍔?搴旇鏄寔鏈変簡idx_payno_kdt鐨勯攣(浣嗘槸杩欎釜鍦ㄦ閿佹棩蹇椾腑骞舵棤璁板綍)锛屼粠鑰屽鑷翠袱涓簨鍔′簰鐩哥瓑寰呭鏂规寔鏈夌殑閿併€?/p>
4.妗堜欢鍚堝苟
铏界劧涓ゆ姝婚攣鍙戠敓鐨剆ql璇彞骞朵笉瀹屽叏涓€鏍凤紝浣嗛兘鏄洜涓轰袱涓簨鍔″鍚屼竴寮犺〃杩涜update寮曡捣鐨勩€傝€屼笖鐢变簬show engine innodb status鍙兘璁板綍鏈€杩戜竴娆″彂鐢熺殑姝婚攣锛屾墍浠ユ垜浠绗竴娆℃閿佸凡鏃犳硶鎷垮埌鏇村鐜板満淇℃伅銆傚啀缁撳悎涓氬姟鍦烘櫙锛岀瑪鑰呭喅瀹氬涓ゆ鍚堝苟涓€妗堣繘琛屽垎鏋愩€?/p>
绾夸笂姝婚攣瀹氫綅棣栬鍘熷垯锛氱涓€鏃堕棿鎷垮埌姝婚攣鏃ュ織鍗忓姪鍒嗘瀽銆?/p>
鍦ㄦ浠?涓紝鎴戜滑鐩墠鏈€澶х殑鐤戦棶鏄細浜嬪姟2鎬庝箞鎷垮埌涓婚敭閿佺殑锛熸槸鍥犱负鍙戠敓浜唅ndex_merge浼樺寲鍚?鍏蜂綋鍙弬鑰僫ndex_merge寮曞彂鐨勬閿佹帓鏌??
浣嗘槸閫氳繃鏌ョ湅鎵ц璁″垝锛屾垜浠彂鐜颁袱涓猽pdate璇彞鎵ц璁″垝涓殑type鍊奸兘鏄痳ange锛屽嵆閮芥病鏈夎蛋index_merge浼樺寲锛屾璺笉閫氿煒?/p>
鏃㈢劧璺焛ndex_merge娌℃湁鍏崇郴锛岃繕鏈変粈涔堟儏鍐靛彲鑳戒細瀵艰嚧浜嬪姟2鎸佹湁涓婚敭閿佸憿锛熺瑪鑰呭啀娆¢櫡鍏ヤ簡娌夋€?..
鍦ㄦ暣涓浠剁殑鍒嗘瀽杩囩▼涓紝绗旇€呭叾瀹炰竴鐩村湪璺烡BA淇濇寔娌熼€?涓€涓細鍋氶キ鐨勬殩鐢凤紝鎶€鏈緢鐗涢€硷紝绉疮浜嗗緢澶氬畾浣嶇嚎涓妋ysql闂鐨勭粡楠屽苟浠ュ崥瀹㈠舰寮忚緭鍑猴紝浼犻€侀棬锛欶or DBA)銆傛湡闂碊BA鐨勪竴涓€濊矾缁欎簡鎴戝惎鍙戯紝浠栬鍥犱负琛╰able_name涓瓨鍦ㄥ涓簩绾х储寮曪紝鍦╳here鏉′欢涓紝order_no鍜宲ay_no鍒嗗埆鏄袱涓簩绾х储寮曚腑鐨勫瓧娈碉紝鏈夊彲鑳芥槸鍦ㄦ煇绉嶆潯浠朵笅锛屼簨鍔?鍏堣蛋浜唅dx_kdt_order绱㈠紩锛屼粠鑰岄€氳繃璇ョ储寮曟嬁鍒颁簡涓婚敭閿併€?/p>
鐒惰€屼簨鍔?鐨勬墽琛岃鍒掓竻娓呮妤氭槑鏄庣櫧鐧藉湴鍛婅瘔鎴戜滑锛岃update璇彞鍙蛋浜唅dx_payno_kdt杩欎釜涓€涓簩绾х储寮曪紝閭d箞鍙兘浼氭湁浠€涔堝満鏅Е鍙戝叾鍏堣蛋idx_kdt_order杩欎釜绱㈠紩鍛紵
鍝﹀浜嗭紒绗旇€呯獊鐒剁伒鍏変竴鐜帮紝姝婚攣鏃ュ織鍙細璁板綍鍙戠敓姝婚攣鐨剆ql璇彞锛屼絾鏄竴涓簨鍔′腑鍙兘浼氭湁澶氫釜sql璇彞锛屽湪鍙戠敓姝婚攣涔嬪墠鐨剆ql璇彞閮戒笉浼氳璁板綍鍒版閿佹棩蹇椾腑锛?/p>
鎯冲埌杩欎竴鐐癸紝绗旇€呴殣闅愭劅瑙夌湡鐩歌秺鏉ヨ秺杩戜簡銆傜敱浜庡妗堜欢鍒嗘瀽宸茬粡鏂柇缁画鎸佺画浜?0鏉ュぉ锛屾垜涓嶇敤缁х画缈讳唬鐮侀兘鐭ラ亾浜嬪姟2鍦ㄦ墽琛寀pdate璇彞涔嬪墠杩樻墽琛屼簡涓€涓猻elect璇彞锛屽苟涓斿垰濂借蛋浜唅dx_kdt_order杩欎釜绱㈠紩锛岃select璇彞濡備笅锛?/p>
select * from
table_name
where
kdt_id=6666 and order_no=鈥楳1' and pay_status in (3, 5) for update;
鍦ㄨ璇彞涓紝浜嬪姟2浼氶€氳繃idx_kdt_order杩欎釜浜岀骇绱㈠紩鎸佹湁涓婚敭閿侊紝杩欏氨瀹岀編鍦拌В閲婁簡鎴戜滑涔嬪墠鐨勭枒闂€傛鏄繖涓猻elect ... for update璇彞鐨勫瓨鍦紝瀵艰嚧浜嗕袱涓簨鍔$殑鍔犻攣椤哄簭鍙戠敓浜嗕笉涓€鑷达紝浠庤€屽紩鍙戞閿併€?br class="mq-209">鎴戞兂锛屾鍙戠幇鍦哄綋鏃剁殑鎯呭喌搴旇鏄繖鏍风殑锛?/p>
image.png
5.鐪熺浉澶х櫧锛烴o锛?/h4>
鑷虫锛屾鎯呬技涔庣湡鐩稿ぇ鐧戒簡锛岀姜榄佺ジ棣栨鏄繖涓猻elect ... for update璇彞銆傜劧鑰岋紝浜嬪疄鏋滅湡濡傛鍚楋紵
鍦ㄨ窡DBA鐨勬矡閫氳繃绋嬩腑锛岀瑪鑰呰繕寰楀埌涓€涓俊鎭氨鏄紝鍦≧C闅旂绾у埆涓嬶紝閽堝select ... for update璇彞锛屽鏋滄渶缁堣繑鍥炵粨鏋滈泦涓虹┖锛岄偅涔坢ysql浼氬姝よ繘琛屼簡涓€浜涗紭鍖栵紝鍔犱笂鐨刋閿佷細鎻愬墠閲婃斁(璇︾粏鍒嗘瀽鍙弬鑰僊ySQL鍔犻攣澶勭悊鍒嗘瀽)銆?/p>
鏈潃瀹炶返鍑虹湡鐭ョ殑鍘熷垯锛岀瑪鑰呭湪鏈湴mysql瀵规杩涜浜嗛獙璇侊紝鍏朵腑t1涓庣嚎涓婄幆澧冨彂鐢熸閿佺殑table_name瀹屽叏涓€鑷达紝涓旀弧瓒砶dt_id=6666 and order_no='M1'鏉′欢鐨勮褰曟湁涓ゆ潯锛屼絾瀹冧滑鐨刾ay_status鐘舵€侀兘涓嶆槸30鎴栬€?0锛屼篃灏辨槸璇达紝浜嬪姟1杩斿洖鐨勭粨鏋滈泦涓虹┖銆?br class="mq-215">浜嬪姟1锛?/p>
begin;
select * from order_t1 where kdt_id=6666 and order_no='M1' and pay_status in (30, 50) for update;
浜嬪姟2锛?/p>
begin ;
UPDATE
t1
SET`updated_at`='2019-01-02 17:02:57'where `kdt_id`=6666 and `order_no`='M1' and `pay_no`='P1' limit 1;
鍏堟墽琛屼簨鍔?锛屽啀鎵ц浜嬪姟2锛屽湪RR绾у埆涓嬶紝浜嬪姟2纭疄浼氳闃诲锛屼絾鏄湪RC绾у埆涓嬪嵈涓嶄細銆?/p>
杩欎篃瀹岀編鏄犺瘉浜咲BA鐨勮娉曪紝閽堝select ... for update璇彞锛屽湪RC闅旂绾у埆涓嬶紝濡傛灉杩斿洖缁撴灉闆嗕负绌猴紝閭d箞閽堝琛岃褰曞姞涓婄殑X閿佷細琚彁鍓嶉噴鏀撅紝涓嶄細闃诲鍏跺畠浜嬪姟鍔犻攣銆?/p>
鏃㈢劧浼氭彁鍓嶉噴鏀撅紝閭d箞妗堜緥2涓紝浜嬪姟2鍙堟槸濡備綍鑾峰緱X閿佺殑鍛紵鐪熺浉浼间箮杩戝湪鐪煎墠锛屽嵈鍙堣繙鍦ㄥぉ杈桂煒?/p>
涓嶈繃鍒颁簡杩欓噷锛屾渚?铏界劧渚濈劧鐤戠偣閲嶉噸锛屾渚?鍗翠技涔庢煶鏆楄姳鏄庝簡銆傚洜涓哄湪妗堜緥1涓紝浜嬪姟2鐨剈pdate涔嬪墠涔熸湁涓€涓被浼肩殑select... for update璇彞濡備笅锛?/p>
select * from order_t1 where kdt_id=6666 and order_no='M1' for update;
鍙笉杩囦笌妗堜緥2鐨勭浉姣旓紝where鏉′欢涓病鏈変娇鐢╬ay_status瀛楁杩涜杩囨护锛屼篃灏辨槸璇磋繖涓鍙ョ殑缁撴灉闆嗕笉涓虹┖锛侀偅涔堝浜庢渚?鏉ヨ锛屼簨鍔?鍦ㄦ墽琛寀dpate璇彞涔嬪墠灏辩湡鐨勪細閫氳繃idx_kdt_order鎸佹湁涓婚敭閿侊紝浠庤€岃Е鍙戞閿侊紒
6.鐪熺浉锛熷惥灏嗕笂涓嬭€屾眰绱?/h4>
鍒颁簡杩欓噷锛岀绗竴娆″嚭鐜版閿佸凡杩囧幓宸笉澶氫袱涓槦鏈燂紝妗堜緥1宸茬湡鐩稿ぇ鐧斤紝浣嗘渚?鍗翠緷鐒跺瓨鐤戙€?/p>
鍥犳绗旇€呭喅瀹氾紝瑕佹槸骞翠細涔嬪墠杩樻病鏈夋按钀界煶鍑猴紝灏辨墦椋炵殑杩囧幓娲绘崏DBA鐜板満缁撳缂栫▼馃槅寰呯湡鐩稿ぇ鐧藉悗鍐嶆潵璺熷皬浼欎即浠垎浜?/p>
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章