优化 MySQL 中的查询- 连接和子查询?

Optimizing query in MySQL- joins and subqueries?

我有几个 tables:一个 table 用户,tables 记录他们可以采取的各种行动(即下载、阅读、测验等)。我正在尝试生成一个 table 列表,其中列出了每个用户,以及他们迄今为止采取的操作数量。

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

我在每个子查询中加入用户记录 table,因为我发现它减少了大约一半的行数。但是,查询仍然花费太多时间。我该如何进一步优化此查询,或创建一个 returns 相似结果的新查询?

p.s。所需的结果格式如下:

user_id    number of actions
00001      459
00002      2461, etc.

我正在尝试了解您的要求, 我发现从查询中删除的内容是不必要的。

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

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

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

但是!...由于 JOINGROUP 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;

如果它被夸大了,那么我们将不得不更加努力地使您的查询既快速正确。