select aa.id as attendance_approvals_id, al.id, al.check_in_time, al.check_out_time,
al.check_in_selfie, al.check_out_selfie, al.check_in_approval, al.check_out_approval,
al.check_in_distance_variation, al.check_out_distance_variation, al.updated_at,
ar.attendance_reason, a.start_date, a.end_date, e.first_name as employee_name,e.profile_picture,
aa.action as action, aa.updated_at, b.branch_name, e.emp_id, e.id as employee_id
from attendances a
inner join attendance_logs al on a.id = al.attendance_id
and a.delete_flag = 0 and a.start_date between '2018-11-21' and '2018-11-28'
inner join attendance_approvals aa on aa.attendance_log_id = al.id
and aa.approval_flag = 0 and aa.active_flag = 1
inner join attendance_reasons ar on ar.id = al.reason_id
inner join employees e on e.id = a.employee_id and e.manager_id = 16266
inner join branches b on b.id = e.branch_id
inner join employee_shift_mappings esm on esm.emp_id = e.id and esm.shift_date = a.start_date
group by aa.id, al.id, al.check_in_time, al.check_out_time, al.check_in_selfie,
al.check_out_selfie, al.check_in_approval,
al.check_out_approval, al.check_in_distance_variation, al.check_out_distance_variation,
al.updated_at, ar.attendance_reason,
a.start_date, a.end_date, e.first_name, e.profile_picture, aa.action, aa.updated_at,
b.branch_name,e.emp_id, e.id
order by aa.id desc
Как оптимизировать этот запрос, для его выполнения потребуется более 35 секунд.
- Таблица посещаемости содержит 7 700 000 записей.
- В таблице serveance_logs есть 6 400 000 записей.
- в табеле посещаемости есть 3 900 000 записей.
- в таблице посещаемости есть 570 записей.
- таблица сотрудников имеет 60 000 записей.
- таблица employee_shift_mappings содержит 1 300 000 записей.
Пожалуйста, найдите мой план запросов
"Group (cost=94304.75..94304.77 rows=1 width=365) (actual time=23724.836..23724.859 rows=43 loops=1)"
" Group Key: aa.id, al.id, ar.attendance_reason, a.start_date, a.end_date, b.branch_name, e.id"
" -> Sort (cost=94304.75..94304.76 rows=1 width=365) (actual time=23724.832..23724.839 rows=43 loops=1)"
" Sort Key: aa.id DESC, al.id, ar.attendance_reason, a.start_date, a.end_date, b.branch_name, e.id"
" Sort Method: quicksort Memory: 47kB"
" -> Nested Loop (cost=2.15..94304.74 rows=1 width=365) (actual time=258.375..23724.602 rows=43 loops=1)"
" -> Nested Loop (cost=1.86..94297.49 rows=1 width=350) (actual time=258.364..23724.098 rows=43 loops=1)"
" Join Filter: (al.reason_id = ar.id)"
" Rows Removed by Join Filter: 24467"
" -> Nested Loop (cost=1.86..94277.15 rows=1 width=338) (actual time=258.344..23719.534 rows=43 loops=1)"
" -> Nested Loop (cost=1.42..5220.10 rows=2 width=322) (actual time=0.615..26.969 rows=344 loops=1)"
" -> Nested Loop (cost=0.99..5204.67 rows=2 width=127) (actual time=0.600..22.920 rows=394 loops=1)"
" Join Filter: (e.id = esm.emp_id)"
" -> Nested Loop (cost=0.56..5116.43 rows=11 width=131) (actual time=0.573..17.221 rows=394 loops=1)"
" -> Seq Scan on employees e (cost=0.00..4437.62 rows=79 width=111) (actual time=0.334..15.277 rows=84 loops=1)"
" Filter: (manager_id = 16266)"
" Rows Removed by Filter: 59110"
" -> Index Scan using uk_attendance_status on attendances a (cost=0.56..8.58 rows=1 width=20) (actual time=0.008..0.017 rows=5 loops=84)"
" Index Cond: ((employee_id = e.id) AND (start_date >= '2018-11-21 00:00:00'::timestamp without time zone) AND (start_date <= '2018-11-28 00:00:00'::timestamp without time zone) AND (delete_flag = 0))"
" -> Index Only Scan using idx_shift_mapping on employee_shift_mappings esm (cost=0.43..8.01 rows=1 width=8) (actual time=0.010..0.011 rows=1 loops=394)"
" Index Cond: ((emp_id = a.employee_id) AND (shift_date = a.start_date))"
" Heap Fetches: 394"
" -> Index Scan using index_attendance_logs on attendance_logs al (cost=0.43..7.71 rows=1 width=203) (actual time=0.007..0.008 rows=1 loops=394)"
" Index Cond: (attendance_id = a.id)"
" -> Index Scan using index_attendance_approvals on attendance_approvals aa (cost=0.43..44528.51 rows=1 width=20) (actual time=68.758..68.872 rows=0 loops=344)"
" Index Cond: ((attendance_log_id = al.id) AND (active_flag = 1))"
" Filter: (approval_flag = 0)"
" Rows Removed by Filter: 1"
" -> Seq Scan on attendance_reasons ar (cost=0.00..12.93 rows=593 width=20) (actual time=0.004..0.055 rows=570 loops=43)"
" -> Index Scan using branches_pkey on branches b (cost=0.29..7.24 rows=1 width=23) (actual time=0.008..0.008 rows=1 loops=43)"
" Index Cond: (id = e.branch_id)"
"Planning time: 3.099 ms"
"Execution time: 23725.179 ms"