Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Locking in MySQL and when would you use it? [closed]

Tags:

mysql

rdbms

What is Locking in MySQL (or any RDBMS) and when would you use it? A Layman explanation with a Example would be great!

like image 363
Imran Avatar asked Jul 28 '10 13:07

Imran


1 Answers

We have a joint bank account with a balance of $200

I go to the ATM and put my card into the machine, the machine checks that I have a balance of $200

Meanwhile, you go into the bank and ask for $50, the teller brings up your account and confirms that you have the money.

I request a withdrawal of $200, the machine counts my money gives me $200 and sets my balance at $0

The teller counts your money and gives you the $50, the system then updates the balance on the account as $150 ($200 - $50 withdrawl).

So now we have $250 cash and $150 left in the account. $200 profit.

The database should have used locks to prevent both transactions occuring at the same time.

The problem is if you handle every transaction in that way then we would lose concurrency and performance would suffer, so there are different transaction isolation levels that are used depending on the scenario, for instance you might not care that someone can modify data that has been read in a transaction.

http://en.wikipedia.org/wiki/Isolation_%28database_systems%29

You should learn these and understand the scenarios where they are applicable.

like image 125
Chris Diver Avatar answered Oct 03 '22 22:10

Chris Diver