建表
(上篇我们建立了两张表) 在新建一张
mysql> create table phone(
-> phoneid int(10) not null auto_increment,
-> card int(10) not null,
-> primary key(phoneid)
-> );
Query OK, 0 rows affected (0.17 sec)
插入数据
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
insert into phone (card) values(floor(1+(rand()*20)));
mysql> select * from phone;
+---------+------+
| phoneid | card |
+---------+------+
| 1 | 13 |
| 2 | 10 |
| 3 | 11 |
| 4 | 6 |
| 5 | 15 |
| 6 | 18 |
| 7 | 3 |
| 8 | 20 |
| 9 | 11 |
| 10 | 14 |
| 11 | 15 |
| 12 | 13 |
| 13 | 2 |
| 14 | 8 |
| 15 | 15 |
| 16 | 9 |
| 17 | 1 |
| 18 | 16 |
| 19 | 16 |
| 20 | 14 |
+---------+------+
20 rows in set (0.00 sec)
//在上一篇中我们在book中建立了索引 将其删除
mysql> drop index idx_book_c on book;
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
案例1
三张表关联查询使用左连接(并没有任何意义,只为了分析)
mysql> explain select * from class left join book on class.card=book.card left join phone on book.card=phone.card;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
| 1 | SIMPLE | class | ALL | NULL | NULL | NULL | NULL | 20 | NULL |
| 1 | SIMPLE | book | ALL | NULL | NULL | NULL | NULL | 20 | Using where; Using join buffer (Block Nested Loop) |
| 1 | SIMPLE | phone | ALL | NULL | NULL | NULL | NULL | 20 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
3 rows in set (0.00 sec)
我们还有没建立索引,当然是全表扫描,该如何建立索引呢? 根据上篇我们了解到 如果是左连接最好是在右表建立索引
//book相当于是class的右表
mysql> create index idx_book_c on book(card);
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0
//phone相当于是book与class左连接之后的右表
mysql> create index idx_phone_c on phone(card);
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
再次运行之前的三表连查
mysql> explain select * from class left join book on class.card=book.card left join phone on book.card=phone.card;
+----+-------------+-------+------+---------------+-------------+---------+-----------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+-------------+---------+-----------------+------+-------------+
| 1 | SIMPLE | class | ALL | NULL | NULL | NULL | NULL | 20 | NULL |
| 1 | SIMPLE | book | ref | idx_book_c | idx_book_c | 4 | test.class.card | 1 | Using index |
| 1 | SIMPLE | phone | ref | idx_phone_c | idx_phone_c | 4 | test.book.card | 1 | Using index |
+----+-------------+-------+------+---------------+-------------+---------+-----------------+------+-------------+
3 rows in set (0.00 sec)
结论
后两行的type 都是ref且orws优化的很好,效果不错.因此索引最好设置在需要经常查询的字段当中