[英]How do I make this personal best highscore MySQL query more efficient?
我有一個游戲排行榜高分數據庫,目前擁有約30.000個條目,下面的查詢查找特定游戲和玩家的個人最佳高分,但執行該過程大約需要60秒 。 我在想應該有一種更有效的方法來執行此操作,也許使用復合索引,但是那是哪種呢?
SELECT name, score, date, version, mode, attempts, time, id
FROM highscore h1
WHERE score = (
SELECT MAX( score )
FROM highscore h2
WHERE name = h1.name && gamename = "asteroids" && name = "bob"
)
我目前有以下指標:
id
score
name
name-gamename (composite)
任何幫助是極大的贊賞!
我將使用標准SQL語法編寫查詢:
SELECT name, score, date, version, mode, attempts, time, id
FROM highscore h1
WHERE score = (SELECT MAX( score )
FROM highscore h2
WHERE h2.name = h1.name AND h2.gamename = 'asteroids' AND h2.name = 'bob'
);
為此,您需要一個綜合索引: highscore(name, gamename, score)
。
如果你只是在尋找一個行(即使有關系),那么這可能是凌晨快一點:
SELECT name, score, date, version, mode, attempts, time, id
FROM highscore h
WHERE h.gamename = 'asteroids' AND h.name = 'bob'
ORDER BY score DESC
LIMIT 1;
適當的索引是highscore(name, gamename, score)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.