We have a database that has more than 70 tables with more than 10k of users. One of the tables in the database is to keep recording of user's latest actions/changes in order to keep them pending until the gameweek finishes (Football matches), let's call this table 'Substitute'.
However, every week when we advance the system (apply changes) at the end of gameweek, we check to see if there's any duplicated data stored regarding user's changes (we have noticed that a while ago), and remove them manually from the database.
The rows have primary keys (which is not duplicated) only the data is duplicated. It's like the insert query is being fired twice. (not sure about this or why)
Example: Let's say the user has a team of football players (11 on the field and 4 substitutes) when the user chooses player and clicks substitute, that player (on the field) will be exchanged with the one who's in on the bench (Substitute). This process will happen immediately without the need of user to save (JavaScript). But, it not be saved in the team details, instead, it will be saved in the Substitute table that keep track of changes.
User wants to remove player 1 and enter player 12 (1->12). For the first step the system will record
id Team from_player to_player
0 x 1 12
And if the user do another substitution
id Team from_player to_player
0 x 1 12
1 x 2 13
And when the user substitute back the player (12->1) that record will be deleted(1->12), since the user substituted back the player and the later subs cancels the first.
id Team from_player to_player
1 x 2 13
But SOMETIMES it record that row more than once
id Team from_player to_player
0 x 1 12
1 x 1 12
2 x 1 12
3 x 2 13
Or the duplicate could be in between
id Team from_player to_player
0 x 1 2
1 x 2 13
2 x 1 2
3 x 1 2
N.B This happens only to around 10-100 users every week, even though there are more than 10k users registered in our database.
So what do you think is causing the problem?
I don't want to use INSERT IGNORE, I want to find the root of the problem and stop it.
So basically, my questions are:
1- Is it likely to be a server or client side problem?
2- Could ajax code be called/fired twice under certain circumstances?
3- Could be there an error in the sql server where it executes the same query twice?
Really appreciate your help.
UPDATE:
4- If the problem is with the client side, how i inspect it? Any suggested way?
For those who ask, what does happen when the user subs back the same player. Let's say this is part of the original players stetting.
| Original Status | After First Subs|After Second Subs|
| | | |
On the field | p1 | P12 | P1 |
-----------------------|-----------------|-----------------|-----------------|
| | | |
Subs(at the bench) | p12 | P1 | P12 |
| | | |
Original Status p1 on the field, p12 on the bench (substitute) Record of actions: s1- P1->P12 Status= P12 on the field. P1 on the bench
s2- P12->P1 Status= P1 back on the field. P12 back to the bench.
Note that s2 wont be recorded, but, s1 will be deleted. it's like A * -1, and then -A * -1 again. The will cancel each other.