Mysql – theISAM data loss at DELETE / INSERT

deleteinsertmyisamMySQL

I have a linked table id_group | id_user. Every time I add a one or more users to a group I DELETE all the links and INSERT the users again including the new one(s). This is how the framework of the application works.

From time to time some groups lose all the users they had, except the new ones, which are inserted.

The primary index of the table is the pair id_group - id_user.

I cannot reproduce this through a query, but the client has complained multiple times and there's an issue no doubt.

I could explain this if the table was innoDB with the DELETE locking the index and the INSERT not being able to add that pair again, however this is a myISAM table and myISAM locks the entire table during DELETE statements.

Best Answer

Is it possible you've got two processes attempting to do this at almost the same time? If this is a web application, could you have a site user who is double-click happy?

Process #1 "examine existing users"
Process #1 "remove all users"
Process #2 "examine existing users" (no users would found)
Process #1 "insert new users and put existing users back"
Process #2 "remove all users"
Process #2 "insert new users and put existing users back (but no existing users were found)"