【发布时间】:2026-02-14 06:30:01
【问题描述】:
我已经在 2 个具有完全相同结构的数据库中测试了以下查询,在第一个具有 4M 条目的数据库中,它在 33 秒内返回了结果。第二个表有 2900 万行,我执行查询已经 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 倍,这正常吗?
欢迎任何其他cmets!
如果我的英语不清楚,请告诉我,我会尝试用不同的方式表达自己。
非常感谢您的帮助,
附加信息:
从我使用的变量来看,其中 3 个是索引:id_hand、id_player、position。主键是 (id_hand, id_player)。该表共有 129 列和 6 个索引。
我还在两个表中都运行了 EXPLAIN,得到了不同的结果。这俩 结果在 gdocs 电子表格上: https://spreadsheets.google.com/ccc?key=tGxqxVNzHYznb1VVjtKyAuw&authkey=CJ-BiYkN&authkey=CJ-BiYkN#gid=0
【问题讨论】:
-
对您的数据模型和索引一无所知,几乎不可能为您提供帮助。您能否也向我们展示 EXPLAIN 的结果?
-
弗兰克,当我回到家时,我会得到这个信息并发布它。谢谢。
-
你在那张桌子上做定期吸尘吗?
-
我前几天抽空了。
-
如果有很多删除/更新,那么“几天前”是不够的。我希望你没有禁用自动真空!
标签: sql optimization postgresql query-optimization