QUERY PLAN
Aggregate (cost=1366.57..1366.58 rows=1 width=8)
-> Sort (cost=1358.47..1359.82 rows=540 width=1766)
Sort Key: "*SELECT* 1".start_at DESC, "*SELECT* 1".ticket_id DESC
-> Result (cost=383.57..1333.96 rows=540 width=1766)
-> Append (cost=383.57..1328.56 rows=540 width=60)
-> Subquery Scan on "*SELECT* 1" (cost=383.57..512.28 rows=48 width=60)
-> Hash Right Join (cost=383.57..511.80 rows=48 width=280)
Hash Cond: (cl.id = COALESCE(p.client_id, c.client_id))
-> Seq Scan on tbl_clients cl (cost=0.00..110.13 rows=2013 width=8)
-> Hash (cost=382.97..382.97 rows=48 width=28)
-> Nested Loop Left Join (cost=0.29..382.97 rows=48 width=28)
-> Seq Scan on tbl_pre_sales p (cost=0.00..96.45 rows=48 width=24)
Filter: ((start_at IS NOT NULL) AND (end_at IS NOT NULL) AND (engineer_id IS NOT NULL) AND ((NOT is_draft) OR (is_draft IS NULL)) AND (end_at > start_at) AND (engineer_id = 1023))
-> Index Scan using tbl_cases_pkey on tbl_cases c (cost=0.29..5.97 rows=1 width=12)
Index Cond: (id = p.case_id)
-> Subquery Scan on "*SELECT* 2" (cost=446.08..816.28 rows=492 width=60)
-> Hash Left Join (cost=446.08..811.36 rows=492 width=280)
Hash Cond: (COALESCE(i.client_id, c_1.client_id) = cl_1.id)
-> Hash Join (cost=310.79..674.78 rows=492 width=28)
Hash Cond: (i.case_id = c_1.id)
-> Seq Scan on tbl_implement_records i (cost=0.00..357.85 rows=2340 width=20)
Filter: ((arrived_at IS NOT NULL) AND (left_at IS NOT NULL) AND ((NOT is_draft) OR (is_draft IS NULL)) AND (left_at > arrived_at))
-> Hash (cost=288.43..288.43 rows=1789 width=16)
-> Seq Scan on tbl_cases c_1 (cost=0.00..288.43 rows=1789 width=16)
Filter: ((last_engineer IS NOT NULL) AND (last_engineer = 1023))
-> Hash (cost=110.13..110.13 rows=2013 width=8)
-> Seq Scan on tbl_clients cl_1 (cost=0.00..110.13 rows=2013 width=8)