我對我的數據庫使用MySQL,並且在數據庫端做了一些處理,以使我的應用程序更加輕鬆。獲得性能並執行快速sql查詢的最佳方法?
我以前的查詢速度非常快,直到最近我的數據庫有很多數據,查詢速度非常非常慢。
我的應用程序主要做統計,並有很多相關的數據庫來獲取數據。
下面是一個例子:
tbl_game
+-------------------------------------+ | id | winner | duration| endedAt | |--------+--------+---------+---------| | 1 | 1 | 1200 |timestamp| | 2 | 0 | 1200 |timestamp| | 3 | 1 | 1200 |timestamp| | 4 | 1 | 1200 |timestamp| +-------------------------------------+
贏家是誰贏得了比賽 時間球隊0或1的秒數場比賽耗時
tbl_game_player
+-------------------------------------------------+ | gameId | playerId | playerSlot | frags | deaths | |--------+----------+------------+-------+--------| | 1 | 100 | 1 | 24 | 50 | | 1 | 150 | 2 | 32 | 52 | | 1 | 101 | 3 | 26 | 62 | | 1 | 109 | 4 | 48 | 13 | | 1 | 123 | 5 | 24 | 52 | | 1 | 135 | 6 | 30 | 30 | | 1 | 166 | 7 | 28 | 48 | | 1 | 178 | 8 | 52 | 96 | | 1 | 190 | 9 | 12 | 75 | | 1 | 106 | 10 | 68 | 25 | +-------------------------------------------------+
該細節僅限於第一場比賽的ID爲1 1場比賽有10個球員插槽其中插槽1-5 =隊0和6-10 =隊1 我的真實表中有更多的細節,這只是給一個概述。
所以我需要計算所有遊戲中每個玩家的統計數據。我創建了一個視圖來實現這一點,並且當我有少量數據時它工作正常。
下面是一個例子:
+--------------------------------------------------------------------------+ | gameId | playerId | frags | deaths | actions | team | percent | isWinner | |--------+----------+-------+--------+---------+------+---------+----------|
行動=斷枝+死亡 百分比=(動作/總和(玩家在同一隊的動作))* 100 團隊使用playerSlot在1,2-計算,3,4,5或6,7,8,9,10 isWinner is the team by winner and winner
這只是1個算法,我還有很多其他的要執行。我的數據庫是1千萬條記錄,查詢速度很慢。
這裏是上面的查詢:
SELECT
tgp.gameId,
tgp.playerId,
tgp.frags,
tgp.deaths,
tgp.frags + tgp.deaths AS actions,
IF(playerSlot in (1,2,3,4,5), 0, 1) AS team,
((SELECT actions)/tgpx.totalActions) * 100 AS percent,
IF((SELECT team) = tg.winner, 1, 0) AS isWinner
FROM tbl_game_player tgp
INNER JOIN tbl_game tg on tgp.gameId = tg.id
INNER JOIN (
SELECT
gameId,
SUM(frags) AS totalFrags,
SUM(deaths) AS totalDeaths,
SUM(frags) + SUM(deaths) as totalActions,
IF(playerSlot in (1,2,3,4,5), 0, 1) as team
FROM tbl_game_player
GROUP BY gameId, team
) tgpx on tgp.gameId = tgpx.gameId and team = tgpx.team
上述查詢所說的「explain select ....」是什麼意思? –
通常應該說些什麼?有2個主要的,1個派生的和1個依賴的子查詢。我不知道如何閱讀'解釋選擇...'數據。你可以解釋嗎? – user2707590
只是將它們複製到問題中。 –