[Solved] performance wise difference between inner join, nested query and natural join?

EverSQL Database Performance Knowledge Base

performance wise difference between inner join, nested query and natural join?

Database type:

I have two tables created with following codes in MySQL 5.6

create table color(color_id int auto_increment primary key,
                   color varchar(10));

and

create table flower(id int auto_increment primary key,
                    name varchar(20),
                    color_id int,
                    constraint fk_color foreign key references color(color_id));

and then I inserted some rows in color then in flower.

then I queried

1)

   select flower.name, color.color from flower, color where
   flower.color_id=color.color_id; 

2)

   select name,(select color from color where color_id=flower.color_id) color from flower;

3)

   select flower.name, color.color from color natural join flower;

4)

   select flower.name, color.color from flower natural join color;

All the queries gave me same result.

Which query is fastest and most efficient (nested query or inner join or natural join) and why specially among nested query and inner join?

what practice should we opt for querying?

whats the difference between the last two queries?

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `color` ADD INDEX `color_idx_color_id` (`color_id`);
ALTER TABLE `flower` ADD INDEX `flower_idx_color_id` (`color_id`);
The optimized query:
SELECT
        flower.name,
        color.color 
    FROM
        flower,
        color 
    WHERE
        flower.color_id = color.color_id

Related Articles



* original question posted on StackOverflow here.