I have this specific code from my application:
if ($request->filled('jawaban')) {
$jawaban = JawabanUserM::where('user_id', $user->id)
->where('paket_id', $kuis['paket_id'])->firstOrNew(['soal_id'=>$kuis['curr_id']]);
if($jawaban->jawaban_id != $request->jawaban) {
$jawaban->user_id = $user->id;
$jawaban->paket_id = $kuis['paket_id'];
$jawaban->soal_id = $kuis['curr_id'];
$jawaban->jawaban_id = $request->jawaban;
$jawaban->save();
}
}
it basicly insert answer from user to table JawabanUserM
I run this application on deployment. using laravel 5.5, mysql 5.5.58, nginx 1.12.2, php-fpm 7.0.27.
with 800-1000++ average user at a time. using InnoDB engine.
when i check around i found some duplicate records. i check using this sql :
SELECT
id, soal_id, user_id, jawaban_id, paket_id, COUNT(*)
FROM
jawabanuserm
GROUP BY
soal_id, user_id, jawaban_id, paket_id
HAVING
COUNT(*) > 1
ORDER BY COUNT(*) DESC
the result was :
id | soal_id | user_id | jawaban_id | paket_id | COUNT(*)
4702 | 117 | 100 | 401 | 6 | 6
4694 | 111 | 100 | 401 | 6 | 5
15080 | 110 | 1300 | 1077 | 7 | 5
8097 | 186 | 435 | 686 | 6 | 4
4679 | 97 | 100 | 401 | 6 | 4
....
Any advice to workaround this problem and why this is happening?
at first i strongly believe this is caused by bad communicating between process. i have ask to my colleague they suggest using procedure, but it will impact the performance i think.