foghost.github.io's People
foghost.github.io's Issues
MySQLのアーキテクチャ
Mysql InnoDB的索引(Index)
存储结构
InnoDB里的索引有两种,都是通过B+树结构来存储的:Clustered index(主键索引/聚簇索引)
, Secondary index(辅助索引)
关于B+树的详细介绍可以上网查询
大致结构如下
B+树有几个特点
- B+树的所有数据都按递增顺序存储在叶子节点
- 因为数据是有序存储的所以B+树的查询效率很高
- 另外B树查询时从根节点到叶子节点的距离都相同,保证了查询效率的稳定
- 因为B树的叶子节点是连起来的
- 做范围查询时会很有效率(
x > 1 and x < 100
) - 做左侧一致性查询也会很有效率(
like ‘abc%’
),反之做右侧一致性查询时效率就会比较差(like ‘%abc%’
,like ‘%abc'
- 做范围查询时会很有效率(
Clustered Index
- 聚簇索引是通过 特定的键(Key) 作为索引来查询数据
- 每一个叶子节点存储着:特定的键(Key)和 对应行的完整数据记录
Clustered Index里使用的 键(Key)
Clustered Index会按下面的优先顺序来选择可以做为索引的键(key)
- Primary Key: 主键
- Not Null Unique Key: 不为空的唯一键
- 如果符合上面的键都没有,InnoDB会在内部生成一个隐藏的键(6字节的单调递增Row ID)来作为索引
详细参考Mysql官方文档介绍:
https://dev.mysql.com/doc/refman/5.0/en/innodb-index-types.html
Clustered Index的 Pros & Cons
优点
- 检索速度快,特别是通过主键进行的查询。因为主键和数据都存在叶子节点上,查到主键后即可获取数据,不需要硬盘访问(如果索引文件已经被读到内存里)
缺点
- 索引文件会变的比较大(因为要同时存储实际数据),一些情况下造成性能下降(索引大小超过Buffer pool大小等)
Secondary Index (辅助索引)
- Secondary Index也是通过 特定的键(Key: 副键) 作为索引来查询数据
- 不同于Clustered Index的是每一个叶子节点存储着: Clustered Index里的 Key (用来到Clustered Index里查找数据完整记录)
- 所以通过 Secondary Index查询数据时通常都需要两次索引查询
- 首先在 Secondary Index里查询到存储实际数据记录的 Clustered Index Key (通常情况下是 PK)
- 再到 Clustered Index里通过上面找到的 Key 取得实际的数据记录(存储的Clustered index的叶子节点里)
Covering Index
上面有讲到使用Secondary Index查找数据时通常需要2次Index检索,导致检索效率降低,如果查询可以可以在 Secondary Index里一次完成那么效率就会提高很多,这种做法被叫做 Covering Index.
具体做法是:select时只指定 Secondary index可以一次性查询到的列名来实现(比如,主键 + Secondary Index的键列,或者是 只指定Secondary index的键列)
当数据量比较大时在满足业务需求的情况下,通过使用Covering Index(限制select的列名)可以提高查询性能
mysql> show create table employ\G;
*************************** 1. row ***************************
Table: employ
Create Table: CREATE TABLE `employ` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8
// 通过Clustered Index(主键)查询
mysql> explain select * from employ where id=1\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: employ
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 8
ref: const
rows: 1
Extra: NULL
1 row in set (0.00 sec)
// 通过Secondary Index(副键)查询
mysql> explain select * from employ where name='aa'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: employ
type: ref
possible_keys: name
key: name
key_len: 152
ref: const
rows: 1
Extra: Using index condition
1 row in set (0.00 sec)
// 通过Covering Index来查询(Extra: Using index)
// name是Seconary Index的Key,
// id是主键存储在Secondary Index的叶子节点上
// 所以只需要一次索引查询就能取到实际数据
mysql> explain select id,name from employ where name='aa'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: employ
type: ref
possible_keys: name
key: name
key_len: 152
ref: const
rows: 1
Extra: Using where; Using index
1 row in set (0.00 sec)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.