mysql> explain select name , a.citycount from country join ( select CountryCode, count(id) citycount from city group by CountryCode having count(id)>=100 ) a on (a.countrycode=country.code) order by a.citycount;
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Sort: a.citycount
-> Stream results (cost=1044 rows=0)
-> Nested loop inner join (cost=1044 rows=0)
-> Table scan on country (cost=24.6 rows=238)
-> Index lookup on a using
(CountryCode = country.`Code`) (cost=0.25..4.29 rows=17.1) -> Materialize (cost=0..0 rows=0)
-> Filter: (count(city.ID) >= 100)
-> Table scan on
-> Aggregate using temporary table
-> Table scan on city (cost=416 rows=4079)
|
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.002 sec)
mysql> explain format=traditional select name , a.citycount from country join ( select CountryCode, count(id) citycount from city group by CountryCode having count(id)>=100
+----+-------------+------------+------------+------+---------------+-------------+---------+----------------------+------+----------+---------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+-------------+---------+----------------------+------+----------+---------------------------------+
| 1 | PRIMARY | country | NULL | ALL | PRIMARY | NULL | NULL | NULL | 238 | 100.00 | Using temporary; Using filesort |
| 1 |
PRIMARY |
| NULL | ref | | | 12 | world_y.country.Code | 17 | 100.00 | NULL || 2 | DERIVED | city | NULL | ALL | NULL | NULL | NULL | NULL | 4079 | 100.00 | Using temporary |
+----+-------------+------------+------------+------+---------------+-------------+---------+----------------------+------+----------+---------------------------------+
3 rows in set, 1 warning (0.001 sec)