I've tested the following query in 2 databases with exactly the same structure, in the first one, with has 4M entries, it returned me the result in 33 seconds. The second table has 29M rows and it's been 16 hours since I executed the query and I haven't got a return yet.
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; What can I do to make this query execute faster ?
Is it likely that the table is corrupted or something like that ? One table is only 7~8 times bigger than the other, but the it is taking 15000x more time to process, is this normal ?
Any other comments are welcome !
If my english is not clear, just let me know that I'll try to express myself in a different way.
Thank you very much for any help,
ADDITIONAL INFO:
From the variables that I am using, 3 of them are indexes: id_hand,id_player,position. The primary key is (id_hand, id_player). The table has 129 columns and 6 indexes in total.
I also ran EXPLAIN in both tables and I got different results. Both the results are on a gdocs spreadsheet: https://spreadsheets.google.com/ccc?key=tGxqxVNzHYznb1VVjtKyAuw&authkey=CJ-BiYkN&authkey=CJ-BiYkN#gid=0