1

I googled and searched on SO, but was not able to find an answer; maybe you could point me to some reference/docs?

This is more about understanding the way MySQL treats table contents while inserting. I have a table (Myisam) which has an auto-increment primary key 'autoid'. I am using a simple script to insert 1000s+ of records. What I am trying to do is running multiple instances of this script (you can image it similar to accessing the script from different machines at same time).

Is MySql capable of distributing the auto-increment primary keys accordingly without any further action from my side or do I have to do some sort of table locking for each machine? Maybe I have to choose InnoDb over MyIsam?

What I am trying to achieve is: irrespective of how many machines are simultaneously triggering the script, all inserts should be completed without skipping any auto-increment id or throwing errors like "Duplicate Value for...".

Thanks a lot

2
  • If you have the table, and you have the script, what prevents you from trying it and seeing for yourself instead of asking here? Commented Oct 4, 2015 at 6:43
  • I was/am not sure whether I'd be able to simulate it dealing with (and capturing potential errors) that amount of data; like running 5 instances where each instance performs 1k+ inserts. Commented Oct 4, 2015 at 6:52

1 Answer 1

3

The whole point of using a database is that can handle situations like this transactionally. So yes, this scenario works fine on every commonly used DBMS system, including MySQL.

How do you think the average forum would work with 50 users simultaneously posting replies to a topic, all from forked parallel Apache processes so possible only microseconds apart, or from multiple loadbalanced webservers?

Internally it just uses a mutex/semaphore like any other process when accessing and incrementing the shared resource (the autoincrement value of a table in this case) to mitigate the inherent race conditions.

Sign up to request clarification or add additional context in comments.

4 Comments

Thousand thanks for your reply. So it is built in, I did not know that. Is there any expected (worth being concerned about) delay while dealing with data that goes beyond 1m records?
Nothing worse than the lot of work it takes anyway. You'd best look into the various bulk insert methods though (INSERT with multiple rows and LOAD DATA command) as they're likely (read: most certainly) far more efficient than your own parallel processing implementation. Just keep it in 1 script and let MySQL do the hard work it was designed to do.
Thanks, Niels. But the current structure allows no other method than simultaneous insert-access from several machines; it has been built like that. I am sure to look into your advice regarding structure/performance improvement.
The bulk inserts are still likely to (dramatically!) improve performance from the separate machines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.