MySQL - Does the READ UNCOMMITED isolation level use locks? - mysql

So I am trying to understand more about the isolation levels and I read that the READ UNCOMMITED isolation level allows dirty readings which may lead to non-consistent readings. I also read that the
SELECT statements are performed in a nonlocking fashion
So my question is, does this type of isolation uses locks for other statements? For example, if I use the INSERT INTO statement does it acquire locks?
Thanks in advance!

Yes. Locks are still needed to ensure data transactions are atomic.
Also locks ensure that elements like auto_increments values only exist once.

Related

MySQL transactions in a stored procedure

I have a stored procedure which collects data from several sources, transforms and in the end deletes and then dumps the data in TableA
While this stored procedure is running, other users often perform a select query on TableA resulting in a deadlock, causing the stored procedure to fail.
My stored procedure does use SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; at the beginning however it does not seem to work or I am using it wrongly.
Without using READ/WRITE LOCKS is there a way I can ensure that this does not happen?
MySQL version: 5.7.23-enterprise-commercial-advanced-log
I have a number of comments:
I assume you're using SELECT ... FOR UPDATE because without the optional locking clause, SELECT does not acquire row locks, so should not cause a deadlock.
Other read queries without the FOR UPDATE clause don't need row locks. They cannot be contributing to the deadlocks.
To prevent deadlocks, you need to use an atomic lock. Consider LOCK TABLES.
Changing the isolation level has no effect on the currently running transaction. If you already have a transaction in progress, you cannot change its isolation level on the fly. The change to the isolation level only applies to transactions you begin subsequently.
The READ UNCOMMITTED isolation level does not eliminate the need for queries to acquire locks, if they need locks in other isolation levels.
In 20+ years of using MySQL, I've never found a legitimate use for READ UNCOMMITTED. I avoid it.

Does using higher isolation level decreases the probability of having deadlocks?

So I am trying to learn MySQL and I came across the isolation levels (SERIALIZED, REPEATABLE READ, READ COMMITED, READ UNCOMMITED)
I believe my question is quite simple, but I did not find any information in the web so here it goes:
If I change the default REPEATABLE READ to SERIALIZED or even from READ UNCOMMITED to another higher level of isolation does I have less probabilities of having deadlock problems?
Thanks in advance!
Actually, deadlocks between SELECT operations on the one hand and INSERT or UPDATE on the other hand will be less likely if you use the more permissive READ UNCOMMITTED isolation level for your SELECT operations.
If it's OK for your SELECT operations not to get the results of concurrent changes to your tables, use that.
The possibility of deadlocks is not affected by isolation level. Isolation level changes the behavior of read operations, but deadlock occurs due to write operations. However, isolation level sets fewer locks, hence it can help you to avoid certain lock types (e.g. gap locking).
These tips to avoid dead_lock are very helpful https://www.percona.com/community-blog/2018/09/24/minimize-mysql-deadlocks-3-steps/

How Pessimistic lock works in database,does Isolation level has to do any thing with it?

I was reading about database locking(pessimistic,optimistic) mechanism,
session 1:
t1: open transaction:
t2: sleep(3 sec)
t5: updte user set name='x' where id =1
session 2:
t2:update user set name='y' where id=1
my doubts are:
1. What will happen at t5
2. does It has to do any thing with Isolation level?if yes what will be the behavior in different isolation level.
3. Does database(mysql,oracle) only do pessimistic locking?
Let me answer your questions in a reverse order bacause this way I do not have to repeat certain parts.
Since optimistic locking means that the records read in a transaction are not locked, optimistic locks cannot be implemented. You should not really use the term optimistic lock, use optimistic concurrency control instead. The pessimistic locking strategy is the one that involves database level locks, which are implemented by all rdbms that use transactions - including mysql with innodb.
Mysql does not have any database level support for optimistic concurrency control. This does not mean that other rdbms do not support OCC either. You need to check out their manuals.
Isolation levels do not affect the outcome of the scenario described in the question, since there is no select there, only 2 atomic updates and the field referenced in the where clause is not updated.
Isolation levels mainly influence how data is read by transactions, not how they can update it.
The outcome of the scenario described in the question depends on which session issues the update first and how long that transaction is open. Whichever session executes the update first will make the change and sets an exclusive lock on the index record. The other transaction will not be able to execute the update until the first transaction completes. If the first transaction runs for a long time, then the other one may time out while waiting for the lock to be released.

Transaction vs locking in mysql

I have some confusing regarding transaction and locking in MySQL.
What is difference between transaction and locking in MySQL and how it related to each other?
Is transaction related to DML (INSERT, UPDATE and DELETE) only or it also related to SELECT query?
Is transaction cover the Truncate?
for example:
START TRANSACTION;
SELECT * from XYX;
UPDATE abc SET summary=788 WHERE type=1;
TRUNCATE TABLE pqr;
INSERT INTO ABL VALUE('OK');
COMMIT;
It's requires a large explanation to full cover your question.
In short a transaction is an "atomic operation". If it's committed all inside it is committed, if it's rolled back all inside it is rolled back.
Locks are a mechanism to avoid dirty and ghost reads (and two process to make updates at the same time, messing with one another) in parallel/concurrent environments.
In a general saying transaction levels defines locks strategies.
Almost everything is contained in a transaction, including the select and truncate.
I suggest you to hit the books to learn about transaction levels, locks (strategies, granularity, performance, deadlocks, starvation, glutton philosophers...)
What is transaction?
A transaction comprises a unit of work performed within a database
management system (or similar system) against a database, and treated
in a coherent and reliable way independent of other transactions.
Also, there is a documentation on MySQL site
What is database lock?
A lock, as a read lock or write lock, is used when multiple users need
to access a database concurrently.
So, it's completely different things, you can't 'compare' them.

Is it possible to issue a select query in mysql without taking any read locks?

It seems that mysql select content (as opposed to e.g. count) queries always take at least a table read lock on myisam tables and a row read lock on innodb tables. Is there a way to issue a select content query in mysql (I could change table type if that's required) without having it to grab any locks? I don't mind if the data returned is inconsistent since I will use it for a search index.
With InnoDB you achieve this by setting the transaction isolation level to: READ UNCOMMITTED.
In this isolation level:
SELECT statements are performed in a
nonlocking fashion, but a possible
earlier version of a row might be
used. Thus, using this isolation
level, such reads are not consistent.
This is also called a “dirty read.”
Otherwise, this isolation level works
like READ COMMITTED.
You can either change the default transaction isolation level from the MySQL option file, or else it can be enabled and disabled for a single session:
SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT * FROM table_name;
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Further Reading: MySQL Documentation: Set Transaction
in the absence of LOCK TABLES, myisam should be equivalent to read uncommitted mode, but it doesn't actually support any transaction types...
innodb runs in "consistent read" mode (at "repeatable read" isolation level) by default, which the docs suggest won't lock:
If the transaction isolation level is
REPEATABLE READ (the default level),
all consistent reads within the same
transaction read the snapshot
established by the first such read in
that transaction
...
Consistent read is the default mode in
which InnoDB processes SELECT
statements in READ COMMITTED and
REPEATABLE READ isolation levels. A
consistent read does not set any locks
on the tables it accesses, and
therefore other sessions are free to
modify those tables at the same time a
consistent read is being performed on
the table.
...
InnoDB uses a consistent read for
select in clauses like INSERT INTO ...
SELECT, UPDATE ... (SELECT), and
CREATE TABLE ... SELECT that do not
specify FOR UPDATE or LOCK IN SHARE
MODE if the
innodb_locks_unsafe_for_binlog option
is set and the isolation level of the
transaction is not set to
SERIALIZABLE. Thus, no locks are set
on rows read from the selected table.
http://dev.mysql.com/doc/refman/5.0/en/innodb-consistent-read.html