QUERY PLAN
Limit (cost=2624.54..2624.59 rows=20 width=785)
-> Sort (cost=2624.54..2625.06 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=2606.61..2619.03 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 ((sum(p_1.budget)) IS NULL) THEN '0'::numeric ELSE (sum(p_1.budget)) END), (CASE WHEN ((count(p_1.id)) IS NULL) THEN '0'::bigint ELSE (count(p_1.id)) END), (CASE WHEN ((sum(p_2.budget)) IS NULL) THEN '0'::numeric ELSE (sum(p_2.budget)) END), (CASE WHEN ((count(p_2.id)) IS NULL) THEN '0'::bigint ELSE (count(p_2.id)) 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 ((sum(puc.budget)) IS NULL) THEN '0'::numeric ELSE (sum(puc.budget)) END), (CASE WHEN ((sum(puc_1.budget)) IS NULL) THEN '0'::numeric ELSE (sum(puc_1.budget)) END), (CASE WHEN ((sum(puc_1.profit)) IS NULL) THEN '0'::numeric ELSE (sum(puc_1.profit)) END)
-> Sort (cost=2606.61..2607.13 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 ((sum(p_1.budget)) IS NULL) THEN '0'::numeric ELSE (sum(p_1.budget)) END), (CASE WHEN ((count(p_1.id)) IS NULL) THEN '0'::bigint ELSE (count(p_1.id)) END), (CASE WHEN ((sum(p_2.budget)) IS NULL) THEN '0'::numeric ELSE (sum(p_2.budget)) END), (CASE WHEN ((count(p_2.id)) IS NULL) THEN '0'::bigint ELSE (count(p_2.id)) 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 ((sum(puc.budget)) IS NULL) THEN '0'::numeric ELSE (sum(puc.budget)) END), (CASE WHEN ((sum(puc_1.budget)) IS NULL) THEN '0'::numeric ELSE (sum(puc_1.budget)) END), (CASE WHEN ((sum(puc_1.profit)) IS NULL) THEN '0'::numeric ELSE (sum(puc_1.profit)) END)
-> Hash Left Join (cost=2399.12..2598.65 rows=207 width=785)
Hash Cond: (ca.engineer_id = egr.engineer_id)
-> Hash Left Join (cost=2397.51..2593.16 rows=207 width=749)
Hash Cond: (c.client_type = ct.id)
-> Hash Right Join (cost=2396.37..2591.11 rows=207 width=603)
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.80..2393.80 rows=206 width=603)
-> Hash Left Join (cost=2114.13..2393.80 rows=206 width=603)
Hash Cond: (c.client_industry = ci.id)
-> Hash Right Join (cost=2112.59..2391.63 rows=206 width=457)
Hash Cond: (cas.client_id = c.id)
-> Seq Scan on tbl_cases cas (cost=0.00..247.26 rows=7926 width=8)
-> Hash (cost=2111.94..2111.94 rows=52 width=453)
-> Merge Left Join (cost=2109.64..2111.94 rows=52 width=453)
Merge Cond: (c.id = puc_1.used_client)
-> Merge Left Join (cost=1788.70..1790.82 rows=52 width=389)
Merge Cond: (c.id = puc.used_client)
-> Merge Left Join (cost=1468.46..1468.98 rows=52 width=357)
Merge Cond: (c.id = p_2.used_client)
-> Merge Left Join (cost=1153.99..1154.30 rows=52 width=317)
Merge Cond: (c.id = p_1.used_client)
-> Sort (cost=839.57..839.70 rows=52 width=277)
Sort Key: c.id
-> Hash Left Join (cost=746.28..838.08 rows=52 width=277)
Hash Cond: (c.id = tmp11.client_id)
-> Hash Left Join (cost=741.18..832.85 rows=52 width=269)
Hash Cond: (c.id = tmp10.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=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_1.id = sc.project_id)
-> Seq Scan on tbl_projects ps_1 (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)
-> 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_2.id = p_1.project_id)
-> Seq Scan on tbl_projects ps_2 (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)
-> 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_3.id = p_2.project_id)
-> Seq Scan on tbl_projects ps_3 (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)
-> 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)
-> 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)