Hi,
I get an error "Deadlock found when trying to get lock; try restarting transaction" when there are 2 indexes (primary key index + one more) for a table, but not when there is only primary key index.
I concluded that this is a side effect of using a second index, by reading 'show innodb status' output (included below).
So my question is, is there any solution to my problem where multiple indexes can be involved but without causing deadlocks during transaction?
Table structure:
CREATE TABLE `gallery` (
`gallery_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`gallery_name` varchar(20) NOT NULL,
`video_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`gallery_id`),
KEY `Index_2` (`gallery_name`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The error occurs consistently when multiple concurrent transactions are simulated and both indexes have been created for table. But with 'Index_2' dropped, there is no deadlock.
Each transaction involves
- one delete * from gallery where gallery_name='name';
- Multiple insert into gallery values (...);
The second index was just to improve performance for queries having 'where' conditions with string matching, but is not critical and can be removed.
I'm new to innodb status report, but what I could infer is that one thread has got lock on primary index and is waiting for lock on 'Index_2', while another thread has got lock on 'Index_2' and is waiting for lock on primary. Is this correct? What can be done to prevent this? Should I look into transaction levels (am new to them too) to solve such issues?
Thanks
Karthik
Details:
--------
MySQL version: 5.1.30-community (x86 build)
OS: Windows 7 64 bit
The application is java. Mysql Connector/J verion is 5.1.12.
Junitperf LoadTest was used to simulate 10 concurrent updates.
Relevant code:
show innodb status output:
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 341, signal count 273
Mutex spin waits 0, rounds 6913, OS waits 205
RW-shared spins 151, OS waits 66; RW-excl spins 78, OS waits 49
------------------------
LATEST DETECTED DEADLOCK
------------------------
110105 12:31:30
*** (1) TRANSACTION:
TRANSACTION 0 19754, ACTIVE 0 sec, OS thread id 5200 starting index read
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 320, 14 row lock(s)
MySQL thread id 125, query id 1504 localhost 127.0.0.1 root updating
delete from gallery where gallery_name='Corporate'
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 220 n bits 112 index `PRIMARY` of table `app_test`.`gallery` trx id 0 19754 lock_
mode X locks rec but not gap waiting
Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000115; asc ;; 1: len 6; hex 000000004d2d; asc M-;; 2: len 7; hex 000000003a1de7; asc : ?;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000001; asc ;;
*** (2) TRANSACTION:
TRANSACTION 0 19757, ACTIVE 0 sec, OS thread id 6080 updating or deleting, thread declared inside InnoDB 0
mysql tables in use 1, locked 1
4 lock struct(s), heap size 320, 14 row lock(s), undo log entries 1
MySQL thread id 127, query id 1540 localhost 127.0.0.1 root updating
delete from gallery where gallery_name='Corporate'
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 0 page no 220 n bits 112 index `PRIMARY` of table `app_test`.`gallery` trx id 0 19757 lock_
mode X
Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000115; asc ;; 1: len 6; hex 000000004d2d; asc M-;; 2: len 7; hex 000000003a1de7; asc : ?;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000001; asc ;;
Record lock, heap no 22 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000109; asc ;; 1: len 6; hex 000000004d25; asc M%;; 2: len 7; hex 000000003a18e3; asc
: ?;; 3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000001; asc ;;
Record lock, heap no 23 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 0000010a; asc ;; 1: len 6; hex 000000004d25; asc M%;; 2: len 7; hex 000000003a1910; asc : ;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000002; asc ;;
Record lock, heap no 24 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 0000010b; asc ;; 1: len 6; hex 000000004d25; asc M%;; 2: len 7; hex 000000003a193d; asc : =;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000003; asc ;;
Record lock, heap no 36 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 0000010e; asc ;; 1: len 6; hex 000000004d26; asc M&;; 2: len 7; hex 000000003a1a7e; asc : ~;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000003; asc ;;
Record lock, heap no 37 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 0000010d; asc ;; 1: len 6; hex 000000004d26; asc M&;; 2: len 7; hex 000000003a1a51; asc : Q;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000002; asc ;;
Record lock, heap no 38 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 0000010f; asc ;; 1: len 6; hex 000000004d27; asc M';; 2: len 7; hex 000000003a1b65; asc : e;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000001; asc ;;
Record lock, heap no 39 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 0000010c; asc ;; 1: len 6; hex 000000004d26; asc M&;; 2: len 7; hex 000000003a1a24; asc : $;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000001; asc ;;
Record lock, heap no 40 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000110; asc ;; 1: len 6; hex 000000004d27; asc M';; 2: len 7; hex 000000003a1b92; asc : ?;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000002; asc ;;
Record lock, heap no 41 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000111; asc ;; 1: len 6; hex 000000004d27; asc M';; 2: len 7; hex 000000003a1bbf; asc : ?;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000003; asc ;;
Record lock, heap no 43 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000113; asc ;; 1: len 6; hex 000000004d28; asc M(;; 2: len 7; hex 000000003a1cd3; asc : ?;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000002; asc ;;
Record lock, heap no 44 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000114; asc ;; 1: len 6; hex 000000004d28; asc M(;; 2: len 7; hex 000000003a1d00; asc : ;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000003; asc ;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 221 n bits 112 index `Index_2` of table `app_test`.`gallery` trx id 0 19757 lock_
mode X locks rec but not gap waiting
Record lock, heap no 5 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 9; hex 436f72706f72617465; asc Corporate;; 1: len 4; hex 00000115; asc ;;
*** WE ROLL BACK TRANSACTION (1)
------------
TRANSACTIONS
------------
Trx id counter 0 19926
Purge done for trx's n:o < 0 19923 undo n:o < 0 0
History list length 20
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 0, not started, OS thread id 4372
MySQL thread id 221, query id 2745 localhost 127.0.0.1 root
show innodb status
---TRANSACTION 0 19925, not started, OS thread id 6080
MySQL thread id 182, query id 2741 localhost 127.0.0.1 root
--------
FILE I/O
--------
I/O thread 0 state: wait Windows aio (insert buffer thread)
I/O thread 1 state: wait Windows aio (log thread)
I/O thread 2 state: wait Windows aio (read thread)
I/O thread 3 state: wait Windows aio (write thread)
Pending normal aio reads: 0, aio writes: 0,
ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
84 OS file reads, 424 OS file writes, 297 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2,
0 inserts, 0 merged recs, 0 merges
Hash table size 419291, node heap has 1 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 0 465789
Log flushed up to 0 465789
Last checkpoint at 0 465789
0 pending log writes, 0 pending chkp writes
230 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 119727652; in additional pool allocated 1402624
Dictionary memory allocated 32888
Buffer pool size 6464
Free buffers 6385
Database pages 78
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages read 74, created 4, written 164
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
1 read views open inside InnoDB
Main thread id 6000, state: waiting for server activity
Number of rows inserted 504, updated 0, deleted 501, read 663
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
I get an error "Deadlock found when trying to get lock; try restarting transaction" when there are 2 indexes (primary key index + one more) for a table, but not when there is only primary key index.
I concluded that this is a side effect of using a second index, by reading 'show innodb status' output (included below).
So my question is, is there any solution to my problem where multiple indexes can be involved but without causing deadlocks during transaction?
Table structure:
CREATE TABLE `gallery` (
`gallery_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`gallery_name` varchar(20) NOT NULL,
`video_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`gallery_id`),
KEY `Index_2` (`gallery_name`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The error occurs consistently when multiple concurrent transactions are simulated and both indexes have been created for table. But with 'Index_2' dropped, there is no deadlock.
Each transaction involves
- one delete * from gallery where gallery_name='name';
- Multiple insert into gallery values (...);
The second index was just to improve performance for queries having 'where' conditions with string matching, but is not critical and can be removed.
I'm new to innodb status report, but what I could infer is that one thread has got lock on primary index and is waiting for lock on 'Index_2', while another thread has got lock on 'Index_2' and is waiting for lock on primary. Is this correct? What can be done to prevent this? Should I look into transaction levels (am new to them too) to solve such issues?
Thanks
Karthik
Details:
--------
MySQL version: 5.1.30-community (x86 build)
OS: Windows 7 64 bit
The application is java. Mysql Connector/J verion is 5.1.12.
Junitperf LoadTest was used to simulate 10 concurrent updates.
Relevant code:
try { ... conn.setAutoCommit(false); stmt = conn.createStatement(); stmt.addBatch(String.format(DELETE_GALLERY_QRY, gallery.getName())); for (Integer videoID : videoIDs) { stmt.addBatch(String.format(INSERT_GALLERY_ITEM_QRY, gallery.getName(), videoID)); } stmt.executeBatch(); // <== EXCEPTION THROWN HERE: java.sql.BatchUpdateException: Deadlock // found when trying to get lock; try restarting transaction conn.commit(); } catch (SQLException e) { if (conn != null) { // can be null in case of too many concurrent connections try { conn.rollback(); } catch (SQLException rollbackEx) { LOG.error("updateGallery rollback failed", rollbackEx); } } finally { if (stmt != null) { try {stmt.close();} catch (Exception ignored) {;} } if (conn != null) { try {conn.close();} catch (Exception ignored) {;} } }
show innodb status output:
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 341, signal count 273
Mutex spin waits 0, rounds 6913, OS waits 205
RW-shared spins 151, OS waits 66; RW-excl spins 78, OS waits 49
------------------------
LATEST DETECTED DEADLOCK
------------------------
110105 12:31:30
*** (1) TRANSACTION:
TRANSACTION 0 19754, ACTIVE 0 sec, OS thread id 5200 starting index read
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 320, 14 row lock(s)
MySQL thread id 125, query id 1504 localhost 127.0.0.1 root updating
delete from gallery where gallery_name='Corporate'
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 220 n bits 112 index `PRIMARY` of table `app_test`.`gallery` trx id 0 19754 lock_
mode X locks rec but not gap waiting
Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000115; asc ;; 1: len 6; hex 000000004d2d; asc M-;; 2: len 7; hex 000000003a1de7; asc : ?;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000001; asc ;;
*** (2) TRANSACTION:
TRANSACTION 0 19757, ACTIVE 0 sec, OS thread id 6080 updating or deleting, thread declared inside InnoDB 0
mysql tables in use 1, locked 1
4 lock struct(s), heap size 320, 14 row lock(s), undo log entries 1
MySQL thread id 127, query id 1540 localhost 127.0.0.1 root updating
delete from gallery where gallery_name='Corporate'
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 0 page no 220 n bits 112 index `PRIMARY` of table `app_test`.`gallery` trx id 0 19757 lock_
mode X
Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000115; asc ;; 1: len 6; hex 000000004d2d; asc M-;; 2: len 7; hex 000000003a1de7; asc : ?;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000001; asc ;;
Record lock, heap no 22 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000109; asc ;; 1: len 6; hex 000000004d25; asc M%;; 2: len 7; hex 000000003a18e3; asc
: ?;; 3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000001; asc ;;
Record lock, heap no 23 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 0000010a; asc ;; 1: len 6; hex 000000004d25; asc M%;; 2: len 7; hex 000000003a1910; asc : ;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000002; asc ;;
Record lock, heap no 24 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 0000010b; asc ;; 1: len 6; hex 000000004d25; asc M%;; 2: len 7; hex 000000003a193d; asc : =;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000003; asc ;;
Record lock, heap no 36 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 0000010e; asc ;; 1: len 6; hex 000000004d26; asc M&;; 2: len 7; hex 000000003a1a7e; asc : ~;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000003; asc ;;
Record lock, heap no 37 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 0000010d; asc ;; 1: len 6; hex 000000004d26; asc M&;; 2: len 7; hex 000000003a1a51; asc : Q;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000002; asc ;;
Record lock, heap no 38 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 0000010f; asc ;; 1: len 6; hex 000000004d27; asc M';; 2: len 7; hex 000000003a1b65; asc : e;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000001; asc ;;
Record lock, heap no 39 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 0000010c; asc ;; 1: len 6; hex 000000004d26; asc M&;; 2: len 7; hex 000000003a1a24; asc : $;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000001; asc ;;
Record lock, heap no 40 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000110; asc ;; 1: len 6; hex 000000004d27; asc M';; 2: len 7; hex 000000003a1b92; asc : ?;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000002; asc ;;
Record lock, heap no 41 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000111; asc ;; 1: len 6; hex 000000004d27; asc M';; 2: len 7; hex 000000003a1bbf; asc : ?;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000003; asc ;;
Record lock, heap no 43 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000113; asc ;; 1: len 6; hex 000000004d28; asc M(;; 2: len 7; hex 000000003a1cd3; asc : ?;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000002; asc ;;
Record lock, heap no 44 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
0: len 4; hex 00000114; asc ;; 1: len 6; hex 000000004d28; asc M(;; 2: len 7; hex 000000003a1d00; asc : ;;
3: len 9; hex 436f72706f72617465; asc Corporate;; 4: len 4; hex 00000003; asc ;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 221 n bits 112 index `Index_2` of table `app_test`.`gallery` trx id 0 19757 lock_
mode X locks rec but not gap waiting
Record lock, heap no 5 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 9; hex 436f72706f72617465; asc Corporate;; 1: len 4; hex 00000115; asc ;;
*** WE ROLL BACK TRANSACTION (1)
------------
TRANSACTIONS
------------
Trx id counter 0 19926
Purge done for trx's n:o < 0 19923 undo n:o < 0 0
History list length 20
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 0, not started, OS thread id 4372
MySQL thread id 221, query id 2745 localhost 127.0.0.1 root
show innodb status
---TRANSACTION 0 19925, not started, OS thread id 6080
MySQL thread id 182, query id 2741 localhost 127.0.0.1 root
--------
FILE I/O
--------
I/O thread 0 state: wait Windows aio (insert buffer thread)
I/O thread 1 state: wait Windows aio (log thread)
I/O thread 2 state: wait Windows aio (read thread)
I/O thread 3 state: wait Windows aio (write thread)
Pending normal aio reads: 0, aio writes: 0,
ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
84 OS file reads, 424 OS file writes, 297 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2,
0 inserts, 0 merged recs, 0 merges
Hash table size 419291, node heap has 1 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 0 465789
Log flushed up to 0 465789
Last checkpoint at 0 465789
0 pending log writes, 0 pending chkp writes
230 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 119727652; in additional pool allocated 1402624
Dictionary memory allocated 32888
Buffer pool size 6464
Free buffers 6385
Database pages 78
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages read 74, created 4, written 164
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
1 read views open inside InnoDB
Main thread id 6000, state: waiting for server activity
Number of rows inserted 504, updated 0, deleted 501, read 663
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT