MySQL高级---B站学习总结---索引优化两表案例

MySQL高级—B站学习总结—索引优化两表案例

案例:
  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 )));
  1. 下面开始explain分析
    EXPLAIN SELECT * FROM class LEFT JOIN book ON class.card = book.card;
    在这里插入图片描述
    结论: type 有All

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

  3. 第2次explain的EXPLAIN SELECT * FROM class LEFT JOIN book ON class.card = book.card;
    在这里插入图片描述
    可以看到第二行的 type 变为了 ref,rows 也变成了优化比较明显。

  4. 删除上个索引,尝试使用创建的class表的索引
    DROP INDEX Y ON book;
    ALTER TABLE class ADD INDEX Y ( card);
    执行EXPLAIN SELECT * FROM class LEFT JOIN book ON class.card = book.card;
    结果是如下图
    在这里插入图片描述
    结论: 所以说由左连接特性决定的。LEFT JOIN(左连接) 条件用于确定如何从右表搜索行,左边一定都有,所以右边是我们的关键点,一定需要建立索引。 所以说RIGHT JOIN(右连接)同理,条件用于确定如何从左表搜索行,右边一定都有,所以左边是我们的关键点,一定需要建立索引。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值