I have two tables members and dep whose description are as follows:
TABLE members:
+-----------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------+------+-----+---------+-------+
| MemberID_M | varchar(8) | YES | MUL | NULL | |
| Age | varchar(5) | YES | | NULL | |
| Sex | varchar(1) | YES | | NULL | |
| SomeInfo | int(11) | YES | | NULL | |
+-----------------+------------+------+-----+---------+-------+
TABLE dep:
+-----------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------+------+-----+---------+-------+
| MemberID_t | int(11) | YES | MUL | NULL | |
| YEAR | varchar(2) | NO | | | |
| Days | tinyint(4) | YES | | NULL | |
| train | bigint(20) | NO | | 0 | |
+-----------------+------------+------+-----+---------+-------+
I want to perform the following query:
CREATE TABLE table2
SELECT a.*,b.*
FROM dep AS a LEFT OUTER JOIN members AS b
ON a.MemberID_t = b.Memberid_M;
Intially, the ids in both the tables were not indexed and the query did not return for hours. Now, even after indexing it is taking a lot of time.
EXPLAIN for the SELECT part of the query is:
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+
| 1 | SIMPLE | a | ALL | NULL | NULL | NULL | NULL | 436689 | |
| 1 | SIMPLE | b | ALL | memid2 | NULL | NULL | NULL | 226127 | |
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+
The problem is MemberID_M is a varchar and MemberID_t is an INT
The conversion of this fields is why take so long
For optimal results, the key fields has to be the same type