2012-10-26 49 views
0

我有2個表格:twitter_followerstwitter_friends。兩個表都有許多列(id,user_id,twitter_id等)。對於單個user_id,兩個表中的行數都可以超過100000條記錄。基於其他表格獲取記錄的最佳方法

我要檢索以下列方式從usertwitter_friends記錄:

SELECT * 
FROM twitter_friends 
WHERE user_id=1 
AND twitter_id NOT IN (SELECT twitter_id FROM twitter_followers WHERE user_id=1) 

該查詢是正常的小數據集,但任何一個可以幫助我獲得大量無數據的(最好是在幾秒鐘)?

+2

嘗試用'LEFT OUTER JOIN' – diEcho

+0

沒有成功但任何一個能幫助我嗎? –

回答

0

MySql的子查詢性能非常糟糕。我會建議使用JOIN語句。

像:

Select Friends.*, Followers.twitter_id 
from twitter_friends as Friends 
LEFT JOIN twitter_followers as Followers 
on Friends.USER_ID = Followers.USER_ID 
where friends.user_id=1 AND followers.twitter_id is null; 
+0

此答案無效。需要無限的時間。 –

相關問題