我試圖優化以下隱式連接在EXPLAIN行較少,但運行速度比慢明確的JOIN
SELECT `publisher_id`, `publisher_name`, SUM(`views`) AS `total_views`, SUM(`channels`) AS `total_channels`
FROM (
SELECT DISTINCT `name` AS `publisher_name`, `id` AS `publisher_id`
FROM `publishers`
WHERE TRIM(`name`) <> ''
) AS `publisher_names`
INNER JOIN
(
SELECT `twitch_name`, `publishers`
FROM `game_profiles`
WHERE `twitch_name` IS NOT NULL
AND `publishers` IS NOT NULL
AND TRIM(`publishers`) <> ''
) AS `game_list`
ON `game_list`.`publishers` LIKE CONCAT('%', `publisher_names`.`publisher_name`, '%')
INNER JOIN
(
SELECT `games`.`id` AS `id`, `games`.`name`, `games`.`simple_name`, `games`.`box`, SUM(`channels`) AS `channels`, SUM(`viewers`) AS `views`
FROM `games`
WHERE `log_date` >= SUBDATE(NOW(), INTERVAL 1 WEEK)
AND `log_date` <= SUBDATE(NOW(), INTERVAL 0 WEEK)
GROUP BY `games`.`id`
) AS `view_list`
ON `game_list`.`twitch_name` = `view_list`.`name`
GROUP BY `publisher_id` ORDER BY `total_views` DESC LIMIT 10;
檢查與查詢性能的查詢說明命令,我得到如下結果。
基本上遊戲表中包含的意見,以小時爲單位的信道的數量,在game_profiles表遊戲映射到其publiisher(S),以及出版商表包含每個現有發佈者的更詳細的行。我試圖實現的是根據過去一週遊戲的總視圖來顯示排名前10位的發佈商。
用盡想法,我嘗試使用隱式JOIN。查詢如下
SELECT `publishers`.`id` AS `publisher_id`, `publishers`.`name` AS `publisher_name`,
SUM(`games`.`viewers`) AS `total_views`, SUM(`games`.`channels`) AS `total_channels`
FROM `game_profiles`, `publishers`, `games`
WHERE `game_profiles`.`twitch_name` IS NOT NULL
AND `game_profiles`.`publishers` IS NOT NULL AND TRIM(`game_profiles`.`publishers`) <> ''
AND `game_profiles`.`publishers` LIKE CONCAT('%', `publishers`.`name`, '%')
AND `game_profiles`.`twitch_name` = `games`.`name`
AND `games`.`log_date` >= SUBDATE(NOW(), INTERVAL 1 WEEK)
AND `games`.`log_date` <= SUBDATE(NOW(), INTERVAL 0 WEEK)
GROUP BY `publisher_id` ORDER BY `total_views` DESC LIMIT 10;
寫這使我對以下結果EXPLAIN命令。
據我所知,這應該是返回相同的結果,但查詢運行在MySQL工作臺慢,我不能等待它的結果,所以我無法驗證,它實際上回報相同的行。只是從EXPLAIN結果來看,我認爲後者的查詢應該運行得更快。有什麼我在這裏失蹤,爲什麼不是這樣嗎?非常感謝你。
P.S.我的數據庫設計並不是最佳的。這更像是一個原型數據庫。做這個時沒有進行標準化。我只是想更好地瞭解我的查詢中發生了什麼。謝謝。
你寫的SQL代碼很亂方式(格式不正確),所以這不是簡單容易理解......請遵循正確的格式化標準,然後更新您的問題 –
感謝您的編輯@AnkitAgrawal – iron59