QUERY PLAN
Limit (cost=1728.78..1728.83 rows=20 width=313)
-> Sort (cost=1728.78..1728.89 rows=43 width=313)
Sort Key: (CASE WHEN (t0_2.id IS NOT NULL) THEN t0_2.remind_date ELSE sc.signed_at END) DESC
-> Hash Left Join (cost=1650.60..1727.64 rows=43 width=313)
Hash Cond: (sc.id = ovsub.id)
-> Nested Loop (cost=1272.59..1348.23 rows=43 width=120)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.77 rows=1 width=4)
Filter: ((sales_id = 1001) AND (group_id = 1))
-> Nested Loop Left Join (cost=1272.59..1346.03 rows=43 width=124)
-> Hash Right Join (cost=1272.31..1317.58 rows=43 width=124)
Hash Cond: (t0.id = sc.id)
-> HashAggregate (cost=201.10..222.45 rows=1708 width=36)
Group Key: t0.id
-> Hash Right Join (cost=139.60..190.54 rows=2112 width=17)
Hash Cond: (t1.contract_id = t0.id)
-> Seq Scan on tbl_sales_contract_invoices t1 (cost=0.00..45.33 rows=2133 width=10)
-> Hash (cost=118.25..118.25 rows=1708 width=11)
-> Seq Scan on tbl_sales_contracts t0 (cost=0.00..118.25 rows=1708 width=11)
Filter: need_analysis
-> Hash (cost=1070.67..1070.67 rows=43 width=92)
-> Hash Right Join (cost=1029.27..1070.67 rows=43 width=92)
Hash Cond: (t0_1.id = sc.id)
-> HashAggregate (cost=411.81..429.06 rows=1725 width=44)
Group Key: t0_1.id
-> Hash Right Join (cost=222.48..401.06 rows=2150 width=36)
Hash Cond: ("*SELECT* 1".contract_id = t0_1.id)
-> Append (cost=122.76..274.19 rows=2150 width=124)
-> Subquery Scan on "*SELECT* 1" (cost=122.76..207.20 rows=2147 width=83)
-> Hash Join (cost=122.76..180.36 rows=2147 width=79)
Hash Cond: (r.erp_collect_ticket_id = t.id)
-> Seq Scan on tbl_erp_collect_ticket_settles r (cost=0.00..35.83 rows=2154 width=18)
Filter: (sales_contract_id IS NOT NULL)
-> Hash (cost=95.42..95.42 rows=2187 width=37)
-> Seq Scan on tbl_erp_collect_tickets t (cost=0.00..95.42 rows=2187 width=37)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Nested Loop (cost=0.29..9.32 rows=1 width=122)
-> Seq Scan on tbl_erp_cash_cost_ticket_sales_contract_settles r_1 (cost=0.00..1.01 rows=1 width=44)
Filter: (sales_contract_id IS NOT NULL)
-> Index Scan using tbl_erp_cash_cost_tickets_pkey on tbl_erp_cash_cost_tickets t_1 (cost=0.29..8.30 rows=1 width=46)
Index Cond: (id = r_1.erp_cash_cost_ticket_id)
Filter: (is_payment_settle AND (status = ANY ('{2,5}'::integer[])))
-> Nested Loop (cost=0.28..57.64 rows=2 width=116)
-> Seq Scan on tbl_erp_payment_ticket_settles r_2 (cost=0.00..41.01 rows=2 width=18)
Filter: (sales_contract_id IS NOT NULL)
-> Index Scan using tbl_erp_payment_tickets_pkey on tbl_erp_payment_tickets t_2 (cost=0.28..8.30 rows=1 width=40)
Index Cond: (id = r_2.erp_payment_ticket_id)
Filter: (is_payment_settle AND (status = ANY ('{2,5}'::integer[])))
-> Hash (cost=78.15..78.15 rows=1725 width=4)
-> Index Only Scan using tbl_sales_contracts_pkey on tbl_sales_contracts t0_1 (cost=0.28..78.15 rows=1725 width=4)
-> Hash (cost=616.93..616.93 rows=43 width=84)
-> Hash Right Join (cost=601.90..616.93 rows=43 width=84)
Hash Cond: (t0_2.contract_id = sc.id)
-> HashAggregate (cost=379.20..385.46 rows=626 width=41)
Group Key: t0_2.id
-> Hash Join (cost=180.87..377.64 rows=626 width=32)
Hash Cond: (p_1.id = t1_1.from_project)
-> Seq Scan on tbl_projects p_1 (cost=0.00..184.28 rows=1691 width=4)
Filter: (project_status = 3)
-> Hash (cost=166.34..166.34 rows=1163 width=36)
-> Hash Join (cost=139.24..166.34 rows=1163 width=36)
Hash Cond: (t0_2.contract_id = t1_1.id)
-> Seq Scan on tbl_sales_contract_payment_collection_remind_dates t0_2 (cost=0.00..23.95 rows=1195 width=32)
-> Hash (cost=118.25..118.25 rows=1679 width=8)
-> Seq Scan on tbl_sales_contracts t1_1 (cost=0.00..118.25 rows=1679 width=8)
Filter: (need_analysis AND (from_project IS NOT NULL))
-> Hash (cost=222.16..222.16 rows=43 width=56)
-> Hash Join (cost=99.40..222.16 rows=43 width=56)
Hash Cond: (sc.client_id = c.id)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..118.25 rows=1713 width=52)
Filter: build_by_project
-> Hash (cost=98.78..98.78 rows=50 width=8)
-> Bitmap Heap Scan on tbl_clients c (cost=4.79..98.78 rows=50 width=8)
Recheck Cond: (sales_rep = 1001)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.78 rows=67 width=0)
Index Cond: (sales_rep = 1001)
-> Index Scan using tbl_projects_pkey on tbl_projects p (cost=0.28..0.66 rows=1 width=8)
Index Cond: (sc.from_project = id)
-> Hash (cost=375.44..375.44 rows=205 width=36)
-> Subquery Scan on ovsub (cost=370.83..375.44 rows=205 width=36)
-> HashAggregate (cost=370.83..373.39 rows=205 width=36)
Group Key: t1_2.id
-> Hash Join (cost=177.18..369.81 rows=205 width=10)
Hash Cond: (p_2.id = t1_2.from_project)
-> Seq Scan on tbl_projects p_2 (cost=0.00..184.28 rows=1691 width=4)
Filter: (project_status = 3)
-> Hash (cost=172.43..172.43 rows=380 width=14)
-> Hash Join (cost=37.79..172.43 rows=380 width=14)
Hash Cond: (t1_2.id = t0_3.contract_id)
-> Seq Scan on tbl_sales_contracts t1_2 (cost=0.00..118.25 rows=1679 width=8)
Filter: (need_analysis AND (from_project IS NOT NULL))
-> Hash (cost=32.91..32.91 rows=390 width=10)
-> Seq Scan on tbl_sales_contract_payment_collection_remind_dates t0_3 (cost=0.00..32.91 rows=390 width=10)
Filter: (((status = 1) AND (overdue_days IS NOT NULL)) OR ((status = 0) AND (remind_date <= '2026-06-02'::date)))