Я использую MySQL представление с объединениями 12 таблиц. Ответ API очень медленный, потому что после получения ответа от представления я использую foreach l oop для некоторых операций. Ответ был медленным, чтобы оптимизировать API. Я удалил foreach l oop и присоединился к еще одной таблице, чтобы получить желаемый результат из представления без использования foreach. и теперь объединение достигает 15 таблиц.
Теперь мой вопрос заключается в том, что лучше всего подходить с использованием представления без foreach или сокращать объединения таблиц и выполнять нужные операции с помощью foreach.
с более соединения таблиц и без foreach, с меньшим числом соединений таблиц и использованием foreach в обоих случаях api не оптимизирован в соответствии с желанием;
мой пример кода
$bills = Billing::get_filtered_collection_data($request);
if ($request->has('pagination') && $request->input('pagination') == 1){
if($request->has('per_page')){
$bills = $bills->paginate($request->input('per_page'));
}else{
$bills = $bills->paginate(20);
}
}else{
$bills = $bills->get();
}
foreach ($bills as $bill) {
Log::info('bill_cooment_query');
$bill->comment_counter = GeneralComment::comment_counter($bill->case_id, 9, $bill
$bill->reminder_cycle = BillAppeal::where('bill_id','=',$bill->bill_id)->count();
}
и мое представление равно
CREATE
OR REPLACE View bill_collections as
SELECT
bills.created_at AS bill_date,
bills.id AS bill_id,
bills.bill_to AS bill_to,
bills.case_id,
bills.delivery_id,
bills.outstanding_amount,
bills.total_amount,
bills.total_amount_paid,
bills.write_off_amount,
bills.litigation_amount,
bills.arbitration_amount,
bills.collection_amount,
bills.settlement_amount,
bills.temp_outstanding_amount,
bills.overload_payment,
bill_states.name AS bill_status_name,
bills.verification AS verification,
bills.denial AS denial,
bills.bill_state_id AS bill_status_id,
bills.last_status_id AS last_status_id,
bills.last_call_date,
bills.last_appeal_date,
bills.reminder_date,
bills.last_call_time,
bills.color_status,
bills.collection_date,
bill_responses.denial_type_id,
insurances.name AS insurance_name,
patients.id as patient_id,
REPLACE(
CONCAT(
patients.first_name,
' ',
COALESCE(patients.middle_name, ''),
' ',
patients.last_name
),
' ',
' '
) as patient_name,
providers.office_name as provider_name,
attorneys.id as attorney_id,
REPLACE(
CONCAT(
attorneys.first_name,
' ',
COALESCE(attorneys.middle_name, ''),
' ',
attorneys.last_name
),
' ',
' '
) as attorney_name,
accidents.accident_date,
accidents.accident_time,
ovada_cases.provider_id,
ovada_cases.accident_id,
ovada_cases.ovada_case_type_id,
ovada_case_types.name as ovada_case_type_name,
case_insurances.adjuster_id,
case_insurances.insurance_id,
adjusters.cell_phone_no as adjuster_phone,
CONCAT(
adjusters.first_name, ' ', adjusters.last_name
) as adjuster_name,
adjusters.email as adjuster_email,
bill_devices.start_date as start_date,
bill_devices.end_date as end_date,
case_insurances.claim_no,
case_insurances.policy_no,
IF(
bceh_fax.id IS NULL,
0,
IF(
bceh_fax.history_type = 'fax', 1, 0
)
) as fax_count,
IF(
bceh_print.id IS NULL,
0,
IF(
bceh_print.history_type = 'print',
1, 0
)
) as print_count,
IF(
bceh_email.id IS NULL,
0,
IF(
bceh_email.history_type = 'email',
1, 0
)
) as email_count,
user_has_collection_bills.id as history_count,
fax_response.id as fax_response_count ,
MAX(CONCAT(
users_basic_info.first_name, ' ', users_basic_info.last_name
)) as last_user_name,
MAX(DATE_FORMAT(bill_collections_appeals_history.created_at, '%Y-%m-%d %H:%i:%s')) as last_appeal_date,
ROUND(PERIOD_DIFF(DATE_FORMAT(CURRENT_DATE(), '%Y%m'),DATE_FORMAT(bills.created_at,'%Y%m'))) as billdays,
bill_splits.injury_split_id
FROM
bills
INNER JOIN ovada_cases ON ovada_cases.id = bills.case_id
AND bills.deleted_at IS NULL
and ovada_cases.deleted_at IS NULL
INNER JOIN case_insurances ON case_insurances.case_id = bills.case_id
AND case_insurances.insurance_type = 1 AND case_insurances.deleted_at IS NULL
INNER JOIN insurances ON insurances.id = case_insurances.insurance_id
AND insurances.deleted_at IS NULL
INNER JOIN patients ON patients.id = ovada_cases.patient_id
AND patients.deleted_at IS NULL
INNER JOIN providers ON providers.id = ovada_cases.provider_id
AND providers.deleted_at IS NULL
INNER JOIN accidents ON accidents.id = ovada_cases.accident_id
AND accidents.deleted_at IS NULL
INNER JOIN ovada_case_types ON ovada_case_types.id = ovada_cases.ovada_case_type_id
AND ovada_case_types.deleted_at IS NULL
INNER JOIN adjusters ON adjusters.id = case_insurances.adjuster_id
AND adjusters.deleted_at IS NULL
LEFT JOIN bill_responses ON bill_responses.bill_id = bills.id
AND bill_responses.deleted_at IS NULL
LEFT JOIN bill_devices ON bill_devices.bill_id = bills.id
AND bill_devices.deleted_at IS NULL
INNER JOIN attorneys ON attorneys.id = ovada_cases.attorney_id
AND attorneys.deleted_at IS NULL
LEFT JOIN bill_collection_efp_history as bceh_fax ON bills.id = bceh_fax.bill_id
AND bceh_fax.history_type = 'fax'
AND bceh_fax.deleted_at is NULL
LEFT JOIN bill_collection_efp_history as bceh_print ON bills.id = bceh_print.bill_id
AND bceh_print.history_type = 'print'
AND bceh_print.deleted_at is NULL
LEFT JOIN bill_collection_efp_history as bceh_email ON bills.id = bceh_email.bill_id
AND bceh_email.history_type = 'email'
AND bceh_email.deleted_at is NULL
LEFT JOIN user_has_collection_bills ON user_has_collection_bills.bill_id = bills.id
AND user_has_collection_bills.deleted_at IS NULL
LEFT JOIN fax_response ON fax_response.bill_id = bills.id
AND fax_response.deleted_at IS NULL
INNER JOIN bill_states ON bill_states.id = bills.bill_state_id
AND bill_states.deleted_at IS NULL
LEFT JOIN users_basic_info ON user_has_collection_bills.user_id = users_basic_info.user_id
AND users_basic_info.deleted_at IS NULL
LEFT JOIN bill_collections_appeals_history ON bill_collections_appeals_history.bill_id = bills.id
AND bill_collections_appeals_history.deleted_at IS NULL
LEFT JOIN bill_splits ON bills.id = bill_splits.bill_id
AND bill_splits.deleted_at IS NULL
GROUP BY
bills.id