Recently, I was engaged in extracting a large amount of data, which consists of thousands of records from a MySQL database. Since this was my first time to process such a large dataset, I did not think about the effectiveness of the SQL statement. And the problem arises.
Here are the database tables (This is just a simple curriculum database model):
course:
+-----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------------------+------+-----+---------+----------------+ | course_id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | lecturer | varchar(20) | NO | | NULL | | | credit | float | NO | | NULL | | | week_from | tinyint(3) unsigned | NO | | NULL | | | week_to | tinyint(3) unsigned | NO | | NULL | | +-----------+---------------------+------+-----+---------+----------------+
select:
+-----------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+------------------+------+-----+---------+----------------+ | select_id | int(10) unsigned | NO | PRI | NULL | auto_increment | | card_no | int(10) unsigned | NO | | NULL | | | course_id | int(10) unsigned | NO | | NULL | | | term | varchar(7) | NO | | NULL | | +-----------+------------------+------+-----+---------+----------------+
When I want to get all the courses a student has chosen (with his card number) SQL statement
SELECT course_id, name, lecturer, credit, week_from, week_to FROM `course` WHERE course_id IN ( SELECT course_id FROM `select` WHERE card_no=<student card number> );
But it was very slow, and it did not return anything for a long time. So I changed the WHERE IN clauses to NATURAL JOIN . Here is the SQL,
SELECT course_id, name, lecturer, credit, week_from, week_to FROM `select` NATURAL JOIN `course` WHERE card_no=<student card number>;
He returns immediately and works great.
So my question is:
- What is the difference between
NATURAL JOIN and WHERE IN Clauses? - What makes them work differently? (Perhaps because I did not configure any
INDEX ?) - When will we use
NATURAL JOIN or WHERE IN ?
rAy
source share