dshakcsq64956 2018-03-11 11:17
浏览 146

Laravel firstOrNew为相同数据创建重复条目

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#python#的问题:求帮写python代码
    • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?