我的查询在循环内花费的时间太长

时间:2020-10-13 08:33:39

标签: php sql laravel eloquent

我有一个雄辩的lumen应用程序,我在循环内有一个查询,执行时间太长。这是代码:

$DtPayments = Payments::whereIn('payment_status', [PYG_STATUS_NEW, PYG_STATUS_PAYED, PYG_STATUS_RESUBMIT, PYG_STATUS_SENT])
    ->whereNull('payment_care_status')
    ->whereNotNull('payment_voucherno')
    ->select('payment_id', 'payment_voucherno', 'payment_installment', 'payment_amount')
    ->orderBy('payment_id', 'ASC')
    ->chunk(200, function ($DtPayments) use ($Logger, $total_data, $total_success, $total_fail){
        foreach($DtPayments as $payment){
            $Logger->write_log('info', 'Check payment ID ' . $payment->payment_id);

            $statementGetInfopSLVoucher = "Select pSL.Doc_No, Max(pSL.Voucher) as Voucher, "
                    . "Sum(dbo.fn_SL_Payment_Nominal(pSL.Nominal_OC, pVoucher.SL_Cat, pVoucher.DebtorF, DebtorF)) as Nominal_OC "
                    . "from pSL, pVoucher Where pSL.PaymentF=1 and pVoucher.Voucher=pSL.Voucher "
                    . "and psl.Doc_No in (SELECT VOUCHER FROM AdmLink WHERE DOCNO LIKE '{$payment->payment_source_number}%' AND TYPE IN ('DI','IC')) Group By pSL.Doc_No";

            try {
                $getInfoPembayaran = DB::connection('default')->select(DB::connection('default')->raw($statementGetInfopSLVoucher));

            ...
            }

我尝试将$ DtPayments更改为此:

$DtPayments = Payments::whereIn('payment_id', [68670,68671,68672,69324,69329,69330,69332,69333,69335,69334])
    ->get();

内部循环查询快速运行。所以我认为问题在于$ DtPayments。是否有最佳做法来调整$ DtPayments?

谢谢

0 个答案:

没有答案