2014-03-19 30 views
1

我試圖獲得每個帖子和評論的清單計數。mysql:簡單的子查詢非常緩慢,因爲有3000萬行

SELECT 
    theposts.id, 
    theposts.name, 
    (SELECT COUNT(*) FROM thecomments WHERE thecomments.post_id = theposts.id) AS comments 
FROM theposts 

問題是:我有20K的帖子和30百萬的評論。該查詢非常慢。

如果我使用LIMIT 5,它在大約40秒內工作正常。但我需要獲得20k個職位的完整列表。

有關如何加快或調試此查詢的任何提示?

服務器正在我的Macbook 8gb RAM中運行。

回答

2

我能想到的最好方法是創建索引。你thecomments(post_id)需要一個指標:

create index thecomments_postid on thecomments(post_id); 

這應該更改查詢計劃只是一個索引掃描和去很快。

我也認爲這將是比使用group by,這是其他可能更快:

SELECT theposts.id, theposts.name, COUNT(*) as comment 
FROM theposts join 
    thecomments 
    on thecomments.post_id = theposts.id 
GROUP BY theposts.id; 
+0

這些方法是免費的。同時使用! –

+0

索引查詢花了1分鐘,選擇查詢耗時10秒。非常感謝! – user3175226

1

首先是檢查是否有索引,在適當情況下。這通常是最常見的問題。

另一個問題是,您可能會運行20,000個子查詢,具體取決於您的查詢分析引擎的智能程度。

SELECT 
    theposts.id   is id, 
    theposts.name   as name, 
    count(thecomments.id) as comments 
FROM 
    theposts, thecomments 
WHERE 
    thecomments.post_id = theposts.id 
GROUP BY thepost.id, theposts.name 

(這是隱式的連接語法,你也可以使用明確的:

您只需通過分組你行,如用(取決於您的架構)實現一個查詢相同的結果join)。

+0

你在這裏指出了這個問題,但我贊成提到索引並有明確聯結的問題。 –

+0

感謝您的問題解釋!我試着運行查詢2分鐘,沒有迴應。用索引解決。謝謝! – user3175226

1

用連接試試這個,你不需要子查詢。

SELECT 
    theposts.id, 
    theposts.name, 
    COUNT(*) comments 
FROM thecomments 
INNER JOIN theposts ON thecomments.post_id = theposts.id 
GROUP BY theposts.id 
+0

我試着運行查詢2分鐘,沒有迴應。用索引解決。謝謝! – user3175226