建表語句
create table if not exists `class`(
`id` int(10) unsigned not null auto_increment,
`card` int(10) unsigned not null,
primary key(`id`)
create table if not exists `book`(
`bookid` int(10) unsigned not null auto_increment,
`card` int(10) unsigned not null,
primary key(`bookid`)
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
insert into class(card) values(floor(1 + (rand() * 20)));
使用explain對sql進行檢查
mysql> explain select * from class left join book on class.card = book.card;
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
| 1 | ****** | class | null | all | null | null | null | null | 24 | 100.00 | null |
| 1 | ****** | book | null | all | null | null | null | null | 1 | 100.00 | using where; using join buffer (block nested loop) |
2 rows in set, 1 warning (0.00 sec)
可以看到type都為all(執行全表查詢)
使用了join緩衝區(其使用的演算法為block nested-loop(bnl))
新增索引
1.對book表的card屬性新增索引
alter table `book` add index y(`card`);
2.再次用explain檢視之前的sql
mysql> explain select * from class left join book on class.card = book.card;
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
| 1 | ****** | class | null | all | null | null | null | null | 24 | 100.00 | null |
| 1 | ****** | book | null | ref | y | y | 4 | test.class.card | 1 | 100.00 | using index |
2 rows in set, 1 warning (0.00 sec)
可以看到book表的type優化為了type, 且extra變為了using index, 不會再使用緩衝區了。
3.對class表的card屬性新增索引
alter table `class` add index y(`card`);
4.再次用explain檢視之前的sql
mysql> explain select * from class left join book on class.card = book.card;
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
| 1 | ****** | class | null | index | null | y | 4 | null | 24 | 100.00 | using index |
| 1 | ****** | book | null | ref | y | y | 4 | test.class.card | 1 | 100.00 | using index |
2 rows in set, 1 warning (0.00 sec)
可以看到type變為了index, ref 而 extra 都變為了using index
可以得出的結論
左連線時, 對右表建索引進行優化更重要(左表都保留, 必然全表掃瞄)
右連線時, 對左表建索引進行優化更重要(右表都保留, 必然全表掃瞄)
mysql 鍵 索引 五 MySQL索引和鍵
mysql索引和鍵 不同的索引有不同功能 不同的約束方式,不同的使用規則 優點 對一張表來說,索引就像一本書的目錄,能夠加快查詢速度 缺點 占用物理儲存空間 索引資訊儲存在表對應的檔案裡 會降低插入 更新表記錄的速度 insert delete update 1.索引的型別 普通索引 index 唯...
mysql 多表索引 mysql 兩表索引優化
建表語句 create table if not exists class id int 10 unsigned not null auto increment,card int 10 unsigned not null,primary key id create table if not exis...
MySQL優化索引之雙表優化
建表sql create table if not exists class id int 10 unsigned not null primary key auto increment,card int 10 unsigned not null create table if not exists...