Bug #22997 subselects with distincts take long time to process
Submitted: 5 Oct 2006 1:38 Modified: 5 Oct 2006 6:46
Reporter: George Lee Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:5.0.18-nt OS:Windows (windows)
Assigned to: CPU Architecture:Any
Tags: distinct, subselect

[5 Oct 2006 1:38] George Lee
Description:
This is our query to find "ACTIVE" countries.  This query with subselect and distinct takes very long time to process.

select * from country where 
fips_code in (
  select distinct(fips_primary_country_code) from place
);
74 rows in set (2 min 11.61 sec)

------------------------------------
However if you break apart the two queries then it processes fine.

mysql> select distinct(fips_primary_country_code) from place;
74 rows in set (0.41 sec)

select * from country where fips_code in ('DA', 'GM', ...  'AS')
74 rows in set (0.00 sec)

------------------------------------
Here is the explain plan of the original query

mysql> explain select * from country where
    -> fips_code in (
    ->   select distinct(fips_primary_country_code) from place
    -> );
+----+--------------------+---------+------+---------------+------+---------+------+-------+------------------------------+
| id | select_type        | table   | type | possible_keys | key  | key_len | ref  | rows  | Extra                        |
+----+--------------------+---------+------+---------------+------+---------+------+-------+------------------------------+
|  1 | PRIMARY            | country | ALL  | NULL          | NULL | NULL    | NULL |   261 | Using where                  |
|  2 | DEPENDENT SUBQUERY | place   | ALL  | NULL          | NULL | NULL    | NULL | 96718 | Using where; Using temporary |
+----+--------------------+---------+------+---------------+------+---------+------+-------+------------------------------+
2 rows in set (0.00 sec)

How to repeat:
See above

Suggested fix:
Is there some way to hint that we are returning a very small subset of original table so that we generate a temporary table for the top level select?
[5 Oct 2006 6:46] Sveta Smirnova
Thank you for the report.

But it is known restriction: http://dev.mysql.com/doc/refman/5.0/en/subquery-restrictions.html