9) Locking in Mysql

You are on page 1of 15
At a glance
Powered by AI
The key takeaways are that MySQL uses different types of locking mechanisms like internal locking (table level and row level locking) and external locking to control access to data concurrently. Deadlocks can also occur when multiple transactions are waiting for locks held by each other.

The different types of locking in MySQL are internal locking (table level and row level locking), external locking, and deadlocking.

Table level locking locks entire tables and is faster for selects but prevents concurrent updates. Row level locking locks individual rows granularly and allows for transactions but uses more resources.

MySQL LOCKING

Ajenda
Locking in MySQL
Internal Locking
Table level Locking
Row level Locking
External Locking

Dead Lock

About Us
Karthik.P.R, Senior MySQL DBA,
Bharath Kumar.P, MySQL DBA

Blog : remotemysqldba.blogspot.in

Locking In MySQL

It places a lock on data on behalf of one client to restrict access by other clients to the

data until the lock has been released.

The effect of locking is to serialize access to


data so that when multiple clients want to perform
conflicting operations, each must wait its turn.

The locks are needed as the data is read / write by


multiple sessions are users.

To reduce the contention of queries waiting for a single resource locking is needed.

To overcome the contention . MySQL has two type of locks namely


1) Internal Locking.
2) External Locking.

Remotemysqldba.blogspot.in

Internal Locking
Internal Locking:
It is performed inside the server itself to avoid the contention by multiple
threads. It does not involve any programs.
Internal Locking Types:
The internal locking is of two types namely .
a) Table level locking
b) Row level locking
The internal locking is maintained within the mysql itself. It helps in managing
the contention between the connections.

Remotemysqldba.blogspot.in

Table level Locking


MyISAM , memory and Merge engine uses the table level locking.
The table level locking is dead lock free .
Web application that needs more selects with few inserts and deletes to a few
specific tables.
MyISAM is tuned for this type of web applications.
Advantages
- It good for making many selects .
- Uses less resource while comparing to Row-level-locking
- Dead lock free.

Remotemysqldba.blogspot.in

Table level Locking

Disadvantages

- Cannot be used in transaction .


- It causes the delay in the update statements if the tables are huge

Remotemysqldba.blogspot.in

Table level Locking

Read locks and write locks occurs in mysql.


When a read lock is acquired on the table , other connections can also

make the select on the table.


If write lock is acquired on the table. Only one connection can make the
Write operation ( update, delete ) on the table.
The update queries have the high priority while comparing to the select
queries.

Remotemysqldba.blogspot.in

Table level Locking


The locking contention can be monitored using the status variables .

table_locks_immediate
table_locks_waited
Table_locks_immediate : The number of locks that are granted immediately on
the request for it.
Table_locks_waited : The number of locks that are granted after waiting .

Remotemysqldba.blogspot.in

Row level locking


The row level locking is the characteristic feature of the InnoDB/XtraDB
storage engine.
It locks only the selected range of rows.
The locking of the rows depend on the where clause of the query.
Advantages :
-Supports the transaction.
-A few conflict occurs.
-Possible to lock single row for a long time.

Remotemysqldba.blogspot.in

Row level locking

Disadvantages :
-Consumes more system resource .
-Slower when a larger part of the table is locked.
-Dead locks occurs.

Remotemysqldba.blogspot.in

External Locking
External locking is the use of file system locking to manage contention for
MyISAM database tables by multiple processes.

External locking is used in situations where a single process such as the


MySQL server cannot be assumed to be the only process that requires access
to tables.
If you run multiple servers that use the same database directory (not
recommended), each server must have external locking enabled.
External locking affects server performance because the server must
sometimes wait for other processes before it can access tables.

External locking is unnecessary if you run a single server to access a given


data directory.

Remotemysqldba.blogspot.in

Dead Lock
Dead is a situation in which two or more competing actions are each
waiting for the other to finish, and thus neither ever does.
A condition that occurs when two processes are each waiting for the
other to complete before proceeding. The result is that both
processes hang. Deadlocks occur most commonly
in multitaskingand client/server environments. Ideally, the programs
that are deadlocked.

Two transaction waiting for a rows which are locked by each other .

Remotemysqldba.blogspot.in

Dead Locking

Remotemysqldba.blogspot.in

THANK YOU

Remotemysqldba.blogspot.in

You might also like