Friday, March 31, 2017

How can I optimise this eloquent query to remove duplicates

I have this query in my controller, which produces a result I send to Datatables.

    $query = Appointment::with(['agent', 'instruction_type', 'sign_up_customer']);
                $query->with(['statuses' => function ($query) {
                    $query->latest()->first();
                }]);
                $query->paginate(10);
$table = Datatables::of($query);

But it is producing this query with 9 duplicate statements. How can I optimise this query to remove the duplicates?

select count(*) as aggregate from `appointments`
select * from `appointments` limit 10 offset 0
select * from `agents` where `agents`.`id` in ('1')
select * from `instruction_types` where `instruction_types`.`id` in ('1')
select * from `organisations` where `organisations`.`id` in ('2')
select `statuses`.*, `appointment_status`.`appointment_id` as `pivot_appointment_id`, `appointment_status`.`status_id` as `pivot_status_id`, `appointment_status`.`created_at` as `pivot_created_at`, `appointment_status`.`updated_at` as `pivot_updated_at` from `statuses` inner join `appointment_status` on `statuses`.`id` = `appointment_status`.`status_id` where `appointment_status`.`appointment_id` in ('2') order by `created_at` desc     select `statuses`.*, `appointment_status`.`appointment_id` as `pivot_appointment_id`, `appointment_status`.`status_id` as `pivot_status_id`, `appointment_status`.`created_at` as `pivot_created_at`, `appointment_status`.`updated_at` as `pivot_updated_at`, `appointment_status`.`appointment_id` as `pivot_appointment_id`, `appointment_status`.`status_id` as `pivot_status_id`, `appointment_status`.`created_at` as `pivot_created_at`, `appointment_status`.`updated_at` as `pivot_updated_at` from `statuses` inner join `appointment_status` on `statuses`.`id` = `appointment_status`.`status_id` where `appointment_status`.`appointment_id` in ('2') order by `created_at` desc limit 1
select count(*) as aggregate from (select '1' as `row_count` from `appointments` limit 10 offset 0) count_row_table
select * from `appointments` limit 100 offset 0
select * from `agents` where `agents`.`id` in ('1')
select * from `instruction_types` where `instruction_types`.`id` in ('1')
select * from `organisations` where `organisations`.`id` in ('2')
select `statuses`.*, `appointment_status`.`appointment_id` as `pivot_appointment_id`, `appointment_status`.`status_id` as `pivot_status_id`, `appointment_status`.`created_at` as `pivot_created_at`, `appointment_status`.`updated_at` as `pivot_updated_at` from `statuses` inner join `appointment_status` on `statuses`.`id` = `appointment_status`.`status_id` where `appointment_status`.`appointment_id` in ('2') order by `created_at` desc limit 1
select `statuses`.*, `appointment_status`.`appointment_id` as `pivot_appointment_id`, `appointment_status`.`status_id` as `pivot_status_id`, `appointment_status`.`created_at` as `pivot_created_at`, `appointment_status`.`updated_at` as `pivot_updated_at`, `appointment_status`.`appointment_id` as `pivot_appointment_id`, `appointment_status`.`status_id` as `pivot_status_id`, `appointment_status`.`created_at` as `pivot_created_at`, `appointment_status`.`updated_at` as `pivot_updated_at` from `statuses` inner join `appointment_status` on `statuses`.`id` = `appointment_status`.`status_id` where `appointment_status`.`appointment_id` in ('2') order by `created_at` desc limit 1



via showFocus

Advertisement