Goir,
Here is table data (from your example):
mysql> select * from st664_1;±—±--------+| id | value |±—±--------+| 1 | MySQL || 2 | Browser || 3 | Upload |±—±--------+3 rows in set (0.00 sec)
mysql> select * from st664_2;±—±---------------±----------+| id | foreign_table1 | value |±—±---------------±----------+| 1 | 1 | rocks || 2 | 1 | sometimes || 3 | 2 | Explorer || 4 | 2 | Internet || 5 | 2 | Micro$oft || 6 | 2 | Firefox || 7 | 1 | Firefox |±—±---------------±----------+7 rows in set (0.00 sec)
And this is query, which returns row from first table, which has requested number of related records in second table:
mysql> select * from st664_1 where id in (select foreign_table1 from st664_2 group by foreign_table1 having count(foreign_table1)=4);±—±--------+| id | value |±—±--------+| 2 | Browser |±—±--------+1 row in set (0.00 sec)
mysql> select * from st664_1 where id in (select foreign_table1 from st664_2 group by foreign_table1 having count(foreign_table1)=3);±—±------+| id | value |±—±------+| 1 | MySQL |±—±------+1 row in set (0.00 sec)
So queries are returning output, which you asked for, and there is no need to filter output by python script.
Here is explaining of this query:
mysql> EXPLAIN select * from st664_1 where id in (select foreign_table1 from st664_2 group by foreign_table1 having count(foreign_table1)=4); ±—±-------------------±--------±-----±--------------+ ------±--------±-----±-----±---------------------------- ----+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | ±—±-------------------±--------±-----±--------------+ ------±--------±-----±-----±---------------------------- ----+| 1 | PRIMARY | st664_1 | ALL | NULL | NULL | NULL | NULL | 3 | Using where || 2 | DEPENDENT SUBQUERY | st664_2 | ALL | NULL | NULL | NULL | NULL | 7 | Using temporary; Using filesort | ±—±-------------------±--------±-----±--------------+ ------±--------±-----±-----±---------------------------- ----+2 rows in set (0.00 sec)
As it’s using filesort and creates temporary table, it is not very fast. If it’s ok for you to add index to column foreign_table1, it can help to speed up those queries:
mysql> create index foreign_table1_idx ON st664_2(foreign_table1);Query OK, 7 rows affected (0.04 sec)Records: 7 Duplicates: 0 Warnings: 0
And let’s look at explain now:
mysql> EXPLAIN select * from st664_1 where id in (select foreign_table1 from st664_2 group by foreign_table1 having count(foreign_table1)=4); ±—±-------------------±--------±------±-------------- ±-------------------±--------±-----±-----±------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | ±—±-------------------±--------±------±-------------- ±-------------------±--------±-----±-----±------------+| 1 | PRIMARY | st664_1 | ALL | NULL | NULL | NULL | NULL | 3 | Using where || 2 | DEPENDENT SUBQUERY | st664_2 | index | NULL | foreign_table1_idx | 5 | NULL | 7 | Using index | ±—±-------------------±--------±------±-------------- ±-------------------±--------±-----±-----±------------+2 rows in set (0.00 sec)
You see that now it uses only index, and it’s faster then doing filesort+temporary. Please note that if you often add data to second table, then maybe it’s not a good option for you…
P.S.
Table1 => st664_1
Table2 => st664_2
Sorry for that, but I try to keep table names in my test db in order )