我測試了2個數據庫中具有完全相同結構的以下查詢,第一個具有4M條目,它在33秒內返回了結果。第二個表有29M行,自從我執行查詢後已經過了16個小時,我還沒有收到回報。查詢優化
SELECT sbvpip*4 as smallbvpip,btnvpip*4 as buttonvpip, sum(amt_won)*400/count(*) AS winrate, count(*) as count
FROM holdem_hand_player_statistics
JOIN (
SELECT id_player AS pid2, id_hand AS hid, sbvpip
FROM holdem_hand_player_statistics
JOIN (
SELECT id_player AS pid, ROUND(avg(flg_vpip::int)*25) AS sbvpip
FROM holdem_hand_player_statistics
WHERE position = 8 AND cnt_players = 6
GROUP BY id_player
) AS auxtable
ON pid = id_player
WHERE position = 8 AND cnt_players = 6
) AS auxtable2
ON hid = id_hand
JOIN (
SELECT id_player AS pid4, id_hand AS hid2, btnvpip
FROM holdem_hand_player_statistics
JOIN (
SELECT id_player AS pid3, ROUND(avg(flg_vpip::int)*25) AS btnvpip
FROM holdem_hand_player_statistics
WHERE position = 0 AND cnt_players = 6
GROUP BY id_player
) AS auxtable3
ON pid3 = id_player
WHERE position = 0 AND cnt_players = 6
) AS auxtable4
ON hid2 = id_hand
WHERE POSITION = 0 and cnt_players = 6
GROUP BY sbvpip,btnvpip
ORDER BY 1,2;
我能做些什麼,使這個查詢執行得更快?
表有可能是損壞或類似的東西?一張桌子只比另一張桌子大7〜8倍,但它需要15000倍的時間來處理,這是正常的嗎?
歡迎任何其他意見!
如果我的英語不清楚,只要告訴我,我會試着用不同的方式表達自己。
非常感謝您的幫助,
附加信息:
從我使用,其中3人爲索引變量:id_hand,id_player,位置。主鍵是(id_hand,id_player)。該表共有129列和6個索引。
我也在兩個表中運行了EXPLAIN,得到了不同的結果。無論是 結果是在gdocs電子表格: https://spreadsheets.google.com/ccc?key=tGxqxVNzHYznb1VVjtKyAuw&authkey=CJ-BiYkN&authkey=CJ-BiYkN#gid=0
沒有關於你的數據模型和索引的任何想法,這幾乎是不可能幫助你。你能否向我們展示EXPLAIN的結果? – 2010-12-07 20:20:28
弗蘭克,當我到家時,我會得到這些信息併發布。謝謝。 – joaoavf 2010-12-07 21:48:26
你在那張桌子上經常抽真空嗎? – 2010-12-08 08:14:03