mysql在同一个选项卡上进行计数和求和的复杂查询
我正在使用下面的查询,它非常慢(大约需要30-45分钟),有时会卡住,生成500个错误,这对应用程序不好mysql在同一个选项卡上进行计数和求和的复杂查询,mysql,sql,Mysql,Sql,我正在使用下面的查询,它非常慢(大约需要30-45分钟),有时会卡住,生成500个错误,这对应用程序不好 SELECT id, account_number, email, referred_by, dnt, (SELECT count(*) FROM tbl.registrations t2 WHERE t2.refe
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;
这个表中大约有35000行,数据库结构如下
| id | account_number | referred_by |
+----+-----------------+--------------+
| 1 | ac01 | 5 |
+----+-----------------+--------------+
| 2 | ac02 | 5 |
+----+-----------------+--------------+
| 3 | ac03 | 4 |
+----+-----------------+--------------+
1级、2级和3级的计算如下
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
(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
比如说
如果每个会员都有10次推荐,那么
level1 = 10
level2 = 100
level3 = 1000
请检查我做错了什么?请帮我重写这个查询或优化,顺便说一句,我已经尝试添加索引了。谢谢这是一个via的示例。我输入了一个包含40个条目的列表,其中对应的ID引用了这些条目,显示了以下的层次结构
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
(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内部查询在整个系统中执行一次。我只想确保您在(refered_,id)上的表上有和索引,以帮助优化按id值refered_的分组如果
id
在您的注册表中是唯一的,那么groupby
是不必要的。尝试删除它,看看它对性能有何影响。@GordonLinoff没什么我已经尝试过了,什么都没发生!此查询非常慢,并引发超时错误您是否尝试对语句运行EXPLAIN?只是想知道发生了什么(查询优化器是如何处理问题的),以及在哪里搜索优化。@Zagor23对不起,我没有尝试过,你能帮我做吗?@Zagor23要解释的输出吗