简体   繁体   中英

Optimizing query in MySQL- joins and subqueries?

I have several tables: a table of users, and tables recording various actions they can take (ie downloading, reading, quiz, etc.). I am trying to produce a table listing each individual user, and the number of actions they have taken thus far.

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

I join the user records table in each sub-query because I found it reduces the number of rows by approximately half. However, the query still takes far too much time. How might I further optimize this query, or create a new query that returns similar results?

ps The desired format for the results is as follows:

user_id    number of actions
00001      459
00002      2461, etc.

I am trying to understand your requirement, Things that i remove from my query i found to be uneccessary .

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

Is userid column in table_of_actions1, table_of_actions2, table_of_actions3 and user_records is indexed ?. Try the following statement once

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 was close. But a couple of fixes are needed:

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

But!... Because of the JOIN , the GROUP BY may give an excessive value for the COUNT . Recommend you check this to see if it gets the right count, or some inflated value:

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;

If it is inflated, then we will have to work a lot harder to make your query both fast and correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM