bakineggs (owner)

Revisions

gist: 167413 Download_button fork
public
Public Clone URL: git://gist.github.com/167413.git
Embed All Files: show embed
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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)