QUERY PLAN
Limit (cost=5541.79..5541.84 rows=20 width=785)
-> Sort (cost=5541.79..5558.40 rows=6645 width=785)
Sort Key: (CASE WHEN (tmp12.budgets IS NULL) THEN '0'::numeric ELSE tmp12.budgets END) DESC NULLS LAST, (CASE WHEN (tmp1.budgets IS NULL) THEN '0'::numeric ELSE tmp1.budgets END) DESC, c.created_at DESC
-> Group (cost=4966.27..5364.97 rows=6645 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=4966.27..4982.88 rows=6645 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) DESC NULLS LAST, (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=1833.00..2227.37 rows=6645 width=785)
Hash Cond: (ca.engineer_id = egr.engineer_id)
-> Hash Right Join (cost=1831.39..2101.17 rows=6645 width=749)
Hash Cond: (cas.client_id = c.id)
-> Hash Right Join (cost=329.70..504.56 rows=7592 width=8)
Hash Cond: (ca.case_id = cas.id)
-> Seq Scan on tbl_case_assignments ca (cost=0.00..154.92 rows=7592 width=8)
-> Hash (cost=235.42..235.42 rows=7542 width=8)
-> Seq Scan on tbl_cases cas (cost=0.00..235.42 rows=7542 width=8)
-> Hash (cost=1481.10..1481.10 rows=1648 width=745)
-> Hash Left Join (cost=1165.90..1481.10 rows=1648 width=745)
Hash Cond: (c.client_type = ct.id)
-> Hash Left Join (cost=1164.76..1472.69 rows=1648 width=599)
Hash Cond: (c.client_industry = ci.id)
-> Hash Left Join (cost=1163.22..1466.09 rows=1648 width=453)
Hash Cond: (c.id = tmp13.client_id)
-> Hash Left Join (cost=982.59..1281.12 rows=1648 width=389)
Hash Cond: (c.id = tmp12.client_id)
-> Hash Left Join (cost=800.66..1094.85 rows=1648 width=357)
Hash Cond: (c.id = tmp11.client_id)
-> Hash Left Join (cost=796.53..1086.38 rows=1648 width=349)
Hash Cond: (c.id = tmp10.client_id)
-> Hash Left Join (cost=585.12..870.64 rows=1648 width=285)
Hash Cond: (c.id = tmp3.client_id)
-> Hash Left Join (cost=411.49..692.66 rows=1648 width=245)
Hash Cond: (c.id = tmp2.client_id)
-> Hash Left Join (cost=237.87..514.71 rows=1648 width=205)
Hash Cond: (c.id = tmp1.client_id)
-> Hash Left Join (cost=13.47..285.98 rows=1648 width=65)
Hash Cond: (c.sales_rep = sgr.sales_id)
-> Seq Scan on tbl_clients c (cost=11.42..269.24 rows=1648 width=69)
Filter: ((NOT deleted) AND ((sales_rep = ANY ('{5049,5301,5022,5209,5026,5293,5309,5356,5329,5299,5303,5302,5332,5047,1026,5096,5070,5420,1029,1001,5037,5087,1008,5048,5043,5045,5230,5031,5088,5208,1003,5294,5357,5044,1030,5076,5300,5027,5089,5311,5066,5069,5364,1009,5295,1013,5128,5203,1025,5032,5075,1004,5072,5033,5359,5409,5412,5229,1005,5304,5358,1006,5093}'::integer[])) OR (sales_rep IS NULL) OR (hashed SubPlan 1)))
SubPlan 1
-> Seq Scan on tbl_user_visit_clients (cost=0.00..11.41 rows=1 width=4)
Filter: (user_id = 5250)
-> Hash (cost=1.47..1.47 rows=47 width=4)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.47 rows=47 width=4)
-> Hash (cost=221.90..221.90 rows=200 width=144)
-> Subquery Scan on tmp1 (cost=215.90..221.90 rows=200 width=144)
-> HashAggregate (cost=215.90..219.90 rows=200 width=144)
Group Key: p.used_client
-> HashAggregate (cost=201.50..205.10 rows=288 width=58)
Group Key: p.id, ps.id
-> Hash Right Join (cost=180.24..199.34 rows=288 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=179.54..179.54 rows=56 width=26)
-> Hash Join (cost=3.41..179.54 rows=56 width=26)
Hash Cond: (ps.id = p.project_id)
-> Seq Scan on tbl_projects ps (cost=0.00..170.06 rows=1469 width=8)
Filter: (project_status = 3)
-> Hash (cost=2.07..2.07 rows=107 width=22)
-> Seq Scan on tbl_project_used_clients p (cost=0.00..2.07 rows=107 width=22)
-> Hash (cost=173.60..173.60 rows=1 width=44)
-> Subquery Scan on tmp2 (cost=173.56..173.60 rows=1 width=44)
-> GroupAggregate (cost=173.56..173.59 rows=1 width=44)
Group Key: p_1.used_client
-> Sort (cost=173.56..173.57 rows=1 width=14)
Sort Key: p_1.used_client
-> Hash Join (cost=3.41..173.56 rows=1 width=14)
Hash Cond: (ps_1.id = p_1.project_id)
-> Seq Scan on tbl_projects ps_1 (cost=0.00..170.06 rows=20 width=4)
Filter: (project_status = 1)
-> Hash (cost=2.07..2.07 rows=107 width=18)
-> Seq Scan on tbl_project_used_clients p_1 (cost=0.00..2.07 rows=107 width=18)
-> Hash (cost=173.62..173.62 rows=1 width=44)
-> Subquery Scan on tmp3 (cost=173.59..173.62 rows=1 width=44)
-> GroupAggregate (cost=173.59..173.61 rows=1 width=44)
Group Key: p_2.used_client
-> Sort (cost=173.59..173.59 rows=1 width=14)
Sort Key: p_2.used_client
-> Hash Join (cost=3.41..173.58 rows=1 width=14)
Hash Cond: (ps_2.id = p_2.project_id)
-> Seq Scan on tbl_projects ps_2 (cost=0.00..170.06 rows=26 width=4)
Filter: (project_status = 2)
-> Hash (cost=2.07..2.07 rows=107 width=18)
-> Seq Scan on tbl_project_used_clients p_2 (cost=0.00..2.07 rows=107 width=18)
-> Hash (cost=210.07..210.07 rows=107 width=68)
-> Subquery Scan on tmp10 (cost=207.40..210.07 rows=107 width=68)
-> HashAggregate (cost=207.40..209.00 rows=107 width=68)
Group Key: sc.used_client
-> HashAggregate (cost=203.65..204.99 rows=107 width=46)
Group Key: sc.id
-> Hash Right Join (cost=180.24..201.82 rows=366 width=46)
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=179.54..179.54 rows=56 width=14)
-> Hash Join (cost=3.41..179.54 rows=56 width=14)
Hash Cond: (ps_3.id = sc.project_id)
-> Seq Scan on tbl_projects ps_3 (cost=0.00..170.06 rows=1469 width=4)
Filter: (project_status = 3)
-> Hash (cost=2.07..2.07 rows=107 width=18)
-> Seq Scan on tbl_project_used_clients sc (cost=0.00..2.07 rows=107 width=18)
-> Hash (cost=3.55..3.55 rows=47 width=12)
-> Subquery Scan on tmp11 (cost=2.61..3.55 rows=47 width=12)
-> HashAggregate (cost=2.61..3.08 rows=47 width=44)
Group Key: sc_1.used_client
-> Seq Scan on tbl_project_used_clients sc_1 (cost=0.00..2.07 rows=107 width=8)
-> Hash (cost=181.37..181.37 rows=45 width=36)
-> Subquery Scan on tmp12 (cost=180.02..181.37 rows=45 width=36)
-> GroupAggregate (cost=180.02..180.92 rows=45 width=44)
Group Key: puc.used_client
-> Sort (cost=180.02..180.13 rows=45 width=10)
Sort Key: puc.used_client
-> Hash Join (cost=3.26..178.78 rows=45 width=10)
Hash Cond: (p_3.id = puc.project_id)
-> Seq Scan on tbl_projects p_3 (cost=0.00..170.06 rows=1336 width=4)
Filter: (project_status <> 3)
-> Hash (cost=2.07..2.07 rows=95 width=14)
-> Seq Scan on tbl_project_used_clients puc (cost=0.00..2.07 rows=95 width=14)
Filter: (used_client IS NOT NULL)
-> Hash (cost=180.60..180.60 rows=2 width=68)
-> Subquery Scan on tmp13 (cost=180.53..180.60 rows=2 width=68)
-> GroupAggregate (cost=180.53..180.58 rows=2 width=68)
Group Key: puc_1.used_client
-> Sort (cost=180.53..180.54 rows=2 width=16)
Sort Key: puc_1.used_client
-> Hash Join (cost=3.26..180.53 rows=2 width=16)
Hash Cond: (p_4.id = puc_1.project_id)
-> Seq Scan on tbl_projects p_4 (cost=0.00..177.07 rows=46 width=4)
Filter: ((project_status = 1) OR (project_status = 2))
-> Hash (cost=2.07..2.07 rows=95 width=20)
-> Seq Scan on tbl_project_used_clients puc_1 (cost=0.00..2.07 rows=95 width=20)
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)