我正在使用以下查询,该查询非常慢(大约需要30-45分钟)并且有时会卡住,并且会产生500错误,这对应用程序来说并不好......
SELECT id, account_number, email, referred_by, dnt,
(SELECT count(*)
FROM tbl.registrations t2
WHERE t2.referred_by = t1.account_number
) AS level1,
(SELECT count(*)
FROM tbl.registrations t2 join
tbl.registrations t3
on t3.referred_by = t2.account_number
WHERE t2.referred_by = t1.account_number
) AS level2,
(SELECT count(*)
FROM tbl.registrations t2 join
tbl.registrations t3
on t3.referred_by = t2.account_number join
tbl.registrations t4
on t4.referred_by = t3.account_number
WHERE t2.referred_by = t1.account_number
) AS level3
FROM tbl.registrations t1 GROUP BY id;
我们在此表和db结构中有大约35000行,如下所示......
| id | account_number | referred_by |
+----+-----------------+--------------+
| 1 | ac01 | 5 |
+----+-----------------+--------------+
| 2 | ac02 | 5 |
+----+-----------------+--------------+
| 3 | ac03 | 4 |
+----+-----------------+--------------+
计算level1,level2和level3如下......
total_referred - total number of members referred by account_number
total_reffered2 - total number of members that THEY(all accounts referred by account_number) all referred
total_reffered3 - total number of members that referred by all members of total_reffered2
例如
如果每个成员总共有10个推荐人,那么......
level1 = 10
level2 = 100
level3 = 1000
请检查我做错了什么?请帮我改写这个查询或优化,顺便说一下我已经尝试过添加INDEX了。感谢
答案 0 :(得分:0)
以下是SQLFiddle的示例。我输入了一个包含相应ID的40个条目的列表,这些ID引用它们显示以下层次结构...
(hierarchy for 1) (hierarchy for 2) (hierarchy for 3)
1 2 3
4 13 21
5 14 22
6 15 23
31 16 39
32 17 40
7 18 24
8 35 25
9 36 26
10 37 27
11 38 28
33 19 29
34 20 30
12
SQLFiddle查询根据引用引用的任何可能创建3列。如果ID没有ID引用的帐户,则它们没有值,需要使用COALESCE解决以防止空值。
select r.*,
coalesce( presum.lvl1Cnt, 0 ) as Lvl1Total,
coalesce( presum.lvl1cnt + presum.lvl2cnt, 0 ) as Lvl2Total,
coalesce( presum.lvl1cnt + presum.lvl2cnt + presum.lvl3cnt, 0 ) as Lvl3Total
from
registrations r
LEFT JOIN
( SELECT
r1.referred_by,
count(distinct r1.id) as Lvl1Cnt,
count(distinct r2.id) as Lvl2Cnt,
count(distinct r3.id) as Lvl3Cnt
from
registrations r1
LEFT JOIN registrations r2
ON r1.id = r2.referred_by
LEFT JOIN registrations r3
ON r2.id = r3.referred_by
group by
r1.referred_by ) presum
ON r.id = presum.referred_by
presum内部查询全面完成一次。我只是确保你在(refer_by,id)上的表上有索引以帮助通过refer_by ID值来优化分组