我還在學習MySQL。我可能會犯一個非常基本的錯誤,我準備在這裏訓練...正在執行count()計算減慢我的mysql查詢?
這個查詢試圖做的是從我們的網站上選擇最高的成員數量的書和食譜評論他們做了。
我做在SQL查詢本身總的計算。查詢速度很慢(9秒),並且絕對不會擴展,因爲我們目前只有400個成員和幾千條評論,並且它的增長速度非常快。
我相信它做一個全表掃描,在這裏,而且在計算減緩下來,但我不知道的另一種方式來做到這一點,也許需要一些智慧。
這裏的SQL語句:
SELECT users.*, COUNT(DISTINCT bookshelf.ID) AS titles, COUNT(DISTINCT book_reviews.ID) as bookreviews, COUNT(DISTINCT recipe_reviews.ID) AS numreviews, COUNT(DISTINCT book_reviews.ID) + COUNT(DISTINCT recipe_reviews.ID) as reviewtotal
FROM users
LEFT OUTER JOIN recipe_reviews ON recipe_reviews.user_id = users.ID
LEFT OUTER JOIN book_reviews ON book_reviews.user_id = users.ID
LEFT OUTER JOIN bookshelf ON users.ID = bookshelf.user_id
GROUP BY users.ID
ORDER BY reviewtotal DESC
LIMIT 8
這裏的解釋是:
+----+-------------+----------------+-------+-------------------+-------------------+---------+---------------------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+-------+-------------------+-------------------+---------+---------------------+------+---------------------------------+
| 1 | SIMPLE | users | index | NULL | PRIMARY | 4 | NULL | 414 | Using temporary; Using filesort |
| 1 | SIMPLE | recipe_reviews | ref | recipe_reviews_fk | recipe_reviews_fk | 5 | users.ID | 12 | |
| 1 | SIMPLE | book_reviews | ref | user_id | user_id | 5 | users.ID | 4 | |
| 1 | SIMPLE | bookshelf | ref | recipe_reviews_fk | recipe_reviews_fk | 5 | users.ID | 13 | |
+----+-------------+----------------+-------+-------------------+-------------------+---------+---------------------+------+---------------------------------+
UPDATE &解決:
我意識到,和@recursive證實,該查詢問題的根源。我從中得到笛卡爾產品。我重寫它作爲一個子查詢系列和最終工作代碼是在這裏:
SELECT *, bookreviews + recipereviews AS totalreviews
FROM (SELECT users.*,
(SELECT count(*) FROM bookshelf WHERE bookshelf.user_id = users.ID) as titles,
(SELECT count(*) FROM book_reviews WHERE book_reviews.user_id = users.ID) as bookreviews,
(SELECT count(*) FROM recipe_reviews WHERE recipe_reviews.user_id = users.ID) as recipereviews
FROM users) q
這給我以毫秒爲單位的結果。還有一些方法可以用JOIN做到這一點。如果你想跟上這一點,請參閱How to add together the results of several subqueries?。
我已經標記了遞歸的答案是正確的,雖然他的初步答案不是解決方案,但他將它釘在下面的註釋中。 – mandel 2010-01-13 16:48:40