QUERY PLAN
Aggregate (cost=688.39..688.40 rows=1 width=8)
-> Sort (cost=688.37..688.38 rows=1 width=1525)
Sort Key: (CASE WHEN ((sum(CASE WHEN (p.project_status = 3) THEN p.budget ELSE '0'::numeric END)) IS NULL) THEN '0'::numeric ELSE (sum(CASE WHEN (p.project_status = 3) THEN p.budget ELSE '0'::numeric END)) END) DESC, c.created_at DESC
-> Hash Right Join (cost=607.88..688.36 rows=1 width=1525)
Hash Cond: ((csrr.client_id = c.id) AND (csrr.sales_rep = c.sales_rep))
-> Hash Join (cost=75.90..146.04 rows=1378 width=8)
Hash Cond: (csrr.id = "ANY_subquery".max)
-> Seq Scan on tbl_client_sales_rep_rels csrr (cost=0.00..47.57 rows=2757 width=12)
-> 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)
-> Hash (cost=531.97..531.97 rows=1 width=48)
-> Hash Right Join (cost=509.91..531.97 rows=1 width=48)
Hash Cond: (p.client_id = c.id)
-> HashAggregate (cost=201.38..212.35 rows=878 width=296)
Group Key: p.client_id
-> Seq Scan on tbl_projects p (cost=0.00..188.20 rows=1757 width=13)
Filter: (project_status = ANY ('{1,2,3}'::integer[]))
-> Hash (cost=308.51..308.51 rows=1 width=28)
-> Seq Scan on tbl_clients c (cost=9.67..308.51 rows=1 width=28)
Filter: ((NOT deleted) AND ((lower((name)::text) ~~ '%东方瑜当场发怒,明说无论死伤的是护法仆人、盘山山贼还是覆海殿贼人,都是人命,都是福国子民%'::text) OR (pinyin_shortcut ~~ '%东方瑜当场发怒,明说无论死伤的是护法仆人、盘山山贼还是覆海殿贼人,都是人命,都是福国子民%'::text)) AND ((sales_rep = ANY ('{1001,5049,5301,5022,5209,5026,5293,5309,5356,5329,5299,5303,5302,5332,5047,1026,5096,5070,5448,5420,1029,5037,5087,1008,5048,5043,5045,5230,5031,5088,5208,1003,5467,5294,5357,5044,1030,5076,5300,5027,5089,5311,5066,5069,5364,1009,5295,1013,5128,5203,1025,5032,5075,5476,1004,5072,5033,5359,5412,5409,5229,1005,5304,5449,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..9.66 rows=1 width=4)
Filter: (user_id = 1001)