QUERY PLAN
Limit (cost=7197.59..7201.89 rows=20 width=1050)
-> Result (cost=7197.59..7210.06 rows=58 width=1050)
-> Sort (cost=7197.59..7197.74 rows=58 width=1134)
Sort Key: (CASE WHEN ((sum(CASE WHEN (p_1.project_status = 3) THEN p_1.budget ELSE '0'::numeric END)) IS NULL) THEN '0'::numeric ELSE (sum(CASE WHEN (p_1.project_status = 3) THEN p_1.budget ELSE '0'::numeric END)) END) DESC, c.created_at DESC
-> Nested Loop (cost=6867.01..7196.05 rows=58 width=1134)
-> 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=6867.01..7192.11 rows=58 width=1101)
Join Filter: (ct.id = c.client_type)
-> Nested Loop Left Join (cost=6867.01..7186.39 rows=58 width=955)
Join Filter: (ci.id = c.client_industry)
-> Nested Loop Left Join (cost=6867.01..7164.02 rows=58 width=809)
Join Filter: (cc.id = c.client_class)
-> Hash Left Join (cost=6867.01..7159.81 rows=58 width=769)
Hash Cond: (c.id = tmp8.client_id)
-> Merge Left Join (cost=6560.90..6853.56 rows=58 width=737)
Merge Cond: (c.id = tmp10.client_id)
-> Merge Left Join (cost=6286.47..6577.92 rows=58 width=673)
Merge Cond: (c.id = tmp9.client_id)
-> Merge Left Join (cost=5307.12..5597.37 rows=58 width=641)
Merge Cond: (c.id = tmp4.client_id)
-> Nested Loop Left Join (cost=5159.11..5447.54 rows=58 width=617)
Join Filter: ((csrr.sales_rep = c.sales_rep) AND (csrr.client_id = c.id))
-> Merge Left Join (cost=5083.21..5271.68 rows=58 width=617)
Merge Cond: (c.id = p_1.client_id)
-> Sort (cost=4798.28..4798.42 rows=58 width=325)
Sort Key: c.id
-> Hash Right Join (cost=4766.82..4796.58 rows=58 width=325)
Hash Cond: (wl.client_id = c.id)
-> HashAggregate (cost=3906.87..3919.25 rows=1238 width=148)
Group Key: wl.client_id
-> Seq Scan on tbl_work_logs wl (cost=0.00..1713.31 rows=30256 width=36)
Filter: ((NOT is_draft) AND (client_id IS NOT NULL))
-> Hash (cost=859.22..859.22 rows=58 width=181)
-> Hash Left Join (cost=835.33..859.22 rows=58 width=181)
Hash Cond: (c.id = tmp5.client_id)
-> Hash Right Join (cost=318.61..342.36 rows=58 width=157)
Hash Cond: (p.client_id = c.id)
-> HashAggregate (cost=209.28..221.09 rows=945 width=44)
Group Key: p.client_id
-> Seq Scan on tbl_projects p (cost=0.00..177.64 rows=3164 width=17)
-> Hash (cost=108.61..108.61 rows=58 width=117)
-> Bitmap Heap Scan on tbl_clients c (cost=14.46..108.61 rows=58 width=117)
Recheck Cond: (sales_rep = 1001)
Filter: (NOT deleted)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.78 rows=67 width=0)
Index Cond: (sales_rep = 1001)
-> Hash (cost=513.62..513.62 rows=247 width=28)
-> Subquery Scan on tmp5 (cost=508.68..513.62 rows=247 width=28)
-> HashAggregate (cost=508.68..511.15 rows=247 width=28)
Group Key: ir.client_id
-> Hash Left Join (cost=1.61..438.45 rows=7023 width=24)
Hash Cond: (ir.created_by = egr.engineer_id)
-> Seq Scan on tbl_implement_records ir (cost=0.00..340.28 rows=7023 width=28)
Filter: (NOT is_draft)
-> Hash (cost=1.27..1.27 rows=27 width=4)
-> Seq Scan on tbl_engineer_group_relationships egr (cost=0.00..1.27 rows=27 width=4)
-> GroupAggregate (cost=284.93..461.82 rows=884 width=296)
Group Key: p_1.client_id
-> Sort (cost=284.93..289.35 rows=1769 width=34)
Sort Key: p_1.client_id
-> Seq Scan on tbl_projects p_1 (cost=0.00..189.50 rows=1769 width=34)
Filter: (project_status = ANY ('{1,2,3}'::integer[]))
-> Materialize (cost=75.90..131.31 rows=44 width=8)
-> Hash Join (cost=75.90..131.09 rows=44 width=8)
Hash Cond: (csrr.id = "ANY_subquery".max)
-> Seq Scan on tbl_client_sales_rep_rels csrr (cost=0.00..54.46 rows=89 width=12)
Filter: (sales_rep = 1001)
-> Hash (cost=73.40..73.40 rows=200 width=4)
-> HashAggregate (cost=71.40..73.40 rows=200 width=4)
Group Key: "ANY_subquery".max
-> Subquery Scan on "ANY_subquery" (cost=52.50..69.30 rows=840 width=4)
-> HashAggregate (cost=52.50..60.90 rows=840 width=8)
Group Key: csrrs.client_id
-> Seq Scan on tbl_client_sales_rep_rels csrrs (cost=0.00..47.57 rows=986 width=8)
Filter: (end_at IS NULL)
-> Sort (cost=148.01..148.80 rows=316 width=28)
Sort Key: tmp4.client_id
-> Subquery Scan on tmp4 (cost=128.57..134.89 rows=316 width=28)
-> HashAggregate (cost=128.57..131.73 rows=316 width=28)
Group Key: ps.client_id
-> Hash Left Join (cost=1.61..112.29 rows=1628 width=24)
Hash Cond: (ps.created_by = egr_1.engineer_id)
-> Seq Scan on tbl_pre_sales ps (cost=0.00..88.30 rows=1628 width=28)
Filter: (NOT is_draft)
-> Hash (cost=1.27..1.27 rows=27 width=4)
-> Seq Scan on tbl_engineer_group_relationships egr_1 (cost=0.00..1.27 rows=27 width=4)
-> Sort (cost=979.35..979.85 rows=200 width=36)
Sort Key: tmp9.client_id
-> Subquery Scan on tmp9 (cost=967.20..971.70 rows=200 width=36)
-> HashAggregate (cost=967.20..969.70 rows=200 width=36)
Group Key: c_1.client_id
-> HashAggregate (cost=733.07..839.49 rows=8514 width=40)
Group Key: c_1.id
-> Hash Left Join (cost=375.17..690.22 rows=8570 width=16)
Hash Cond: (ca.engineer_id = egr_2.engineer_id)
-> Hash Right Join (cost=373.56..570.77 rows=8570 width=20)
Hash Cond: (ca.case_id = c_1.id)
-> Seq Scan on tbl_case_assignments ca (cost=0.00..174.70 rows=8570 width=16)
-> Hash (cost=267.14..267.14 rows=8514 width=8)
-> Seq Scan on tbl_cases c_1 (cost=0.00..267.14 rows=8514 width=8)
-> Hash (cost=1.27..1.27 rows=27 width=4)
-> Seq Scan on tbl_engineer_group_relationships egr_2 (cost=0.00..1.27 rows=27 width=4)
-> Sort (cost=274.44..274.94 rows=200 width=68)
Sort Key: tmp10.client_id
-> Subquery Scan on tmp10 (cost=261.79..266.79 rows=200 width=68)
-> HashAggregate (cost=261.79..264.79 rows=200 width=68)
Group Key: sc.client_id
-> HashAggregate (cost=201.42..222.98 rows=1725 width=47)
Group Key: sc.id
-> Hash Right Join (cost=139.81..190.75 rows=2133 width=21)
Hash Cond: (sci.contract_id = sc.id)
-> Seq Scan on tbl_sales_contract_invoices sci (cost=0.00..45.33 rows=2133 width=10)
-> Hash (cost=118.25..118.25 rows=1725 width=15)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..118.25 rows=1725 width=15)
-> Hash (cost=300.70..300.70 rows=432 width=36)
-> Subquery Scan on tmp8 (cost=290.98..300.70 rows=432 width=36)
-> HashAggregate (cost=290.98..296.38 rows=432 width=36)
Group Key: r.client_id
-> Seq Scan on tbl_reimbursements r (cost=0.00..249.84 rows=8229 width=9)
Filter: (status = ANY ('{2,3}'::integer[]))
-> Materialize (cost=0.00..1.06 rows=4 width=44)
-> Seq Scan on tbl_client_classes cc (cost=0.00..1.04 rows=4 width=44)
-> Materialize (cost=0.00..1.38 rows=25 width=150)
-> Seq Scan on tbl_client_industries ci (cost=0.00..1.25 rows=25 width=150)
-> Materialize (cost=0.00..1.09 rows=6 width=150)
-> Seq Scan on tbl_client_types ct (cost=0.00..1.06 rows=6 width=150)