This section discusses internal locking; that is, locking performed within the MySQL server to manage contention for table contents by multiple sessions.
MySQL uses row-level locking for InnoDB
tables, and table-level locking for MyISAM
,
MEMORY
, and MERGE
tables.
Which lock type works better for your application depends on the application and its workload, especially whether the data is modified frequently and how many concurrent sessions need to read or write the same tables. Different parts of an application may require different lock types.
To decide whether you want to use a storage engine with
row-level locking, look at what your application does and what
mix of select and update statements it uses. For example, the
InnoDB
storage engine is targeted towards a
wide variety of application workloads, especially those with
heavy write activity or high concurrent usage. The
MyISAM
storage engine is targeted towards Web
applications that perform many selects, relatively few deletes,
updates based mainly on key values, and inserts into a few
specific tables.
Considerations for Row Locking
Advantages of row-level locking:
Fewer lock conflicts when different sessions access different rows.
Fewer changes for rollbacks.
Possible to lock a single row for a long time.
Disadvantages of row-level locking:
Requires more memory than table-level locks.
Slower than table-level locks when used on a large part of the table because you must acquire many more locks.
Slower than other locks if you often do
GROUP BY
operations on a large part of the data or if you must scan the entire table frequently.
Considerations for Table Locking
Table locking in MySQL is deadlock-free for storage engines that use table-level locking. Deadlock avoidance is managed by always requesting all needed locks at once at the beginning of a query and always locking the tables in the same order.
MySQL grants table write locks as follows:
If there are no locks on the table, put a write lock on it.
Otherwise, put the lock request in the write lock queue.
MySQL grants table read locks as follows:
If there are no write locks on the table, put a read lock on it.
Otherwise, put the lock request in the read lock queue.
Table updates are given higher priority than table retrievals.
Therefore, when a lock is released, the lock is made available
to the requests in the write lock queue and then to the requests
in the read lock queue. This ensures that updates to a table are
not “starved” even if there is heavy
SELECT
activity for the table.
However, if you have many updates for a table,
SELECT
statements wait until
there are no more updates.
For information on altering the priority of reads and writes, see Section 7.10.2, “Table Locking Issues”.
You can analyze the table lock contention on your system by
checking the
Table_locks_immediate
and
Table_locks_waited
status
variables, which indicate the number of times that requests for
table locks could be granted immediately and the number that had
to wait, respectively:
mysql> SHOW STATUS LIKE 'Table%';
+-----------------------+---------+
| Variable_name | Value |
+-----------------------+---------+
| Table_locks_immediate | 1151552 |
| Table_locks_waited | 15324 |
+-----------------------+---------+
The MyISAM
storage engine supports concurrent
inserts to reduce contention between readers and writers for a
given table: If a MyISAM
table has no free
blocks in the middle of the data file, rows are always inserted
at the end of the data file. In this case, you can freely mix
concurrent INSERT
and
SELECT
statements for a
MyISAM
table without locks. That is, you can
insert rows into a MyISAM
table at the same
time other clients are reading from it. Holes can result from
rows having been deleted from or updated in the middle of the
table. If there are holes, concurrent inserts are disabled but
are enabled again automatically when all holes have been filled
with new data.. This behavior is altered by the
concurrent_insert
system
variable. See Section 7.10.3, “Concurrent Inserts”.
If you acquire a table lock explicitly with
LOCK TABLES
, you can request a
READ LOCAL
lock rather than a
READ
lock to enable other sessions to perform
concurrent inserts while you have the table locked.
To perform many INSERT
and
SELECT
operations on a table
real_table
when concurrent inserts are not
possible, you can insert rows into a temporary table
temp_table
and update the real table with the
rows from the temporary table periodically. This can be done
with the following code:
mysql>LOCK TABLES real_table WRITE, temp_table WRITE;
mysql>INSERT INTO real_table SELECT * FROM temp_table;
mysql>DELETE FROM temp_table;
mysql>UNLOCK TABLES;
InnoDB
uses row locks. Deadlocks are possible
for InnoDB
because it automatically acquires
locks during the processing of SQL statements, not at the start
of the transaction.
Choosing the Type of Locking
Generally, table locks are superior to row-level locks in the following cases:
Most statements for the table are reads.
Statements for the table are a mix of reads and writes, where writes are updates or deletes for a single row that can be fetched with one key read:
UPDATE
tbl_name
SETcolumn
=value
WHEREunique_key_col
=key_value
; DELETE FROMtbl_name
WHEREunique_key_col
=key_value
;SELECT
combined with concurrentINSERT
statements, and very fewUPDATE
orDELETE
statements.Many scans or
GROUP BY
operations on the entire table without any writers.
With higher-level locks, you can more easily tune applications by supporting locks of different types, because the lock overhead is less than for row-level locks.
Options other than row-level locking:
Versioning (such as that used in MySQL for concurrent inserts) where it is possible to have one writer at the same time as many readers. This means that the database or table supports different views for the data depending on when access begins. Other common terms for this are “time travel,” “copy on write,” or “copy on demand.”
Copy on demand is in many cases superior to row-level locking. However, in the worst case, it can use much more memory than using normal locks.
Instead of using row-level locks, you can employ application-level locks, such as those provided by
GET_LOCK()
andRELEASE_LOCK()
in MySQL. These are advisory locks, so they work only with applications that cooperate with each other. See Section 11.15, “Miscellaneous Functions”.