Transaction, set isolation level - mysql

I have been reading about setting the isolation levels of transactions. But couldn't really find a straight answer to the simple question:
Do I first set the isolation level and then start the transaction or vice versa.
START TRANSACTION
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
So which command comes first? Does it even matter?
PS: Is there any disadvantage about using transactions/isolation levels for PHP/MySQL sites?

You should set the transaction level first:
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
START TRANSACTION;
as you can't change the isolation level from within a transaction.
In fact, attempting to is the only way to tell if you're inside a transaction with certain versions of MySQL.

Related

Which isolation levels use MVCC in MySQL?

I read Transaction Isolation Levels in MySQL documentation. Then, only READ COMMITTED and REPEATABLE READ talk about snapshot as shown below:
READ COMMITTED
Each consistent read, even within the same transaction, sets and reads
its own fresh snapshot. ...
REPEATABLE READ
This is the default isolation level for InnoDB. Consistent reads
within the same transaction read the snapshot established by the first
read. ...
snapshot
A representation of data at a particular time, which remains
the same even as changes are committed by other transactions. Used by
certain isolation levels to allow consistent reads.
So, do only READ COMMITTED and REPEATABLE READ use MVCC(Multiversion concurrency control)?
What about READ UNCOMMITTED and SERIALIZABLE?
All transaction isolation levels in InnoDB use MVCC.

Any design reason why Mysql doesn't support isolation level pre transaction?

From document,
A user can change the isolation level for a single session or for all
subsequent connections with the SET TRANSACTION statement. To set the
server's default isolation level for all connections, use the
--transaction-isolation option on the command line or in an option file. For detailed information about isolation levels and
level-setting syntax, see Section 13.3.7, “SET TRANSACTION Statement”.
But it cannot set the isolation level for a specific transaction, any specific reason?

Is it possible to set TRANSACTION ISOLATION LEVEL READ UNCOMMITTED for particular user in MYSQL

I have many users for MYSQL database some has Read access only and some has Read and Write access.
Read access users are Report Users which usually create queries which results in high volume data and puts lock on tables used hence system users who are using system at that time get lock and wait period are very high
Is it possible to set TRANSACTION ISOLATION LEVEL READ UNCOMMITTED for particular user in MYSQL
Or is there any way out of this
No, you cannot set the ISOLATION LEVEL for a particular user.
As far as what I can think of you can change the Isolation level of MYSQL which is REPEATABLE READ which means locks will be placed for each operation. So you can set the global ISOLATION LEVEL like this:
SET GLOBAL TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;
You can also go through this article: mysql with nolock

How to set transaction isolation level none in mysql?

I want to test the transaction isolation issues : lost update. How can I do that in mysql console. I know the following one
SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
Using read uncommitted i can demo it does not allows lost update but allows dirty read. I want to demo the lost update how do I demo that practically in two different mysql consoles.

Difference between set autocommit=0 and set session transaction isolation level REPEATABLE READ;start transaction;?

It is mentioned here that default isolation level for any transaction in innodb is repeatable read.
In the same link above it is also mention that if I do set autocommit=0 then session always have transaction open.
Now My undersatnding is if I do
set session transaction isolation level REPEATABLE READ;
start transaction;
DML(insert, update, delete) queries
commit;
and
set autocommit=0;
DML(insert, update, delete) queries
commit;
Both should do the same, should run in repeatable read isolation level. I need to do commit or rollback in the end.
But as I use them for lot of queries the first one take lot of time and second one takes very little time. So clearly they aren't same. What I am missing? what is the default isolation level of transaction while auto commit is set to 0? Thanks in advance.
EDIT:I got the answer of what is default isolation level if I do set autocommit=0;(Thanks to kostja) It can be seen from show variables like "%isolation"; But I am yet to find out why first one is slower.
Setting autocommit in the session doesn't change it globally.
Sure it's not the case?
Answering your question, transaction isolation level doesn't depend on autocommit.