我有几个表:用户表和记录他们可以采取的各种行动的表(即下载,阅读,测验等)。我正在尝试生成一个列出每个用户的表,以及他们迄今为止所采取的操作数。
SELECT a.user_id, b.action1 + c.action2 + d.action3 AS actions
FROM table_of_applicable_users a
LEFT JOIN
(SELECT DISTINCT e.user_id , COUNT( e.user_id ) AS action1
FROM table_of_actions1 e
JOIN user_records f ON f.user_id = e.user_id
WHERE f.group_id =15
GROUP BY e.user_id )b
LEFT JOIN
(SELECT DISTINCT g.user_id , COUNT( g.user_id ) AS action2
FROM table_of_actions2 g
JOIN user_records h ON h.user_id = g.user_id
WHERE h.company_id =15
GROUP BY g.user_id )c
LEFT JOIN
(SELECT DISTINCT i.user_id , COUNT( i.user_id ) AS action3
FROM table_of_actions3 i
JOIN user_records j ON j.user_id = i.user_id
WHERE j.company_id =15
GROUP BY user_id )d
JOIN user_records z ON z.user_id = a.user_id
WHERE z.group_id =15
GROUP BY a.user_id
我在每个子查询中加入用户记录表,因为我发现它将行数减少了大约一半。但是,查询仍然需要太多时间。我如何进一步优化此查询,或创建返回类似结果的新查询?
P.S。所需的结果格式如下:
user_id number of actions
00001 459
00002 2461, etc.
答案 0 :(得分:0)
我想了解你的要求, 我从我的查询中删除的东西我发现是不必要的。
Select user_id,action1+action2+action3 AS actions from
(
SELECT e.user_id , COUNT( e.user_id ) AS action1
FROM table_of_actions1 e
JOIN user_records f ON f.user_id = e.user_id
WHERE f.group_id =15
GROUP BY e.user_id
union all
SELECT g.user_id , COUNT( g.user_id ) AS action2
FROM table_of_actions2 g
JOIN user_records h ON h.user_id = g.user_id
WHERE h.company_id =15
GROUP BY g.user_id
union all
SELECT i.user_id , COUNT( i.user_id ) AS action3
FROM table_of_actions3 i
JOIN user_records j ON j.user_id = i.user_id
WHERE j.company_id =15
GROUP BY user_id
)t4
答案 1 :(得分:0)
table_of_actions1,table_of_actions2,table_of_actions3和user_records中的userid列是否已编入索引?尝试以下语句
SELECT a.user_id, b.action1 + c.action2 + d.action3 AS actions
FROM table_of_applicable_users a
LEFT JOIN
(SELECT DISTINCT e.user_id , COUNT( e.user_id ) AS action1
FROM table_of_actions1 e
JOIN (SELECT user_id, group_id from user_records where group_id = 15) f ON f.user_id = e.user_id
WHERE f.group_id =15
GROUP BY e.user_id )b
LEFT JOIN
(SELECT DISTINCT g.user_id , COUNT( g.user_id ) AS action2
FROM table_of_actions2 g
JOIN (SELECT user_id, company_id from user_records where company_id= 15) h ON h.user_id = g.user_id
WHERE h.company_id =15
GROUP BY g.user_id )c
LEFT JOIN
(SELECT DISTINCT i.user_id , COUNT( i.user_id ) AS action3
FROM table_of_actions3 i
JOIN (SELECT user_id, company_id from user_records where company_id = 15) j ON j.user_id = i.user_id
WHERE j.company_id =15
GROUP BY user_id )d
JOIN user_records z ON z.user_id = a.user_id
WHERE z.group_id =15
GROUP BY a.user_id
答案 2 :(得分:0)
KumarHarsh很接近。但是需要一些修复:
Select user_id, SUM(actions) AS actions from -- Note: SUM
(
( SELECT e.user_id , COUNT(*) AS actions
FROM table_of_actions1 e
JOIN user_records f ON f.user_id = e.user_id
WHERE f.group_id = 15
GROUP BY e.user_id )
UNION ALL
( SELECT g.user_id , COUNT(*) AS actions
FROM table_of_actions2 g
JOIN user_records h ON h.user_id = g.user_id
WHERE h.company_id = 15
GROUP BY g.user_id )
UNION ALL
( SELECT i.user_id , COUNT(*) AS actions
FROM table_of_actions2 j
JOIN user_records j ON j.user_id = i.user_id
WHERE j.company_id = 15
GROUP BY user_id )
) t4
)
GROUP BY user_id; -- Note: GROUP BY
但是!...由于JOIN
,GROUP BY
可能会为COUNT
提供过多的价值。建议您检查一下,看看它是否得到正确的计数,或者是否有一些夸大的价值:
SELECT e.user_id , COUNT(*) AS action1
FROM table_of_actions1 e
JOIN user_records f ON f.user_id = e.user_id
WHERE f.group_id = 15
GROUP BY e.user_id;
如果它被夸大了,那么我们将不得不更加努力地使您的查询快速和正确。