InnoDB
Tables
InnoDB
InnoDB
InnoDB
Monitor
Deadlocks are a classic problem in transactional databases, but they are not dangerous, unless they are so frequent that you cannot run certain transactions at all. Normally you have to write your applications so that they are always prepared to re-issue a transaction if it gets rolled back because of a deadlock.
InnoDB
uses automatic row level locking. You can get deadlocks
even in the case of transactions which just insert or delete a
single row. That is because these operations are not really 'atomic':
they automatically set locks on the (possibly several) index
records of the row inserted/deleted.
You can cope with deadlocks and reduce the number of them with the following tricks:
SHOW INNODB STATUS
in MySQL versions >= 3.23.52 and >= 4.0.3
to determine the cause of the latest deadlock. That can help you to tune
your application to avoid deadlocks.
SELECT ... FOR UPDATE
or ... LOCK IN SHARE MODE
, try using a lower isolation
level READ COMMITTED
.
EXPLAIN SELECT
to determine that MySQL picks
appropriate indexes for your queries.
SELECT
to return data
from an old snapshot, do not add the clause FOR UPDATE
or LOCK IN SHARE MODE
to it. Using READ COMMITTED
isolation level is good here, because each consistent read
within the same transaction reads from its own fresh snapshot.
LOCK TABLES t1 WRITE, t2 READ, ... ;
[do something with tables t1 and t2 here]; UNLOCK TABLES
.
Table level locks make you transactions to queue nicely,
and deadlocks are avoided. Note that LOCK TABLES
implicitly starts a transaction, just like the command BEGIN
,
and UNLOCK TABLES
implicitly ends the transaction in a COMMIT
.
InnoDB
instant deadlock detection algorithm works,
because the serializing lock is a row level lock.
In MySQL table level locks we have to resort to the timeout method to
resolve a deadlock.