码迷,mamicode.com
首页 > 其他好文 > 详细

5.2 索引两表优化案例

时间:2017-08-30 15:35:58      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:ble   索引   表连接   idt   添加   highlight   blog   dex   条件   

1、案例

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 book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));
INSERT INTO book(card) VALUES(FLOOR(1+(RAND()*20)));

#两张表连接查询优化

2、下面使用explain 分析sql

explain SELECT * from class LEFT JOIN book ON class.card = book.card

技术分享

结论:type 有All ,需要优化

3、优化

#添加索引优化
ALTER TABLE `book` ADD INDEX Y(`card`);

#第2次explain
explain SELECT * from class LEFT JOIN book ON class.card = book.card

技术分享

结论:

#可以看到第二行的 type 变为了 ref , rows 也变成了 1 优化比较明显。
#这是由左连接特性决定的。LEFT JOIN 条件用于确定从右表搜索行,左边一定都有,
#所以右边是我们的关键点,一定需要建立索引。

 

3、再次分析

#左连接 改成 右连接
explain SELECT * from class RIGHT JOIN book ON class.card = book.card

技术分享

#删除索引
drop index Y on book;
#重建索引
create index X on class(card);
#再次分析
explain SELECT * from class RIGHT JOIN book ON class.card = book.card

技术分享

结论:优化比较明显。这是因为RIGHT JOIN 条件用于确定如何从左表搜索行,右边一定都有,所以左边是我们的关键点,一定需要建立索引。

 

综上所述 :我们得到以下结论

左连接:索引建在右表上。
右连接:索引建在左表上。

 

 

  

 

  

5.2 索引两表优化案例

标签:ble   索引   表连接   idt   添加   highlight   blog   dex   条件   

原文地址:http://www.cnblogs.com/huanchupkblog/p/7452954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!