MySQL Forums
Forum List  »  Performance

Why does this select query take forever?
Posted by: KK Night
Date: July 29, 2005 01:30PM

I just have two simple tables with 30,000 rows each.
The first table, fs1, has two fields, value char(32) and name char(50).
The second table fs2 has the same fields as fs1.
Both tables have indexes on the value field and the name field respectively.

I want to find out the records that have the same 'value' but different 'name' between these two tables. Here is the select query:

select fs1.name, fs2.name
from fs1, fs2
where fs1.value = fs2.value and
fs1.name != fs2.name;

But, running this query took forever on my MySQL 4.1 server. Finally, it was killed by the server.

Here are the DESCRIBE of fs1 and fs2 tables:
fs1:
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| value | char(32) | | MUL | | |
| name | char(50) | | MUL | | |
+---------------+-------------+------+-----+---------+-------+
fs2:
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| value | char(32) | | MUL | | |
| name | char(50) | | MUL | | |
+---------------+-------------+------+-----+---------+-------+

Show INDEX from fs1:
mysql> show index from fs1;
+--------+------------+------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------+------------+------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| fs1 | 1 | v1idx | 1 | value | A | 1655 | NULL | NULL | | BTREE | |
| fs1 | 1 | n1idx | 1 | name| A | 810 | NULL | NULL | | BTREE | |
+--------+------------+------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+

Show INDEX from fs2:
mysql> show index from fs2;
+--------+------------+------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------+------------+------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| fs2 | 1 | v2idx | 1 | value | A | 1655 | NULL | NULL | | BTREE | |
| fs2 | 1 | n2idx | 1 | name| A | 810 | NULL | NULL | | BTREE | |
+--------+------------+------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+

Explain of the select query:
+----+-------------+-------+------+-----------------------+------------+---------+-------------------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+-----------------------+------------+---------+-------------------+-------+-------------+
| 1 | SIMPLE | fs1 | ALL | v1idx,n1idx | NULL | NULL | NULL | 38074 | |
| 1 | SIMPLE | fs2 | ref | v2idx | v2idx | 32 | fs1.value | 23 | Using where |
+----+-------------+-------+------+-----------------------+------------+---------+-------------------+-------+-------------+

Is there anyone that knows why? Can MySQL really handle join of tables that have tens of thousands records in it?

Many thanks.

--KKnight

Options: ReplyQuote


Subject
Views
Written By
Posted
Why does this select query take forever?
2302
July 29, 2005 01:30PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.