I'm trying to optimize the query below
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;
Checking the performance of the query with the EXPLAIN command, I get the following results.
Basically the games table contains the number of views and the channels in an hourly basis, the game_profiles table maps the game to its publiisher(s), and the publishers table contains the more detailed row of each existing publisher. What I'm trying to achieve is display the top 10 publishers based on the total views of its games from the past week.
Running out of ideas, I tried using implicit JOIN. The query is written below
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;
Which gives me the following result for the EXPLAIN command.
From what I understand, this should be returning the same result, but the query is running slow in MySQL workbench that I can't wait for its result so I can't verify that it actually returns the same rows. Just judging from the EXPLAIN result however, I think the latter query should run faster. Is there something I'm missing here on why this is not the case? Thank you very much.
P.S. My database design is not really optimal. This is more of a prototype database. No normalization was performed when this was made. I just want to understand better what's happening in my queries. Thank you.