Laravel 4.2 - 存储在数据库中对于大量数据来说非常慢

时间:2017-08-16 13:18:53

标签: php mysql laravel optimization large-data

我正在构建一个每月处理数千套公寓的应用程序。 这意味着每个公寓每月在不同的表格中进行多次插入,更新和选择。

我使用了内置的laravel / eloquent语法,在原始sql中没有任何内容,因为我不太熟悉它。

我有一个按钮,可以为所有公寓生成一个月的信息。我不是很高兴不得不把所有这些都放在一个动作中但是我需要它。

现在,每次单击按钮并生成信息时,每个月数据库填充时都会越来越难以处理。考虑到这种情况,我认为是正常的。我可以就如何优化这个提出建议吗?我必须等待30秒 - 1分钟才能完成......这不行......

这就是我的控制器基本上运行的方式(这是主要的存储函数,它调用了其他几个函数):

// define month
$month = '01-04-2017';

// identify all payment categories (like water, electricity, maintenance etc)
// all these must be treated separately as they are calculated in very different ways
$payment_categories = PaymentCategories::get();
$payments_array = [];
foreach($payment_categories as $payment_category){
    $total = Transactions::where('month', $month)->where('payment_category_id', $payment_category->id)->sum('amount');
    $payments_array[$payment_category->id] = $total;
}

$apartments = Apartments::get();
foreach($apartaments as $apartment){

    $this->storeMainData($apartment);

    // each of these categories must be treates separately as they are calculated in very different ways
    foreach($payment_categories as $payment_category){
        $total_amount = $payments_array[$payment_category->id];
        if($payment_category->id == '1'){
            $this->storePaymentCategory1($apartment, $payment_category, $month, $total_amount);
        }

        // there are aprox. 10 categories of payments which dont have a common pattern so they must be treated separately
        // these functions below work similarly to storePaymentCategory1()
        if($payment_category->id == '2'){
            $this->storePaymentCategory2($apartment, $payment_category, $month, $total_amount);
        }
        if($payment_category->id == '3'){
            $this->storePaymentCategory3($apartment, $payment_category, $month, $total_amount);
        }
        if($payment_category->id == '4'){
            $this->storePaymentCategory4($apartment, $payment_category, $month, $total_amount);
        }
        // ...

        // NEXT I HAVE A FUNCTION FOR GENERATING THE PDF RECEIPT
        // NEXT I HAVE ANOTHER FUNCTION TO GENERATE THE INVOICE, IF THE APARTMENT REQUIRES ONE
    }
}

接下来我有storeMainData()函数:

$validator = Validator::make(Input::all(), MainData::$rules, MainData::$messages);
if($validator->passes()){
    $check = MainData::where('month', $month)->where('apartament_id', $apartament->id)->first();
    if(count($check)>0){
        $store_main_data = $check;
    }
    else{
        $store_main_data = new MainData;
    }

    //main data stuff (like apartment receipt data etc...)

    $store_main_data->save();
}
else{
    return Redirect::back()
        ->withErrors($validator);
}

接下来我有storePaymentCategory1()函数(这是其他付款类别如何工作的一个示例):

// NOW WE MUST APPLY: total amount stored in the payment category / apartment percentage = due payment for the apartment;

// identify the percentage 
$percentage_type = $payment_category->percentage_id;
$percentage = Percentages::where('percentage_type_id', $percentage_type)->where('apartment_id', $apartment->id)->first();

// verify if the percentage type is applied to this apartment
$calculated_apartments = Percentages::where('percentage_type_id', $percentage_type)->get();
$calculated_apartments_array = [];
foreach($calculated_apartments as $calculated_apartment){
    array_push($calculated_apartments_array, $calculated_apartment->apartment_id);
}

if(in_array($apartment->id, $calculated_apartments_array)){
    $validator = Validator::make(Input::all(), DuePayments::$rules, DuePayments::$messages);
    if($validator->passes()){
        $check = DuePayments::where('month', $month)->where('apartment_id', $apartment->id)->where('payment_category_id', $payment_category->id)->first();
        if(count($check)>0){
            $store_due_payment = $check;
        }
        else{
            $store_due_payment = new DuePayments;
        }

        // store due payment stuff

        $store_due_payment->save();
    }
    else{
        return Redirect::back()
            ->withErrors($validator);
    }
}

0 个答案:

没有答案