因此,我有一個包含500萬條記錄的數據庫,它存儲了問答網站的信息。該結構是...簡單的MySQL查詢需要兩個多小時
question, qid, quserid, answer, auserid;
(QID是問題的id號)
我試圖找到所有具有特定用戶不回答問題,並通過排序它的用戶未被特定用戶回答的問題數量。這是我的查詢:
SELECT quserid, COUNT(quserid)
FROM `qanda`
WHERE qid NOT IN (SELECT qid FROM `qanda` WHERE auserid='myusername' GROUP BY qid)
GROUP BY quserid
ORDER BY COUNT(quserid) DESC
LIMIT 0,1000;
問題:它超過2小時,時鐘仍在滴答滴答!任何人都知道如何加快這個數據庫?我認爲數據庫中存在一個錯誤,通常最多隻需30秒就可以完成一個簡單查詢,所以這有點荒謬。任何人都知道任何調整?可能是對配置文件或其他東西的簡單更改?
..........
下面是我只是複製並粘貼數據庫中的某些數據。對不起格式。
you could have any one person in the entire wor... greendaystud ive got the person i want...its great...because sh... •glitter•rock• 191437 If you could have any one person in the entire wor... just~another~slave2tears i already got em
•glitter•rock• 191437 If you could have any one person in the entire wor... korn_chick2007 matt or chris... i have feelings for them
•glitter•rock• 189555 why are you so sexy?
just~another~slave2tears my b/f says i am...i dun tink so tho
•glitter•rock• 189555 why are you so sexy?
korn_chick2007 im not
•glitter•rock• 189555 why are you so sexy?
MyKool-AidsSexy i dont think i am
†brokengirl† 115228 If you are supposed to expect the unexpected,
doe... death-tone yip
†brokengirl† 115228 If you are supposed to expect the unexpected,
doe... _doieverknowwhoiam_ you know whats weird? my friend sandy says that a ...
†brokengirl† 115228 If you are supposed to expect the unexpected,
doe... Cute_Physco_kitty Pretty much..
†brokengirl† 115228 If you are supposed to expect the unexpected,
doe... Leslie02 WHAT! OK, now im confused!
†brokengirl† 114995 Why does my listerine taste like sausage this
mor... death-tone what's listerine?
†brokengirl† 114995 Why does my listerine taste like sausage this
mor... _doieverknowwhoiam_ i don't know, and maybe it's jut me bu...
†brokengirl† 114995 Why does my listerine taste like sausage this
mor... darksunofdeath How old is the listerine pack?
†brokengirl† 114995 Why does my listerine taste like sausage this
mor... Cute_Physco_kitty uhh... New brand of Listerine?
†brokengirl† 114995 Why does my listerine taste like sausage this
mor... Leslie02 did you have sausage for breakfast? †brokengirl† 104305 What should I name my pinky toe on my left
foot?¿... death-tone "Pinkytoe"
而且,預期的輸出,使用便捷的列標題...
Questioner User ID | Number of questions asked by the Questioner that were unanswered by 'myuserid'
Greenbay Packer | 6
DollyDoll | 63
PsychoticPokemon | 62
HelloKitty | 61
GreenDayFan | 60
...
IDontAskManyQuestion | 2<br>
WhatsAQuestion? | 1<br>
而這裏的EXPLAIN輸出
> mysql-> EXPLAIN
> ->
> -> SELECT quserid, COUNT(quserID)
> -> FROM `qanda`
> -> WHERE qid NOT IN (SELECT qid FROM `qanda` WHERE auserid='boxocereal' GROU P BY qid)
> -> GROUP BY quserid
> -> ORDER BY COUNT(quserid) DESC
> -> LIMIT 0,1000;
>
+----+--------------------+-------+------+---------------+------+---------+-----
> -+---------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len |
> ref | rows | Extra |
> +----+--------------------+-------+------+---------------+------+---------+-----
> -+---------+----------------------------------------------+ | 1 | PRIMARY | qanda | ALL | NULL | NULL | NULL |
> NULL | 3167995 | Using where; Using temporary; Using filesort | | 2
> | DEPENDENT SUBQUERY | qanda | ALL | NULL | NULL | NULL |
> NULL | 3167995 | Using where; Using temporary; Using filesort |
> +----+--------------------+-------+------+---------------+------+---------+-----
> -+---------+----------------------------------------------+ 2 rows in set (0.02 sec)
>
> mysql->
你有什麼指標? –
MySQL是不聰明足以緩存依賴子查詢的結果,它對結果集的每一行都運行一次, – Interrobang
@Interrobang,不可能是真的......真的嗎?? – McGarnagle