2013-07-10 75 views
0

我有低於查詢的從多個表中提取數據創建一個更新:加速MySQL Query inc子查詢連接?

UPDATE en_inter.subscribers_data AS sd 
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid 
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns 
FROM email.papr_down pd1 
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id 
INNER JOIN email.papr_subj ps on ps.id = pd2.subject 
INNER JOIN email.papr_exam pe on pe.id = pd2.exam 
INNER JOIN email.papr_levl pl on pl.id = pd2.level 
WHERE pd2.exam = 1 
and pd2.level = 4 
GROUP BY email_address 
) AS downs ON downs.email_address = ls.emailaddress 
SET sd.data = ifnull(downs.NumDowns,1) 
WHERE sd.fieldid = 33; 

它工作正常,但是當有大量的papr_down記錄則需要年齡處理。關於如何優化的任何想法?

+0

http://s22.postimg.org/icqwci2jl/screeny.jpg – JoshGeake

回答

0

我認爲emailAddress之間的連接是問題在這裏,你可以嘗試與Id的連接。 如果您向我們提供以下查詢::

EXPLAIN Select * from 
en_inter.subscribers_data AS sd 
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid 

LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns 
FROM email.papr_down pd1 
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id 
INNER JOIN email.papr_subj ps on ps.id = pd2.subject 
INNER JOIN email.papr_exam pe on pe.id = pd2.exam 
INNER JOIN email.papr_levl pl on pl.id = pd2.level 
WHERE pd2.exam = 1 
and pd2.level = 4 
GROUP BY email_address 
) AS downs ON downs.email_address = ls.emailaddress 
WHERE sd.fieldid = 33 
+0

好的,這裏的截圖 - [鏈接] http://s22.postimg.org/icqwci2jl/screeny.jpg [/ link] – JoshGeake

+0

btw是的,它是在電子郵件加入時,服務器將返回<1s – JoshGeake

+0

中的選擇內容從我的系統無法訪問該屏幕截圖的鏈接,如果您可以將屏幕截圖與問題粘貼在一起,則效果會更好 –

0

的屏幕截圖,據我所知,我們應該僅適用於在SELECT條款預設列和其他使用聯接聯接我們應該實現使用WHERE條款

請嘗試以下查詢:

UPDATE en_inter.subscribers_data AS sd 
inner join en_inter.list_subscribers AS ls 
on sd.subscriberid = ls.subscriberid 
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns 
FROM email.papr_down AS pd1 
INNER JOIN email.papr_data AS pd2 on pd1.paper_id = pd2.id 
WHERE 
email.papr_exam.id in (select exam from email.papr_data where exam = 1) 
AND 
email.papr_levl.id in (select level from email.papr_data where level = 4) 
AND 
email.papr_subj.id in (select subject from email.papr_data) 

GROUP BY email_address 
) AS downs ON downs.email_address = ls.emailaddress 
SET sd.data = ifnull(downs.NumDowns,1) 
WHERE sd.fieldid = 33; 

我不能在我的計算機上執行此,因爲我沒有架構