I have a query that joined many table at once, for now it's OK without any performance issue (only less than 100 data in the database). But I'm scared that it will cause some trouble when the data in database become large (ex: thousand of data).
I'm doing join in the same table to get attendance in & out time in same row data. And the other join is for information purpose
Here is my query builder:
DB::table('attendance as in')
->where('in.in_out', 'in')
->where('in.company_id', \Session::get('selected_company'))
->whereDate('in.created', Carbon::today())
->leftJoin('attendance as out', function ($join) {
$join->on('in.employee_id', 'out.employee_id')
->where('out.in_out', 'out')
->where('in.company_id', \Session::get('selected_company'))
->whereDate('out.created', Carbon::today());
})
->join('employee', 'employee.id', 'in.employee_id')
->join('location_library', 'location_library.id', 'in.attendance_location_id')
->join('company as cp', 'cp.id', 'in.company_id')
->join('employee_in_app as e_app', 'e_app.employee_id', 'in.employee_id')
->select('in.id', 'in.employee_id', 'in.attendance_time as in_time', 'out.attendance_time as out_time', 'in.work_hour_start', 'in.late_tolerance', 'employee.*', 'location_library.location_name', 'in.attendance_location_id', 'cp.alias', 'e_app.note', 'in.company_id', 'in.attendance_time')
->orderBy('in.attendance_time', 'DESC')
->get();
If I get the raw sql using toSql()
method. It's like this:
select
`in`.`id`,
`in`.`employee_id`,
`in`.`attendance_time` as `in_time`,
`out`.`attendance_time` as `out_time`,
`in`.`work_hour_start`,
`in`.`late_tolerance`,
`employee`.*,
`location_library`.`location_name`,
`in`.`attendance_location_id`,
`cp`.`alias`,
`e_app`.`note`,
`in`.`company_id`,
`in`.`attendance_time`
from
`attendance` as `in`
left join `attendance` as `out` on `in`.`employee_id` = `out`.`employee_id`
and `out`.`in_out` = ?
and `in`.`company_id` = ?
and date(`out`.`created`) = ?
inner join `employee` on `employee`.`id` = `in`.`employee_id`
inner join `location_library` on `location_library`.`id` = `in`.`attendance_location_id`
inner join `company` as `cp` on `cp`.`id` = `in`.`company_id`
inner join `employee_in_app` as `e_app` on `e_app`.`employee_id` = `in`.`employee_id`
where
`in`.`in_out` = ?
and `in`.`company_id` = ?
and date(`in`.`created`) = ?
order by
`in`.`attendance_time` desc
Is my query fine enough to handle many data? if not what I have to change?
Did you run an explain for your query?
EXPLAIN <sql>
This can give you more information about the usage of indexes, that can have a big impact on your performance.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community