I need help to optimize the query to avoid using "Using temporary", "Using filesort".
CREATE TABLE `target_bahrain_pepsi` (
`id` int(11) DEFAULT NULL,
`col_value` varchar(50) DEFAULT NULL,
`source_file_id` int(11) DEFAULT NULL,
KEY `id` (`id`)
) ENGINE=InnoDB
SELECT su.adbrand_id, sum(st.col_value) as amount
FROM statex_with_id su INNER JOIN target_bahrain_pepsi st ON st.id = su.id
GROUP BY su.adbrand_id
ORDER BY amount DESC ;
Table statex_with_id has also index.
mysql> EXPLAIN select su.adbrand_id, sum(st.col_value) as amount
-> from statex_with_id su
-> INNER JOIN target_bahrain_pepsi st
-> ON st.id = su.id GROUP BY su.adbrand_id ORDER BY amount DESC ;
+----+-------------+-------+--------+---------------+---------+---------+------------------+---------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+------------------+---------+----------------------------------------------+
| 1 | SIMPLE | st | index | id | id | 58 | NULL | 1804021 | Using index; Using temporary; Using filesort |
| 1 | SIMPLE | su | eq_ref | PRIMARY | PRIMARY | 4 | dashboard1.st.id | 1 | Using where |
+----+-------------+-------+--------+---------------+---------+---------+------------------+---------+----------------------------------------------+
SUM()on aVARCHARcolumn? Why? – ypercube Jan 14 at 9:41CREATE TABLEfor the other table as well. What is the relationship between the 2 tables? – ypercube Feb 13 at 15:20