的單獨列的值我有一個查詢它看起來像:的MySQL獲得最後一個記錄
SELECT max(sp.id) as max_id, p.name as player, max(update_time) as last_seen, min(login_time) as first_seen, s.name as last_server,
sum(sp.play_time) as ontime_total,
sum(case when login_time > NOW() - INTERVAL 1 DAY then sp.play_time end) as ontime_day,
sum(case when login_time > NOW() - INTERVAL 7 DAY then sp.play_time end) as ontime_week,
sum(case when login_time > NOW() - INTERVAL 1 MONTH then sp.play_time end) as ontime_month
FROM session_player sp
INNER JOIN players p ON p.id=sp.player_id
INNER JOIN server s ON s.id=sp.server_id
WHERE p.name = ?
結果:
問題: Node22不是上次服務器。我正在努力找到一種方法來獲取此查詢中最後一條記錄的服務器。如果可能的話,你將如何解決這個問題,而不需要運行第二個查詢。
(該查詢已經發生取決於用戶2-3S秒,如果可能的話,我想,以避免任何開銷,如果你看到的性能優化的可能性我將不勝感激任何東西。)
這會工作,但其性能可以猜想(4-5s):
SELECT
MAX(sp.id) AS max_id, p.name AS player, MAX(update_time) AS last_seen, MIN(login_time) AS first_seen,
SUM(sp.play_time) AS ontime_total,
SUM(CASE WHEN login_time > NOW() - INTERVAL 1 DAY THEN sp.play_time END) AS ontime_day,
SUM(CASE WHEN login_time > NOW() - INTERVAL 7 DAY THEN sp.play_time END) AS ontime_week,
SUM(CASE WHEN login_time > NOW() - INTERVAL 1 MONTH THEN sp.play_time END) AS ontime_month,
(SELECT s.name
FROM session_player sp
JOIN players p ON p.id=sp.player_id
JOIN server s ON s.id=sp.server_id
WHERE p.name = ?
ORDER BY sp.id DESC
LIMIT 1
) as last_server
FROM session_player sp
INNER JOIN players p ON p.id = sp.player_id
INNER JOIN server s ON s.id = sp.server_id
WHERE p.name = ?
你是如何定義「最後的記錄」? –
關於性能 - 請確保字段player.id,server.id,session_player.player_id和session_player.server_id都被索引,並且(對於myisam至少)它們是索引中的第一項。 – Giles
@ session_player表上的@MarcusAdams max id – user2693017