I have 2 very simple tables created as below:
create table company(
company_id int auto_increment,
company_no varchar(20),
company_name varchar(20),
primary key(company_id),
unique index uni_company_no(company_no)
);
create table department(
department_id int auto_increment,
department_no varchar(20),
department_name varchar(20),
company_id int not null,
primary key(department_id),
unique index uni_department_no(department_no),
index idx_company_id(company_id)
);
And if I query with the follwing SQL(I know it's an odd query), the result of EXPLAIN statenment shows that MySQL takes unique index(uni_company_no) instead of primary key:
explain select dept.* from department dept right join company com on dept.company_id = com.company_id;
My questions are:
- Why didn't MySQL use full table scan for table company?
- Why didn't MySQL take primary key of table company if it decides to use index?
I'm very new to MySQL, so any input could be helpful. Thanks in advance.