Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How MySQL manage multiple queries from multiple users simultaneously?

Tags:

sql

php

mysql

Just to give you an example:

I have a PHP script that manages users votes.

When a user votes, the script makes a query to check if someone has already voted for the same ID/product. If nobody has voted, then it makes another query and insert the ID into a general ID votes table and another one to insert the data into a per user ID votes table. And this kind of behavior is repeated in other kind of scripts.

The question is, if two different users votes simultaneously its possible that the two instances of the code try to insert a new ID (or some similar type of query) that will give an error??

If yes, how I prevent this from happening?

Thanks?

Important note: I'm using MyISAM! My web hosting don't allow InnoDB.

like image 665
Jonathan Avatar asked Oct 03 '10 22:10

Jonathan


People also ask

How many simultaneous queries can MySQL handle?

By default 151 is the maximum permitted number of simultaneous client connections in MySQL 5.5. If you reach the limit of max_connections you will get the “Too many connections” error when you to try to connect to your MySQL server. This means all available connections are in use by other clients.

Can MySQL handle concurrent requests?

The same server will then be able to handle the same concurrency (5000 TPS) over 10000 connections, but adding more connections will not increase the concurrency. However, 10000 connections will require higher memory usage for (more) THDs and will result in less efficient use of hardware.

Is MySQL a multi-user database?

The MySQL software delivers a very fast, multithreaded, multi-user, and robust SQL (Structured Query Language) database server.

How multiple users handle same data change in SQL server?

Answers. You need to write SQL that prevents that. You can go fancy and create a que that syncs data or you can use concurrency that prevents record updating on records that have been modified by someone else in the same time that you were changing the same record.


2 Answers

The question is, if two different users votes simultaneously its possible that the two instances of the code try to insert a new ID (or some similar type of query) that will give an erro

Yes, you might end up with two queries doing the insert. Depending on the constraints on the table, one of them will either generate an error, or you'll end up with two rows in your database.

You could solve this, I believe, with applying some locking; e.g. if you need to add a vote to the product with id theProductId:(pseudo code)

START TRANSACTION;
//lock on the row for our product id (assumes the product really exists)
select 1 from products where id=theProductId for update;
//assume the vote exist, and increment the no.of votes
update votes set numberOfVotes = numberOfVotes + 1 where productId=theProductId ;
//if the last update didn't affect any rows, the row didn't exist
if(rowsAffected == 0) 
  insert into votes(numberOfVotes,productId) values(1,theProductId )
//insert the new vote in the per user votes
insert into user_votes(productId,userId) values(theProductId,theUserId);
COMMIT;

Some more info here

MySQL offers another solution as well, that might be applicable here, insert on duplicate

e.g. you might be able to just do:

 insert into votes(numberOfVotes,productId) values(1,theProductId ) on duplicate key 
   update numberOfVotes = numberOfVotes  + 1;

If your votes table have a unique key on the product id column, the above will do an insert if the particular theProductId doesn't exist, otherwise it will do an update, where it increments the numberOfVotes column by 1

You could probably avoid a lot of this if you created a row in the votes table at the same time you added the product to the database. That way you could be sure there's always a row for your product, and just issue an UPDATE on that row.

like image 174
nos Avatar answered Oct 17 '22 01:10

nos


The question is, if two different users votes simultaneously its possible that the two instances of the code try to insert a new ID (or some similar type of query) that will give an error??

Yes, in general this is possible. This is an example of a very common problem in concurrent systems, called a race condition.

Avoiding it can be rather tricky, but in general you need to make sure that the operations cannot interleave in the way you describe, e.g. by locking the database for a while.

There are several practical solutions to this, all with their own advantages and risks (e.g. dead locks). See the Wikipedia article for a discussion and further pointers to information.

like image 42
sleske Avatar answered Oct 17 '22 02:10

sleske