dan@dan:~$ mysql sandbox
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.0.37 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create table some_table (foo int(11), bar int(11));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into some_table (foo, bar) values (1, 2), (3, 4), (5, 6), (7, 8);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> explain select avg(foo) from some_table group by bar;
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
| 1 | SIMPLE | some_table | ALL | NULL | NULL | NULL | NULL | 4 | Using temporary; Using filesort |
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
1 row in set (0.00 sec)
mysql> create index index_bar on some_table (bar);
Query OK, 4 rows affected (0.02 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> explain select avg(foo) from some_table group by bar;
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
| 1 | SIMPLE | some_table | ALL | NULL | NULL | NULL | NULL | 4 | Using temporary; Using filesort |
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
1 row in set (0.00 sec)
mysql> create index index_bar_foo on some_table (bar, foo);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> explain select avg(foo) from some_table group by bar;
+----+-------------+------------+-------+---------------+---------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+-------+---------------+---------------+---------+------+------+-------------+
| 1 | SIMPLE | some_table | index | NULL | index_bar_foo | 10 | NULL | 4 | Using index |
+----+-------------+------------+-------+---------------+---------------+---------+------+------+-------------+
1 row in set (0.00 sec)