おそらく以下のクエリからわかるように、私はMySQLの専門家ではありませんが、クエリを取得して必要な結果を得ることができましたが、非常に遅いようです:
SELECT
bouts.bout_id,
bouts.bout_date,
bouts.winner,
bouts.result,
bouts.ended_in_round,
bouts.total_rounds,
bouts.verified AS verified,
CONCAT(opponent.first_name, ' ', opponent.last_name) AS opponent_name,
opponent.last_name AS opponent_lastname,
opponent.id AS opponent_id,
IF(opponent.id = bouts.boxer_1, bouts.boxer_1_weight, bouts.boxer_2_weight) AS opp_weight,
IF(opponent.id = bouts.boxer_1, bouts.boxer_2_weight, bouts.boxer_1_weight) AS fighter_weight,
SUM(opp_bouts.ended_in_round) AS opp_total_rounds_boxed,
SUM(IF(opp_bouts.winner = -1,1,0)) AS opp_draws,
SUM(IF(opp_bouts.winner = opponent.id,1,0)) AS opp_wins,
COUNT(opp_bouts.bout_id) AS opp_fights,
IF(opponent.id = bouts.boxer_1, bouts.bxr2_power_points, bouts.bxr1_power_points) AS boxer_power,
IF(opponent.id = bouts.boxer_1, bouts.bxr2_chin_points, bouts.bxr1_chin_points) AS boxer_chin,
IF(opponent.id = bouts.boxer_1, bouts.bxr2_boxer_points, bouts.bxr1_boxer_points) AS boxer_boxer,
SUM(IF(opponent.id = opp_bouts.boxer_1, opp_bouts.bxr1_power_points, opp_bouts.bxr2_power_points)) AS opp_power,
SUM(IF(opponent.id = opp_bouts.boxer_1, opp_bouts.bxr1_chin_points, opp_bouts.bxr2_chin_points)) AS opp_chin,
SUM(IF(opponent.id = opp_bouts.boxer_1, opp_bouts.bxr1_boxer_points, opp_bouts.bxr2_boxer_points)) AS opp_boxer,
SUM(IF(opponent.id = opp_bouts.boxer_1, opp_bouts.bxr1_exp_points, opp_bouts.bxr2_exp_points)) AS opp_experience,
MAX(IF(opp_bouts.verified = 1, opp_bouts.bout_date, 0)) AS verified_up_to,
MAX(opp_bouts.bout_date) AS opp_last_bout
FROM
bouts
LEFT JOIN boxers opponent ON opponent.id=IF(bouts.boxer_2 = '" . $boxer_id . "', bouts.boxer_1, bouts.boxer_2)
LEFT JOIN bouts opp_bouts ON (opp_bouts.boxer_1=opponent.id OR opp_bouts.boxer_2=opponent.id) AND opp_bouts.bout_date < bouts.bout_date
WHERE bouts.bout_date < '" . $to_date . "' AND (bouts.boxer_1 = '" . $boxer_id . "' OR bouts.boxer_2 = '" . $boxer_id . "')
GROUP BY bouts.bout_id
ORDER BY bouts.bout_date DESC
bouts
約boxers
11,570 行あり、約 86,370 行あり、現在、実行に 0.6500 ~ 0.7500 秒かかります。と に主キーがboxers.id
ありbouts.bout_id
、 と にインデックスがありますbouts.boxer_1
がbouts.boxer_2
、インデックスの使用に関する知識もあまりありません。
クエリがこれほど遅いのは普通のことですか?少しスピードアップするためにできることはありますか?
アップデート
クエリの結果を説明します。これが正しくない場合は、私に知らせなければなりません...
更新 2
試合テーブルの構造:
ボクサーのテーブル構造:
更新 3
わかりました、おそらくいくつかの進歩...
私が変われば
LEFT JOIN bouts opp_bouts ON (opp_bouts.boxer_1=opponent.id OR opp_bouts.boxer_2=opponent.id) AND opp_bouts.bout_date < bouts.bout_date
に:
LEFT JOIN bouts opp_bouts ON (opp_bouts.boxer_1=opponent.id) AND opp_bouts.bout_date < bouts.bout_date
これにより、クエリが約 0.0250 まで高速化されます。これは速度に大きな問題があるようです。OR機能を改善するか、別の方法で行うことができますか?
更新 4
Kickstart のおかげで、以下のコードが非常に高速になりましたが、100% ではありません。すべてを正しく実行しました... または、さらに高速化するために一部の列にインデックスを作成する必要がある場合は? どのようにスケールアップするかはまだわかりませんが、私が手に入れることができる最高のものかもしれません?
bouts
現在、最大 46,066 行と最大 90,531 行がありboxers
、クエリは bout_date と両方の boxer_1/2 のインデックスで 0.0466 で実行されます。
SELECT bout_id,
bout_date,
winner,
result,
ended_in_round,
total_rounds,
verified,
opponent_first_name,
opponent_last_name,
opponent_id,
opp_weight,
fighter_weight,
SUM(opp_ended_in_round) AS opp_total_rounds_boxed,
SUM(IF(opp_winner = -1,1,0)) AS opp_draws,
SUM(IF(opp_winner = opponent_id,1,0)) AS opp_wins,
COUNT(opp_id) AS opp_fights,
boxer_power,
boxer_chin,
boxer_boxer,
SUM(opp_power_rating) AS opp_power,
SUM(opp_chin_rating) AS opp_chin,
SUM(opp_boxer_rating) AS opp_boxer,
SUM(opp_exp_rating) AS opp_experience,
MIN(IF(opp_verified = 1,9999-01-01 , opp_bout_date)) AS verified_up_to,
MAX(opp_bout_date) AS opp_last_bout
FROM (
SELECT bouts.bout_id, bouts.total_rounds, bouts.result, bouts.verified, opp_bouts.verified AS opp_verified, opp_bouts.bout_id AS opp_id, bouts.winner, opp_bouts.winner AS opp_winner, bouts.ended_in_round, opp_bouts.ended_in_round AS opp_ended_in_round, bouts.boxer_2_weight AS opp_weight, bouts.bxr1_power_points AS boxer_power, opp_bouts.bxr1_power_points AS opp_power_rating , opp_bouts.bxr1_chin_points AS opp_chin_rating, opp_bouts.bxr1_boxer_points AS opp_boxer_rating, opp_bouts.bxr1_exp_points AS opp_exp_rating, bouts.bxr1_chin_points AS boxer_chin, bouts.bxr1_boxer_points AS boxer_boxer, bouts.boxer_1_weight AS fighter_weight, bouts.boxer_2 AS opponent_id, bouts.bout_date, opp_bouts.bout_date AS opp_bout_date, opponent.first_name AS opponent_first_name, opponent.last_name AS opponent_last_name
FROM bouts
LEFT JOIN boxers opponent ON opponent.id = boxer_2
LEFT JOIN bouts opp_bouts ON opp_bouts.boxer_1 = opponent.id AND opp_bouts.bout_date < bouts.bout_date
WHERE bouts.bout_date < 'NOW()'
AND bouts.boxer_1 = '63514'
UNION ALL
SELECT bouts.bout_id, bouts.total_rounds, bouts.result, bouts.verified, opp_bouts.verified AS opp_verified, opp_bouts.bout_id AS opp_id, bouts.winner, opp_bouts.winner AS opp_winner, bouts.ended_in_round, opp_bouts.ended_in_round AS opp_ended_in_round, bouts.boxer_2_weight AS opp_weight, bouts.bxr1_power_points AS boxer_power, opp_bouts.bxr2_power_points AS opp_power_rating, opp_bouts.bxr2_chin_points AS opp_chin_rating, opp_bouts.bxr2_boxer_points AS opp_boxer_rating, opp_bouts.bxr2_exp_points AS opp_exp_rating, bouts.bxr1_chin_points AS boxer_chin, bouts.bxr1_boxer_points AS boxer_boxer, bouts.boxer_1_weight AS fighter_weight, bouts.boxer_2 AS opponent_id, bouts.bout_date, opp_bouts.bout_date AS opp_bout_date, opponent.first_name AS opponent_first_name, opponent.last_name AS opponent_last_name
FROM bouts
LEFT JOIN boxers opponent ON opponent.id = boxer_2
LEFT JOIN bouts opp_bouts ON opp_bouts.boxer_2 = opponent.id AND opp_bouts.bout_date < bouts.bout_date
WHERE bouts.bout_date < 'NOW()'
AND bouts.boxer_1 = '63514'
UNION ALL
SELECT bouts.bout_id, bouts.total_rounds, bouts.result, bouts.verified, opp_bouts.verified AS opp_verified, opp_bouts.bout_id AS opp_id, bouts.winner, opp_bouts.winner AS opp_winner, bouts.ended_in_round, opp_bouts.ended_in_round AS opp_ended_in_round, bouts.boxer_1_weight AS opp_weight, bouts.bxr2_power_points AS boxer_power, opp_bouts.bxr1_power_points AS opp_power_rating, opp_bouts.bxr1_chin_points AS opp_chin_rating, opp_bouts.bxr1_boxer_points AS opp_boxer_rating, opp_bouts.bxr1_exp_points AS opp_exp_rating, bouts.bxr2_chin_points AS boxer_chin, bouts.bxr2_boxer_points AS boxer_boxer, bouts.boxer_2_weight AS fighter_weight, bouts.boxer_1 AS opponent_id, bouts.bout_date, opp_bouts.bout_date AS opp_bout_date, opponent.first_name AS opponent_first_name, opponent.last_name AS opponent_last_name
FROM bouts
LEFT JOIN boxers opponent ON opponent.id = boxer_1
LEFT JOIN bouts opp_bouts ON opp_bouts.boxer_1 = opponent.id AND opp_bouts.bout_date < bouts.bout_date
WHERE bouts.bout_date < 'NOW()'
AND bouts.boxer_2 = '63514'
UNION ALL
SELECT bouts.bout_id, bouts.total_rounds, bouts.result, bouts.verified, opp_bouts.verified AS opp_verified, opp_bouts.bout_id AS opp_id, bouts.winner, opp_bouts.winner AS opp_winner, bouts.ended_in_round, opp_bouts.ended_in_round AS opp_ended_in_round, bouts.boxer_1_weight AS opp_weight, bouts.bxr2_power_points AS boxer_power, opp_bouts.bxr2_power_points AS opp_power_rating, opp_bouts.bxr2_chin_points AS opp_chin_rating, opp_bouts.bxr2_boxer_points AS opp_boxer_rating, opp_bouts.bxr2_exp_points AS opp_exp_rating, bouts.bxr2_chin_points AS boxer_chin, bouts.bxr2_boxer_points AS boxer_boxer, bouts.boxer_2_weight AS fighter_weight, bouts.boxer_1 AS opponent_id, bouts.bout_date, opp_bouts.bout_date AS opp_bout_date, opponent.first_name AS opponent_first_name, opponent.last_name AS opponent_last_name
FROM bouts
LEFT JOIN boxers opponent ON opponent.id = boxer_1
LEFT JOIN bouts opp_bouts ON opp_bouts.boxer_2 = opponent.id AND opp_bouts.bout_date < bouts.bout_date
WHERE bouts.bout_date < 'NOW()'
AND bouts.boxer_2 = '63514'
) AS my_table GROUP BY bout_id ORDER BY bout_date DESC