p group by p.CompanyId,p.CallerPhone order by p.StartTime desc limit 5) as t,companies c where t.CompanyId=c.Id;
+----+-------------+------------+--------+---------------+---------+---------+-------------+--------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+-------------+--------+---------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 5 | |
| 1 | PRIMARY | c | eq_ref | PRIMARY | PRIMARY | 4 | t.CompanyId | 1 | |
| 2 | DERIVED | p | ALL | NULL | NULL | NULL | NULL | 220955 | Using temporary; Using filesort |
+----+-------------+------------+--------+---------------+---------+---------+-------------+--------+---------------------------------+
test > desc select sql_no_cache p.CallerPhone,p.CompanyId Id,c.Name from PhoneBill p left join companies c on p.CompanyId=c.Id group by p.CompanyId,p.CallerPhone order by p.StartTime desc limit 5;
+----+-------------+-------+--------+---------------+---------+---------+-----------------+--------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+-----------------+--------+---------------------------------+
| 1 | SIMPLE | p | ALL | NULL | NULL | NULL | NULL | 220955 | Using temporary; Using filesort |
| 1 | SIMPLE | c | eq_ref | PRIMARY | PRIMARY | 4 | b2c.p.CompanyId | 1 | |
+----+-------------+-------+--------+---------------+---------+---------+-----------------+--------+---------------------------------+
+----+-------------+------------+--------+---------------+---------------------------------+---------+-------------+--------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------------------------------+---------+-------------+--------+----------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 5 | |
| 1 | PRIMARY | c | eq_ref | PRIMARY | PRIMARY | 4 | t.CompanyId | 1 | |
| 2 | DERIVED | p | index | NULL | CompanyId_CallerPhone_StartTime | 95 | NULL | 232706 | Using index; Using temporary; Using filesort |
+----+-------------+------------+--------+---------------+---------------------------------+---------+-------------+--------+----------------------------------------------+
test > desc select sql_no_cache p.CallerPhone,p.CompanyId Id,c.Name from PhoneBill p left join companies c on p.CompanyId=c.Id group by p.CompanyId,p.CallerPhone order by p.StartTime desc limit 5;
+----+-------------+-------+--------+---------------+-----------+---------+-----------------+------+-----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+-----------+---------+-----------------+------+-----------------+
| 1 | SIMPLE | p | index | NULL | StartTime | 8 | NULL | 5 | Using temporary |
| 1 | SIMPLE | c | eq_ref | PRIMARY | PRIMARY | 4 | b2c.p.CompanyId | 1 | |
+----+-------------+-------+--------+---------------+-----------+---------+-----------------+------+-----------------+
两条SQL耗时都0.4s左右,不可避免的join而Using temporary;