高性能MySQL阅读笔记

第五章

2020-10-29  本文已影响0人  frankie_cheung
mysql b+树索引基础

作者在索引基础讲了一些废话,关于索引的基础,看下图足以。

image.png
b+树索引作者列举的一些信息

示例表:

Create Table: CREATE TABLE `people` (
  `last_name` varchar(50) NOT NULL,
  `first_name` varchar(50) NOT NULL,
  `dob` date NOT NULL,
  KEY `last_name` (`last_name`,`first_name`,`dob`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
mysql> select * from people;
+-----------+------------+------------+
| last_name | first_name | dob        |
+-----------+------------+------------+
| bob       | kk         | 1997-12-24 |
| frankie   | cheung     | 1998-12-24 |
| hhhaa     | erkk       | 1996-12-24 |
+-----------+------------+------------+
3 rows in set (0.00 sec)

以下均为可以使用索引的案例

mysql> explain select * from people where last_name='bob' and first_name='kk' and dob='1997-12-24';
+----+-------------+--------+------------+------+---------------+-----------+---------+-------------------+------+----------+-------------+
| id | select_type | table  | partitions | type | possible_keys | key       | key_len | ref               | rows | filtered | Extra       |
+----+-------------+--------+------------+------+---------------+-----------+---------+-------------------+------+----------+-------------+
|  1 | SIMPLE      | people | NULL       | ref  | last_name     | last_name | 407     | const,const,const |    1 |   100.00 | Using index |
+----+-------------+--------+------------+------+---------------+-----------+---------+-------------------+------+----------+-------------+
1 row in set, 1 warning (0.01 sec)
mysql> explain select * from people where last_name='bob';
+----+-------------+--------+------------+------+---------------+-----------+---------+-------+------+----------+-------------+
| id | select_type | table  | partitions | type | possible_keys | key       | key_len | ref   | rows | filtered | Extra       |
+----+-------------+--------+------------+------+---------------+-----------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | people | NULL       | ref  | last_name     | last_name | 202     | const |    1 |   100.00 | Using index |
+----+-------------+--------+------------+------+---------------+-----------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql>

mysql> explain select * from people where last_name like '%b';
+----+-------------+--------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
| id | select_type | table  | partitions | type  | possible_keys | key       | key_len | ref  | rows | filtered | Extra                    |
+----+-------------+--------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
|  1 | SIMPLE      | people | NULL       | index | NULL          | last_name | 407     | NULL |    3 |    33.33 | Using where; Using index |
+----+-------------+--------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> explain select * from people where last_name >='bob' and last_name <='frankie' ;
+----+-------------+--------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
| id | select_type | table  | partitions | type  | possible_keys | key       | key_len | ref  | rows | filtered | Extra                    |
+----+-------------+--------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
|  1 | SIMPLE      | people | NULL       | range | last_name     | last_name | 202     | NULL |    2 |   100.00 | Using where; Using index |
+----+-------------+--------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> explain select * from people where last_name ='bob' and first_name like '%k' ;
+----+-------------+--------+------------+------+---------------+-----------+---------+-------+------+----------+--------------------------+
| id | select_type | table  | partitions | type | possible_keys | key       | key_len | ref   | rows | filtered | Extra                    |
+----+-------------+--------+------------+------+---------------+-----------+---------+-------+------+----------+--------------------------+
|  1 | SIMPLE      | people | NULL       | ref  | last_name     | last_name | 202     | const |    1 |    33.33 | Using where; Using index |
+----+-------------+--------+------------+------+---------------+-----------+---------+-------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> explain select last_name from people where last_name ='bob' ;
+----+-------------+--------+------------+------+---------------+-----------+---------+-------+------+----------+-------------+
| id | select_type | table  | partitions | type | possible_keys | key       | key_len | ref   | rows | filtered | Extra       |
+----+-------------+--------+------------+------+---------------+-----------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | people | NULL       | ref  | last_name     | last_name | 202     | const |    1 |   100.00 | Using index |
+----+-------------+--------+------------+------+---------------+-----------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

因为索引是有序的,所以假如你可以使用上述方式,则那你也可以进行排序

b+树索引的限制
mysql> explain select last_name from people where first_name ='kk' ;
+----+-------------+--------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
| id | select_type | table  | partitions | type  | possible_keys | key       | key_len | ref  | rows | filtered | Extra                    |
+----+-------------+--------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
|  1 | SIMPLE      | people | NULL       | index | NULL          | last_name | 407     | NULL |    3 |    33.33 | Using where; Using index |
+----+-------------+--------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)
其他索引

作者又开始介绍了哈希索引,空间数据索引R-TREE,全文索引,因为基本不使用,所以直接略过

索引的优点

作者在此介绍了一种索引评价规则:三星系统
1.索引把相关信息放到一起。则一星,这里的放到一起 我个人理解为多列索引组合的
2.索引的数据顺序和查询顺序一直。则二星 这里就是要按照顺序where条件
3.覆盖索引 则三星

高性能索引策略
上一篇 下一篇

猜你喜欢

热点阅读