QUERY PLAN
Aggregate (cost=2627.81..2627.82 rows=1 width=8)
-> Sort (cost=2624.70..2625.22 rows=207 width=785)
Sort Key: (CASE WHEN (tmp1.budgets IS NULL) THEN '0'::numeric ELSE tmp1.budgets END) DESC, c.created_at DESC
-> Group (cost=2604.32..2616.74 rows=207 width=785)
Group Key: c.id, (CASE WHEN (tmp1.budgets IS NULL) THEN '0'::numeric ELSE tmp1.budgets END), (CASE WHEN (tmp1.profits IS NULL) THEN '0'::numeric ELSE tmp1.profits END), (CASE WHEN (tmp1.count IS NULL) THEN '0'::bigint ELSE tmp1.count END), (CASE WHEN ((tmp1.budgets IS NULL) OR (tmp1.budgets = '0'::numeric)) THEN '0'::numeric ELSE (tmp1.profits / tmp1.budgets) END), (CASE WHEN (tmp2.budgets IS NULL) THEN '0'::numeric ELSE tmp2.budgets END), (CASE WHEN (tmp2.count IS NULL) THEN '0'::bigint ELSE tmp2.count END), (CASE WHEN (tmp3.budgets IS NULL) THEN '0'::numeric ELSE tmp3.budgets END), (CASE WHEN (tmp3.count IS NULL) THEN '0'::bigint ELSE tmp3.count END), tmp1.last_bargain_date, tmp1.count, ci.id, ct.id, (CASE WHEN (tmp1.collected IS NULL) THEN '0'::numeric ELSE tmp1.collected END), (CASE WHEN (tmp1.un_collected IS NULL) THEN '0'::numeric ELSE tmp1.un_collected END), (CASE WHEN (tmp10.invoices IS NULL) THEN '0'::numeric ELSE tmp10.invoices END), (CASE WHEN (tmp10.un_invoices IS NULL) THEN '0'::numeric ELSE tmp10.un_invoices END), (CASE WHEN (tmp11.count IS NULL) THEN '0'::bigint ELSE tmp11.count END), (CASE WHEN (tmp12.budgets IS NULL) THEN '0'::numeric ELSE tmp12.budgets END), (CASE WHEN (tmp13.budgets IS NULL) THEN '0'::numeric ELSE tmp13.budgets END), (CASE WHEN (tmp13.profits IS NULL) THEN '0'::numeric ELSE tmp13.profits END)
-> Sort (cost=2604.32..2604.84 rows=207 width=785)
Sort Key: c.id, (CASE WHEN (tmp1.budgets IS NULL) THEN '0'::numeric ELSE tmp1.budgets END) DESC, (CASE WHEN (tmp1.profits IS NULL) THEN '0'::numeric ELSE tmp1.profits END), (CASE WHEN (tmp1.count IS NULL) THEN '0'::bigint ELSE tmp1.count END), (CASE WHEN ((tmp1.budgets IS NULL) OR (tmp1.budgets = '0'::numeric)) THEN '0'::numeric ELSE (tmp1.profits / tmp1.budgets) END), (CASE WHEN (tmp2.budgets IS NULL) THEN '0'::numeric ELSE tmp2.budgets END), (CASE WHEN (tmp2.count IS NULL) THEN '0'::bigint ELSE tmp2.count END), (CASE WHEN (tmp3.budgets IS NULL) THEN '0'::numeric ELSE tmp3.budgets END), (CASE WHEN (tmp3.count IS NULL) THEN '0'::bigint ELSE tmp3.count END), tmp1.last_bargain_date, tmp1.count, ci.id, ct.id, (CASE WHEN (tmp1.collected IS NULL) THEN '0'::numeric ELSE tmp1.collected END), (CASE WHEN (tmp1.un_collected IS NULL) THEN '0'::numeric ELSE tmp1.un_collected END), (CASE WHEN (tmp10.invoices IS NULL) THEN '0'::numeric ELSE tmp10.invoices END), (CASE WHEN (tmp10.un_invoices IS NULL) THEN '0'::numeric ELSE tmp10.un_invoices END), (CASE WHEN (tmp11.count IS NULL) THEN '0'::bigint ELSE tmp11.count END), (CASE WHEN (tmp12.budgets IS NULL) THEN '0'::numeric ELSE tmp12.budgets END), (CASE WHEN (tmp13.budgets IS NULL) THEN '0'::numeric ELSE tmp13.budgets END), (CASE WHEN (tmp13.profits IS NULL) THEN '0'::numeric ELSE tmp13.profits END)
-> Hash Left Join (cost=2397.74..2596.36 rows=207 width=785)
Hash Cond: (ca.engineer_id = egr.engineer_id)
-> Hash Right Join (cost=2396.13..2590.87 rows=207 width=749)
Hash Cond: (ca.case_id = cas.id)
-> Seq Scan on tbl_case_assignments ca (cost=0.00..162.76 rows=7976 width=8)
-> Hash (cost=2393.55..2393.55 rows=206 width=749)
-> Hash Right Join (cost=2114.51..2393.55 rows=206 width=749)
Hash Cond: (cas.client_id = c.id)
-> Seq Scan on tbl_cases cas (cost=0.00..247.26 rows=7926 width=8)
-> Hash (cost=2113.86..2113.86 rows=52 width=745)
-> Hash Left Join (cost=2021.12..2113.86 rows=52 width=745)
Hash Cond: (c.client_type = ct.id)
-> Hash Left Join (cost=2019.99..2112.50 rows=52 width=599)
Hash Cond: (c.client_industry = ci.id)
-> Hash Left Join (cost=2018.45..2110.80 rows=52 width=453)
Hash Cond: (c.id = tmp13.client_id)
-> Hash Left Join (cost=1697.47..1789.68 rows=52 width=389)
Hash Cond: (c.id = tmp12.client_id)
-> Hash Left Join (cost=1375.31..1467.39 rows=52 width=357)
Hash Cond: (c.id = tmp11.client_id)
-> Hash Left Join (cost=1370.22..1462.16 rows=52 width=349)
Hash Cond: (c.id = tmp10.client_id)
-> Hash Left Join (cost=1011.12..1102.92 rows=52 width=285)
Hash Cond: (c.id = tmp3.client_id)
-> Hash Left Join (cost=696.55..788.22 rows=52 width=245)
Hash Cond: (c.id = tmp2.client_id)
-> Hash Left Join (cost=382.08..473.62 rows=52 width=205)
Hash Cond: (c.id = tmp1.client_id)
-> Nested Loop (cost=14.49..105.88 rows=52 width=65)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.73 rows=1 width=4)
Filter: ((sales_id = 1001) AND (group_id = 1))
-> Bitmap Heap Scan on tbl_clients c (cost=14.49..103.63 rows=52 width=69)
Recheck Cond: (sales_rep = 1001)
Filter: (NOT deleted)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.72 rows=59 width=0)
Index Cond: (sales_rep = 1001)
-> Hash (cost=365.10..365.10 rows=200 width=144)
-> Subquery Scan on tmp1 (cost=359.10..365.10 rows=200 width=144)
-> HashAggregate (cost=359.10..363.10 rows=200 width=144)
Group Key: p.used_client
-> HashAggregate (cost=344.45..348.11 rows=293 width=58)
Group Key: p.id, ps.id
-> Hash Right Join (cost=322.21..342.25 rows=293 width=58)
Hash Cond: (pucp.project_used_client_id = p.id)
-> Seq Scan on tbl_project_used_client_payments pucp (cost=0.00..15.50 rows=550 width=36)
-> Hash (cost=321.08..321.08 rows=90 width=26)
-> Hash Join (cost=4.80..321.08 rows=90 width=26)
Hash Cond: (ps.id = p.project_id)
-> Seq Scan on tbl_projects ps (cost=0.00..309.54 rows=1558 width=8)
Filter: (project_status = 3)
-> Hash (cost=2.69..2.69 rows=169 width=22)
-> Seq Scan on tbl_project_used_clients p (cost=0.00..2.69 rows=169 width=22)
-> Hash (cost=314.46..314.46 rows=1 width=44)
-> Subquery Scan on tmp2 (cost=314.42..314.46 rows=1 width=44)
-> GroupAggregate (cost=314.42..314.45 rows=1 width=44)
Group Key: p_1.used_client
-> Sort (cost=314.42..314.43 rows=1 width=15)
Sort Key: p_1.used_client
-> Hash Join (cost=4.80..314.41 rows=1 width=15)
Hash Cond: (ps_1.id = p_1.project_id)
-> Seq Scan on tbl_projects ps_1 (cost=0.00..309.54 rows=17 width=4)
Filter: (project_status = 1)
-> Hash (cost=2.69..2.69 rows=169 width=19)
-> Seq Scan on tbl_project_used_clients p_1 (cost=0.00..2.69 rows=169 width=19)
-> Hash (cost=314.54..314.54 rows=2 width=44)
-> Subquery Scan on tmp3 (cost=314.47..314.54 rows=2 width=44)
-> GroupAggregate (cost=314.47..314.52 rows=2 width=44)
Group Key: p_2.used_client
-> Sort (cost=314.47..314.48 rows=2 width=15)
Sort Key: p_2.used_client
-> Hash Join (cost=4.80..314.46 rows=2 width=15)
Hash Cond: (ps_2.id = p_2.project_id)
-> Seq Scan on tbl_projects ps_2 (cost=0.00..309.54 rows=27 width=4)
Filter: (project_status = 2)
-> Hash (cost=2.69..2.69 rows=169 width=19)
-> Seq Scan on tbl_project_used_clients p_2 (cost=0.00..2.69 rows=169 width=19)
-> Hash (cost=356.99..356.99 rows=169 width=68)
-> Subquery Scan on tmp10 (cost=352.76..356.99 rows=169 width=68)
-> HashAggregate (cost=352.76..355.30 rows=169 width=68)
Group Key: sc.used_client
-> HashAggregate (cost=346.85..348.96 rows=169 width=47)
Group Key: sc.id
-> Hash Right Join (cost=322.21..344.98 rows=373 width=47)
Hash Cond: (sci.project_used_client_id = sc.id)
-> Seq Scan on tbl_project_used_client_invoices sci (cost=0.00..17.00 rows=700 width=36)
-> Hash (cost=321.08..321.08 rows=90 width=15)
-> Hash Join (cost=4.80..321.08 rows=90 width=15)
Hash Cond: (ps_3.id = sc.project_id)
-> Seq Scan on tbl_projects ps_3 (cost=0.00..309.54 rows=1558 width=4)
Filter: (project_status = 3)
-> Hash (cost=2.69..2.69 rows=169 width=19)
-> Seq Scan on tbl_project_used_clients sc (cost=0.00..2.69 rows=169 width=19)
-> Hash (cost=4.49..4.49 rows=48 width=12)
-> Subquery Scan on tmp11 (cost=3.53..4.49 rows=48 width=12)
-> HashAggregate (cost=3.53..4.01 rows=48 width=44)
Group Key: sc_1.used_client
-> Seq Scan on tbl_project_used_clients sc_1 (cost=0.00..2.69 rows=169 width=8)
-> Hash (cost=321.59..321.59 rows=45 width=36)
-> Subquery Scan on tmp12 (cost=320.24..321.59 rows=45 width=36)
-> GroupAggregate (cost=320.24..321.14 rows=45 width=44)
Group Key: puc.used_client
-> Sort (cost=320.24..320.36 rows=45 width=11)
Sort Key: puc.used_client
-> Hash Join (cost=3.90..319.01 rows=45 width=11)
Hash Cond: (p_3.id = puc.project_id)
-> Seq Scan on tbl_projects p_3 (cost=0.00..309.54 rows=1365 width=4)
Filter: (project_status <> 3)
-> Hash (cost=2.69..2.69 rows=97 width=15)
-> Seq Scan on tbl_project_used_clients puc (cost=0.00..2.69 rows=97 width=15)
Filter: (used_client IS NOT NULL)
-> Hash (cost=320.97..320.97 rows=1 width=68)
-> Subquery Scan on tmp13 (cost=320.93..320.97 rows=1 width=68)
-> GroupAggregate (cost=320.93..320.96 rows=1 width=68)
Group Key: puc_1.used_client
-> Sort (cost=320.93..320.94 rows=1 width=15)
Sort Key: puc_1.used_client
-> Hash Join (cost=3.90..320.92 rows=1 width=15)
Hash Cond: (p_4.id = puc_1.project_id)
-> Seq Scan on tbl_projects p_4 (cost=0.00..316.85 rows=44 width=4)
Filter: ((project_status = 1) OR (project_status = 2))
-> Hash (cost=2.69..2.69 rows=97 width=19)
-> Seq Scan on tbl_project_used_clients puc_1 (cost=0.00..2.69 rows=97 width=19)
Filter: (used_client IS NOT NULL)
-> Hash (cost=1.24..1.24 rows=24 width=150)
-> Seq Scan on tbl_client_industries ci (cost=0.00..1.24 rows=24 width=150)
-> Hash (cost=1.06..1.06 rows=6 width=150)
-> Seq Scan on tbl_client_types ct (cost=0.00..1.06 rows=6 width=150)
-> 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)