最后来看看 inner join 的情况:
代码如下:
explain select * from class inner join book on class.card = book.card;
结果:
explain select * from class inner join book on class.card = book.card;
结果:
代码如下:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: book
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: class
type: ref
possible_keys: x
key: x
key_len: 4
ref: test.book.card
rows: 1000
Extra:
2 rows in set (0.00 sec)
删除旧索引:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: book
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: class
type: ref
possible_keys: x
key: x
key_len: 4
ref: test.book.card
rows: 1000
Extra:
2 rows in set (0.00 sec)
删除旧索引:
代码如下:
DROP INDEX y ON book;
结果
DROP INDEX y ON book;
结果
代码如下:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: class
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: book
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
2 rows in set (0.00 sec)
建立新索引。
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: class
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: book
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
2 rows in set (0.00 sec)
建立新索引。
代码如下:
ALTER TABLE `class` ADD INDEX x ( `card`);
结果
ALTER TABLE `class` ADD INDEX x ( `card`);
结果
代码如下:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: class
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: book
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
2 rows in set (0.00 sec)
综上所述,inner join 和 left join 差不多,都需要优化右表。而 right join 需要优化左表。
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: class
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: book
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
2 rows in set (0.00 sec)
综上所述,inner join 和 left join 差不多,都需要优化右表。而 right join 需要优化左表。
我们再来看看三表查询的例子
添加一个新索引:
代码如下:
ALTER TABLE `phone` ADD INDEX z ( `card`);
ALTER TABLE `book` ADD INDEX y ( `card`);
ALTER TABLE `phone` ADD INDEX z ( `card`);
ALTER TABLE `book` ADD INDEX y ( `card`);
代码如下:
explain select * from class left join book on class.card=book.card left join phone on book.card = phone.card;
explain select * from class left join book on class.card=book.card left join phone on book.card = phone.card;
代码如下:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: class
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: book
type: ref
possible_keys: y
key: y
key_len: 4
ref: test.class.card
rows: 1000
Extra:
*************************** 3. row ***************************
id: 1
select_type: SIMPLE
table: phone
type: ref
possible_keys: z
key: z
key_len: 4
ref: test.book.card
rows: 260
Extra: Using index
3 rows in set (0.00 sec)
后 2 行的 type 都是 ref 且总 rows 优化很好,效果不错。
MySql 中的 explain 语法可以帮助我们改写查询,优化表的结构和索引的设置,从而最大地提高查询效率。当然,在大规模数据量时,索引的建立和维护的代价也是很高的,往往需要较长的时间和较大的空间,如果在不同的列组合上建立索引,空间的开销会更大。因此索引最好设置在需要经常查询的字段中。
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: class
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: book
type: ref
possible_keys: y
key: y
key_len: 4
ref: test.class.card
rows: 1000
Extra:
*************************** 3. row ***************************
id: 1
select_type: SIMPLE
table: phone
type: ref
possible_keys: z
key: z
key_len: 4
ref: test.book.card
rows: 260
Extra: Using index
3 rows in set (0.00 sec)
后 2 行的 type 都是 ref 且总 rows 优化很好,效果不错。
MySql 中的 explain 语法可以帮助我们改写查询,优化表的结构和索引的设置,从而最大地提高查询效率。当然,在大规模数据量时,索引的建立和维护的代价也是很高的,往往需要较长的时间和较大的空间,如果在不同的列组合上建立索引,空间的开销会更大。因此索引最好设置在需要经常查询的字段中。
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。