您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“MYSQL對表的最大ID搶加鎖時的阻塞分析”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
示例SQL:
SELECT q.queueid FROM render.queues q WHERE q.queueid in ( SELECT max(queueid) FROM ( SELECT t.queueid FROM queues t WHERE 1 = 1 AND STATUS = 0 AND queuetype <> 1 。。。 ORDER BY queuetype ASC, createdate ASC ) a limit 1 ) FOR UPDATE ;
需求:
從ORACLE轉(zhuǎn)化到MYSQL的SQL,目的是多個會話輪詢?nèi)”碇袧M足條件最大ID的一行數(shù)據(jù),然后第一個搶到的會話需要對這一行數(shù)據(jù)加鎖,以免其他會話讀到該行數(shù)據(jù),在這期間,表是會不斷有新進數(shù)據(jù)插入的。
問題過程:
A會話執(zhí)行 select for update;
queues 11:03:13>set autocommit=0 -> ; Query OK, 0 rows affected (0.00 sec) -> SELECT -> q.queueid -> FROM -> queues.queues q -> WHERE -> q.queueid IN ( -> SELECT -> max(queueid) -> FROM -> ( -> SELECT -> t.queueid -> FROM -> queues t -> WHERE -> 1 = 1 -> AND STATUS = 0 -> AND queuetype <> 1 。。。 -> ORDER BY -> queuetype ASC, -> createdate ASC -> ) a -> ) FOR UPDATE ; +-----------+ | queueid | +-----------+ | 278082656 | +-----------+ 1 row in set (24.46 sec)
此時 B 會話繼續(xù)往表中插入數(shù)據(jù),讓ID不斷增長,例如當(dāng)前滿足條件的最大 QUEUEID 是278082665
接著 C 會話重新執(zhí)行以上 SELECT for update語句,理論上,當(dāng)再次查詢時需要加鎖的ID 應(yīng)該是 278082665,但發(fā)現(xiàn)此時C會話在等待鎖。
分析:
于是,查了下鎖看看:
oot@(none) 01:52:24>select * from information_schema.INNODB_LOCKS\G; *************************** 1. row *************************** lock_id: 133781546:45140:18:178 lock_trx_id: 133781546 lock_mode: X lock_type: RECORD lock_table: `queues`.`queues` lock_index: INDEX_QUEUE_QUEUETYPE lock_space: 45140 lock_page: 18 lock_rec: 178 lock_data: 0, 0, '1000505419', 0x99A438AAF5, 1280, 960, 278082656 *************************** 2. row *************************** lock_id: 133777540:45140:18:178 lock_trx_id: 133777540 lock_mode: X lock_type: RECORD lock_table: `queues`.`queues` lock_index: INDEX_QUEUE_QUEUETYPE lock_space: 45140 lock_page: 18 lock_rec: 178 lock_data: 0, 0, '1000505419', 0x99A438AAF5, 1280, 960, 278082656 2 rows in set, 1 warning (0.00 sec) ERROR: No query specified root@(none) 01:52:24>select * from information_schema.INNODB_LOCK_waits\G; *************************** 1. row *************************** requesting_trx_id: 133781546 requested_lock_id: 133781546:45140:18:178 blocking_trx_id: 133777540 blocking_lock_id: 133777540:45140:18:178 1 row in set, 1 warning (0.00 sec)
從上面結(jié)果發(fā)現(xiàn)lock_index是 INDEX_QUEUE_QUEUETYPE,而從SQL,我們繼續(xù)查看執(zhí)行計劃 。
+----+-------------+-------+------------+-------+-----------------------+-----------------------+---------+------+------+----------+--------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+-----------------------+-----------------------+---------+------+------+----------+--------------------------+ | 1 | PRIMARY | q | NULL | index | NULL | INDEX_QUEUE_QUEUETYPE | 285 | NULL | 2067 | 100.00 | Using where; Using index | | 2 | SUBQUERY | t | NULL | ALL | INDEX_QUEUE_QUEUETYPE | NULL | NULL | NULL | 2067 | 0.05 | Using where | +----+-------------+-------+------------+-------+-----------------------+-----------------------+---------+------+------+----------+--------------------------+
很明顯,主查詢使用了子查詢的索引,而該索引是非唯一性索引,MYSQL 的加鎖是基于索引加鎖的,同時從以上鎖情況可以看出此存在對包含278082656在內(nèi)的多條數(shù)據(jù)進行加鎖。
怎么解決:
于是我們再重新看SQL,其實這里應(yīng)該是要讓主查詢走具有唯一性的主鍵索引即可,這樣便不會存在以上問題了,只需將主查詢的WHERE q.queueid in ()改成 WHERE q.queueid =() ,這是開發(fā)規(guī)范問題。
SELECT q.queueid FROM queues.queues q WHERE q.queueid =( SELECT max(queueid) FROM ( SELECT t.queueid FROM queues t WHERE 1 = 1 AND STATUS = 0 AND queuetype <> 1 。。。 ORDER BY queuetype ASC, createdate ASC ) a limit 1 ) FOR UPDATE
修改之后的執(zhí)行計劃:
+----+-------------+-------+------------+-------+-----------------------+---------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+-----------------------+---------+---------+-------+------+----------+-------------+ | 1 | PRIMARY | q | NULL | const | PRIMARY | PRIMARY | 8 | const | 1 | 100.00 | Using index | | 2 | SUBQUERY | t | NULL | ALL | INDEX_QUEUE_QUEUETYPE | NULL | NULL | NULL | 2067 | 0.05 | Using where | +----+-------------+-------+------------+-------+-----------------------+---------+---------+-------+------+----------+-------------+
“MYSQL對表的最大ID搶加鎖時的阻塞分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。