2018-02-20 10:40:05 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
2018-02-22 14:47:22 +08:00
|
|
|
|
* [?<3F><>????](#?<3F><>????)
|
2018-02-20 10:40:05 +08:00
|
|
|
|
* [1. InnoDB](#1-innodb)
|
|
|
|
|
* [2. MyISAM](#2-myisam)
|
2018-02-22 14:47:22 +08:00
|
|
|
|
* [3. InnoDB ?? MyISAM ????](#3-innodb-??-myisam-????)
|
|
|
|
|
* [????????](#????????)
|
|
|
|
|
* [1. ????](#1-????)
|
|
|
|
|
* [2. ??????](#2-??????)
|
|
|
|
|
* [3. ?????](#3-?????)
|
|
|
|
|
* [4. ????????](#4-????????)
|
|
|
|
|
* [????](#????)
|
|
|
|
|
* [1. ????????](#1-????????)
|
|
|
|
|
* [1.1 B-Tree ????](#11-b-tree-????)
|
|
|
|
|
* [1.2 ???????](#12-???????)
|
|
|
|
|
* [1.3. ????????????R-Tree??](#13-????????????r-tree??)
|
|
|
|
|
* [1.4 ???????](#14-???????)
|
|
|
|
|
* [2. ?????????](#2-?????????)
|
|
|
|
|
* [3. ???????](#3-???????)
|
|
|
|
|
* [3.1 ????????](#31-????????)
|
|
|
|
|
* [3.2 ??????](#32-??????)
|
|
|
|
|
* [3.3 ????????](#33-????????)
|
|
|
|
|
* [3.4 ?????<3F><>????](#34-?????<3F><>????)
|
|
|
|
|
* [3.5 ???????](#35-???????)
|
|
|
|
|
* [3.6 ????????](#36-????????)
|
|
|
|
|
* [4. B-Tree ?? B+Tree ???](#4-b-tree-??-b+tree-???)
|
2018-02-20 10:40:05 +08:00
|
|
|
|
* [4. 1 B-Tree](#4-1-b-tree)
|
|
|
|
|
* [4.2 B+Tree](#42-b+tree)
|
2018-02-22 14:47:22 +08:00
|
|
|
|
* [4.3 ?????????????? B+Tree](#43-??????????????-b+tree)
|
|
|
|
|
* [4.4 ?????? B-Tree ?? B+Tree](#44-??????-b-tree-??-b+tree)
|
|
|
|
|
* [??????????](#??????????)
|
2018-02-20 10:40:05 +08:00
|
|
|
|
* [1. Explain](#1-explain)
|
2018-02-22 14:47:22 +08:00
|
|
|
|
* [2. ??????????](#2-??????????)
|
|
|
|
|
* [3. ??????????](#3-??????????)
|
|
|
|
|
* [4. ????? DELETE ?? INSERT ???](#4-?????-delete-??-insert-???)
|
|
|
|
|
* [???????](#???????)
|
|
|
|
|
* [?????????????](#?????????????)
|
|
|
|
|
* [1. ???????](#1-???????)
|
|
|
|
|
* [2. ??????](#2-??????)
|
|
|
|
|
* [?<3F><>?????](#?<3F><>?????)
|
2018-02-20 10:40:05 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?<3F><>????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
## 1. InnoDB
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
InnoDB ?? MySQL ??????????????<3F><>???????? InnoDB ??????????????????????????<3F><>???<3F><>
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???? MVCC ?????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????????????<3F><>?????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????????????????????????????????????????????<3F><>??? hash ??????????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????<3F><>???????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
## 2. MyISAM
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
MyISAM ??????????????????????????????????<3F><>????GIS??????? MyISAM ???????????<3F><>?????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????????????<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????<3F><>??????????????????????????????????????????????<3F><>????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????? DELAY_KEY_WRITE ??????????????????????????????????????????<3F><>???????????<3F><>??????<3F><>??????????????????????????????????????????????????<3F><>????????????????????????<3F><>?????????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????????????????????????????????????????? MyISAM ?????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????<3F><>?????????????????????????????????? MyISAM??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
MyISAM ?????????????????<3F><>?????????<3F><>???????????<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 3. InnoDB ?? MyISAM ????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
InnoDB ??????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
InnoDB ?????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
MyISAM ??????????????? InnoDB ??????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
MyISAM ??????????? InnoDB ??????<3F><>?????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
MyISAM ????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 1. ????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT ?????? 8, 16, 24, 64 <20><>?<3F><>????????????<3F><>??????<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
INT(11) ?<3F><>????????<3F><>??????????????????????????<3F><>???????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 2. ??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
FLOAT ?? DOUBLE ??????????DECIMAL ??????<3F><>???????CPU ????????????????????? DECIMAl ??????????? DECIMAL ?????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
FLOAT??DOUBLE ?? DECIMAL ??????????<3F><>????? DECIMAL(18, 9) ?????? 18 <20><>??? 9 <20><>?<3F><><EFBFBD><EFBFBD>?????????? 9 <20><>?<3F><>?????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 3. ?????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????? CHAR ?? VARCHAR ?????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
VARCHAR ??????????????????????????<3F><>??????????????????? UPDATE ????????<3F><>??????????????????????????????<3F><>????????<3F><>?????????MyISAM ???<3F><>????????<3F>ě<EFBFBD>???? InnoDB ??????????????<3F><>??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
VARCHAR ??????????<3F><>????? CHAR ???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 4. ????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
MySQL ?????????????????????????DATATIME ?? TIMESTAMP??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
**DATATIME**
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????? 1001 ?? 9999 ????????????????????? 8 ????<3F><>???
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????MySQL ???????????????????????? DATATIME ??????<3F><>2008-01016 22:37:08???????? ANSI ????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
**TIMESTAMP**
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?? UNIX ?????????????? 1970 ?? 1 ?? 1 ????????????????????????????????? 4 ????????????? 1970 ?? ?? 2038 ??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
MySQL ???? FROM_UNIXTIME() ?????? Unxi ??????????????????? UNIX_TIMESTAMP() ?????????????? Unix ??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????????? TIMESTAMP ?<3F><>?????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????? TIMESTAMP????????? DATETIME ???<3F><>??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????<3F><>?????????????????????????????????????<3F><>??????<3F><>?????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????<3F><>???????????????????????????????<3F><>???????<3F><>?????????????????<3F><>????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????<3F><>?????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 1. ????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 1.1 B-Tree ????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
B-Tree ?????????? MySQL ?<3F><>?????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????<3F>k?????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????????<3F><>?????????<3F><>????????B-Tree ?????????????????????<3F><>?????????????<3F><>?????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????<3F><>
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????<3F><>??????<3F><>??????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 1.2 ???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?? MySQL ????? Memory ??????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
InnoDB ?????????????????<3F><>????????????????????????????????????????????? B-Tree ???????????????????????????????? B-Tree ???????<3F><>?????????<3F><>??????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????????<3F><>?????????????????????<3F><>???????????<3F><>???????????????<3F><>??<3F><>???????????????????????????????????????????????????????????????????????????????<3F><>??????????????????????????<3F><>?????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 1.3. ????????????R-Tree??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
MyISAM ?<3F><>??????????????????????????????<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????????????????????<3F><>?????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 1.4 ???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
MyISAM ?<3F><>???????????????????????????<3F><>??????????????????????<3F><>????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??? MATCH AGAINST????????????? WHERE??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 2. ?????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
- ????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
- ??????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
- ????? I/O ?????? I/O??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 3. ???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 3.1 ????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????<3F><>??????????<3F><>?????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????? actor_id ?<3F><>???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
SELECT actor_id FROM sakila.actor WHERE actor_id + 1 = 5;
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 3.2 ??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???? BLOB??TEXT ?? VARCHAR ??????<3F><>??????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????? **?????????** ?????????????????????????????????????????????<3F><>???????????? 1 ???????????????<3F><>???????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 3.3 ????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????????<3F><>????????????????????????????????????<3F><>??????????????<3F><>????? actor_id ?? file_id ???????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
SELECT file_id, actor_ id FROM sakila.film_actor
|
|
|
|
|
WhERE actor_id = 1 OR film_id = 1;
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 3.4 ?????<3F><>????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????<3F><>?????<3F><>????????????????? customer_id ???????? staff_id ???????????? customer_id ?<3F><>??????????????<3F><>
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
SELECT COUNT(DISTINCT staff_id)/COUNT(*) AS staff_id_selectivity,
|
|
|
|
|
COUNT(DISTINCT customer_id)/COUNT(*) AS customer_id_selectivity,
|
|
|
|
|
COUNT(*)
|
|
|
|
|
FROM payment;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
staff_id_selectivity: 0.0001
|
|
|
|
|
customer_id_selectivity: 0.0373
|
|
|
|
|
COUNT(*): 16049
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 3.5 ???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b9e9ae8c-e216-4c01-b267-a50dbeb98fa4.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????????????????????????<3F><>?????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????<3F><>?????????????<3F><>?????InnoDB ???????????????<3F><>???? B-Tree ???????<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????<3F><>???????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**???**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
1. ??????????????????????? I/O ??????
|
|
|
|
|
2. ???????????? B-Tree ?<3F><>??????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**???**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
1. ?????????????????? I/O ??????????????????????????????????<3F><>????????????????
|
|
|
|
|
2. ???????????????????????????????????????????
|
|
|
|
|
3. ????????????????????????????<3F><>???????????<3F><>?<3F><>?
|
|
|
|
|
4. ?????????????????<3F><>??<3F><>????????????????????????????<3F><>????????<3F><>?????????????
|
|
|
|
|
5. ????<3F><>????<3F>k????????????????????<3F><>?????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 3.6 ????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????????<3F><>????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 4. B-Tree ?? B+Tree ???
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
### 4. 1 B-Tree
|
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5ed71283-a070-4b21-85ae-f2cbfd6ba6e1.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????? B-Tree??????????????????????????? [key, data]??key ?????????data ????????? key ????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
B-Tree ???????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
- ?????????????????????????? B-Tree ???????
|
|
|
|
|
- ???????<3F><>? key ????????????<3F><>?
|
|
|
|
|
- ?????????????????? key ????? key<sub>i</sub> ?? key<sub>i+1</sub>?????? null?????????????????? key ???? key<sub>i</sub> ??<3F><>?? key<sub>i+1</sub>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?? B-Tree ?<3F><>? key ?????????????????????????????<3F><>?????????????????????? data????????????????????????????<3F><>????????????????? null ????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????????? B-Tree ????????????????????????????????????????????????????????? B-Tree ?????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
### 4.2 B+Tree
|
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/63cd5b50-d6d8-4df6-8912-ef4a1dd5ba13.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?? B-Tree ????B+Tree ??????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
- ??????????????? 2d ?????? 2d+1??
|
|
|
|
|
- ?????<3F><> data????<3F><> key????????<3F><>???
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 4.3 ?????????????? B+Tree
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1ee5f0a5-b8df-43b9-95ab-c516c54ec797.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????????????? B+Tree ????????? B+Tree ????????????????????????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 4.4 ?????? B-Tree ?? B+Tree
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????????????????????????????? B-/+Tree ???????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????<3F><>????????<3F><>?????????????????????????<3F><>?????????????<3F><>????<3F><>????<3F><>???????????????????<3F><>?????<3F><>???? 4k??????????????????<3F><>?????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????<3F><>??????<3F><>???????????????????????????<3F><>????????????????? I/O?????????????????????????????<3F><>?????????????????????????????????????????????????????????????????<3F><>?????????????????????<3F><>????????????????????????????????????????????<3F><>?????????????????????????????? I/O ????????????B-Tree ????<3F><>????????? h-1 ?? I/O??????????<3F><>???????????? O(h)=O(logdN)????????????<3F><>????? d ??????????????????? 100????? h ???<3F><>??????????? 3??????????????????h ????????????????????????????????????????????????????????<3F><>??????? B-Tree ????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
B+Tree ????????????????????????? d ?<3F><>?????? B+Tree ????????? data ??????????<3F><>??????????<3F><>?????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ??????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
## 1. Explain
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????? SQL ???????????<3F><>???????????<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
- select_type : ?????????<3F><>?????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
- key : ????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
- rows : ????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 2. ??????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????????????????????????????????????????<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??<3F><>????? SELECT * ???????????????????<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 3. ??????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????? LIMIT ???????????????<3F><>?<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????????????????????<3F><>????????????????????????????????????????????<3F>k???????????????ݔ?<3F><>?????????? Explain ???????????? rows ????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
SELECT * FROM sakila.film_actor WHERE film_id = 1;
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 4. ????? DELETE ?? INSERT ???
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????<3F><>???????????????????????????????????????????????????????<3F><>????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
DELEFT FROM messages WHERE create < DATE_SUB(NOW(), INTERVAL 3 MONTH);
|
|
|
|
|
```
|
|
|
|
|
```sql
|
|
|
|
|
rows_affected = 0
|
|
|
|
|
do {
|
|
|
|
|
rows_affected = do_query(
|
|
|
|
|
"DELETE FROM messages WHERE create < DATE_SUB(NOW(), INTERVAL 3 MONTH) LIMIT 10000")
|
|
|
|
|
} while rows_affected > 0
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ????????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????<3F><>????<3F><><EFBFBD><EFBFBD>????<3F><>???????????????????????????????????<3F><>??<3F><>???????<3F><>??????????????????<3F><>???????????????????<3F><>????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ??<3F><>??????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????????<3F><>???????????????<3F><>?????????????????????<3F><>????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ????<3F><>?**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????<3F><>??????<3F><>?????????????????????????<3F><>??????????????? payDB?????????? userDB ???????????<3F><>?????????<3F><>??????????<3F><>???
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ???<3F><>?**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????<3F><>??????????????<3F><>??????????????<3F><>????<3F><> id ??????????????<3F><>????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**5. ????<3F><>??????<3F><>?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????<3F><>???????????????????????????????????<3F><>????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????????????????????????<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**6. ???<3F><>???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????? merge ?<3F><>???<3F><>
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**7. ???????????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
(1) ????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????<3F><>???????????????<3F><>??????????????????????????????????????????????????????????????????????????????????????????????<3F><>????<3F><>????????<3F><>???????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
(2) ?????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????????????????????????????????????????????????????????????????????????<3F><>?????????????????????????????????????????<3F><>????????????????????<3F><>?<3F><>?????<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????<3F><>???????????????????<3F><>???????????????????????????????????????<3F><>?????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 1. ???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1.1 ??????????<3F><>????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????????????-????????<3F><>?????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1.2 ???? IP ????? IP ?<3F><>?**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
? MySQL ???????????? IP ??????? MySQL ????<3F><>???????? IP ??????????? MySQL ?????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1.3 ?<3F><>?????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????<3F><>????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/fabd5fa0-b75e-48d0-9e2c-31471945ceb9.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1.4 ??????<3F><>?????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????<3F><>???????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 2. ??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?<3F><>?????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
- ?????? MySQL
|
|
|
|
|
- [MySQL ????????????????????? ](http://blog.codinglabs.org/articles/theory-of-mysql-index.html)
|
|
|
|
|
- [MySQL ???????????? ](http://www.runoob.com/w3cnote/mysql-index.html)
|
|
|
|
|
- [20+ ?? MySQL ??????????????? ](https://www.jfox.info/20-tiao-mysql-xing-nen-you-hua-de-zui-jia-jing-yan.html)
|