簡體   English   中英

在MySQL中優化查詢-聯接和子查詢?

[英]Optimizing query in MySQL- joins and subqueries?

我有幾個表:一個用戶表和一個記錄他們可以執行的各種操作(即下載,閱讀,測驗等)的表。 我正在嘗試生成一個表格,列出每個用戶以及他們迄今為止采取的行動數量。

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

我在每個子查詢中加入了用戶記錄表,因為我發現它使行數減少了大約一半。 但是,查詢仍然花費太多時間。 如何進一步優化此查詢,或創建一個返回相似結果的新查詢?

ps結果的所需格式如下:

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;

如果它膨脹了,那么我們將不得不付出更多的努力才能使您的查詢既快速正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM