This section discusses internal locking; that is, locking performed within the MySQL server itself to manage contention for table contents by multiple threads. This type of locking is internal because it is performed entirely by the server and involves no other programs. External locking occurs when the server and other programs lock table files to coordinate among themselves which program can access the tables at which time. See Section 7.3.4, “External Locking”.
MySQL uses table-level locking for MyISAM and
MEMORY tables, and row-level locking for
InnoDB tables.
In many cases, you can make an educated guess about which locking type is best for an application, but generally it is difficult to say that a given lock type is better than another. Everything depends on the application and different parts of an application may require different lock types.
To decide whether you want to use a storage engine with
row-level locking, you should look at what your application does
and what mix of select and update statements it uses. For
example, most Web applications perform many selects, relatively
few deletes, updates based mainly on key values, and inserts
into a few specific tables. The base MySQL
MyISAM setup is very well tuned for this.
MySQL Enterprise. The MySQL Enterprise Monitor provides expert advice on when to use table-level locking and when to use row-level locking. To subscribe see http://www.mysql.com/products/enterprise/advisors.html.
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.
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 means that if you have many updates for a
table, SELECT statements wait until there are
no more updates.
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 re-enabled automatically when all holes have been filled
with new data.. This behavior is altered by the
concurrent_insert system variable. See
Section 7.3.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.
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
Definitely much 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
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:
UPDATEtbl_nameSETcolumn=valueWHEREunique_key_col=key_value; DELETE FROMtbl_nameWHEREunique_key_col=key_value;
SELECT combined with concurrent
INSERT statements, and very few
UPDATE or DELETE
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() and
RELEASE_LOCK() in MySQL.
These are advisory locks, so they work only with
applications that cooperate with each other. See
Section 11.11.4, “Miscellaneous Functions”.

User Comments
This entire article is so vague and anecdotal.
"Table locks are superior to page-level or row-level locks in the following cases:
*Most statements for the table are reads."
Really? Any application at all where "most" (>51%) statements are reads? I find that hard to believe.
John, the description of where table-level locks are superior was crystal clear. It was not vague or anecdotal, rather, it was deliberately general and there's a difference.
This is a fundamental and simple concept in concurrency. If you do not change a data set there is no reason to lock the data set. If you change the data you must serialize access using, for example, locks. You cannot have two ore more people changing the same item at the same time and you do not want anybody reading changes half way through the change.
The granularity of the lock is a performance issue. You could lock access to the entire database. You could lock access to a table. You could lock access to a row. You could lock access to each field. The finer the granularity the less likely you will have overlapping changes that have to be queued up. The increased granularity has it's own cost and complexity, so there is a trade-off. Table locking is great for low frequency changes.
As far as applications mostly reading data, what's not crashingly obvious about that? Deletes and updates are relatively rare with respect to reads. An application that only changed data would still be 50% reads. I'd venture to say that 99% of all applications read 99% of the time, at the risk of being anecdotal.
Well, consider the following use case :
- rather large table, a bit larger than RAM
- lots of concurrent selects
- concurrent updates
If the hot set where most of the SELECTs happen fits in RAM, you'll have a very high SELECT throughput, since everything is cached.
Now, when you UPDATE a row, you think "well, it's only a row, so I can use MyISAM, it won't stay locked for long".
WRONG.
If, by any chance, you update an old row, which isn't in the cache, the table will stay locked for the time of a few disk seeks... a few indexes also have to be updated... say, 5-10 head seeks, maybe ? 50 ms ? During that time, you could have done about 500 or 1000 simple SELECTs...
Another example, I call this the select/update sandwich.
You have a quad dual core xeon CPU. Suppose (for an instant) that you are dumb and use MyISAM in the following configuration, which corresponds to the "users" table on a website I had to fix :
- table fits in RAM
- heavy small SELECT load (get the current user at each page)
- respectable UPDATE load (update last activity time every 5 minutes for each user)
- a slow frequent query (about 100 milliseconds) to get some statistics to display on the front page.
Now, since all those come in random order all the time, and SELECTs block UPDATEs, the query queue ends up like a sandwich :
- UPDATE
- SELECT
- UPDATE
- SELECT
- UPDATE
- SELECT
- UPDATE
- SELECT
- etc.
And since UPDATEs are non-concurrent, everything gets serialized, and only ONE cpu core is used.
If you have enough of the slow 50-100 ms SELECT coming, throughput drops to 10-20 queries per second since it sandwiches between the UPDATEs and locks everything.
So, use your brain (and don't use MyISAM)
For info... I once did an analysis of a system handling requests for government grants - ie mostly oltp with standard reports. It had a 5:1 read:write ratio in terms of requests to permanent tables. From 30 years relational database experience I doubt if many systems had a higher proportion of writes. However one factor to consider is critical times - thats what you need to design for.
Add your own comment.