CS-Notes/notes/MySQL.md
2018-02-22 14:47:22 +08:00

422 lines
12 KiB
Markdown
Raw Blame History

<!-- GFM-TOC -->
* [?썷????](#?썷????)
* [1. InnoDB](#1-innodb)
* [2. MyISAM](#2-myisam)
* [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 ?????㎒????](#34-?????㎒????)
* [3.5 ???????](#35-???????)
* [3.6 ????????](#36-????????)
* [4. B-Tree ?? B+Tree ???](#4-b-tree-??-b+tree-???)
* [4. 1 B-Tree](#4-1-b-tree)
* [4.2 B+Tree](#42-b+tree)
* [4.3 ?????????????? B+Tree](#43-??????????????-b+tree)
* [4.4 ?????? B-Tree ?? B+Tree](#44-??????-b-tree-??-b+tree)
* [??????????](#??????????)
* [1. Explain](#1-explain)
* [2. ??????????](#2-??????????)
* [3. ??????????](#3-??????????)
* [4. ????? DELETE ?? INSERT ???](#4-?????-delete-??-insert-???)
* [???????](#???????)
* [?????????????](#?????????????)
* [1. ???????](#1-???????)
* [2. ??????](#2-??????)
* [?┩?????](#?┩?????)
<!-- GFM-TOC -->
# ?썷????
## 1. InnoDB
InnoDB ?? MySQL ??????????????뿨???????? InnoDB ??????????????????????????썷???도
???? MVCC ?????????????????????????????????????????????????
?????????????????????????????????????㎊?????????
????????????????????????????????????????????????????????㎑??? hash ??????????????????????????????????????????????????????????
????㎉???????????????????????
## 2. MyISAM
MyISAM ??????????????????????????????????걠????GIS??????? MyISAM ???????????㎌?????????????????????????
????????????????????????㎡?
????????????????㎌??????????????????????????????????????????????㎉????????????????????????????
???????????????????㎡?
???????? DELAY_KEY_WRITE ??????????????????????????????????????????㎑???????????㎑??????㎒??????????????????????????????????????????????????㎑????????????????????????㎑?????????????????????????????????????????????????????????
??????????????????????????????????????????????????????? MyISAM ?????
???????????????????㎡?????????????????????????????????? MyISAM??
MyISAM ?????????????????썷?????????㎉???????????Ð?
## 3. InnoDB ?? MyISAM ????
**????**
InnoDB ??????????
**????**
InnoDB ?????????????
**???????**
MyISAM ??????????????? InnoDB ??????????????????????
**????**
MyISAM ??????????? InnoDB ??????㎌?????
**????????**
MyISAM ????????????????????????
# ????????
## 1. ????
TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT ?????? 8, 16, 24, 64 ┡?썷????????????㎡??????Ð?
INT(11) ?㎒????????朗??????????????????????????썷???????????????????
## 2. ??????
FLOAT ?? DOUBLE ??????????DECIMAL ??????㎡???????CPU ????????????????????? DECIMAl ??????????? DECIMAL ?????????????????????????
FLOAT??DOUBLE ?? DECIMAL ??????????㎭????? DECIMAL(18, 9) ?????? 18 ┡??? 9 ┡?썷㎡?????????? 9 ┡?썷?????????
## 3. ?????
????? CHAR ?? VARCHAR ?????????????????????????????
VARCHAR ??????????????????????????썷??????????????????? UPDATE ????????㎾??????????????????????????????㎡????????㎔?????????MyISAM ???㎿????????┙썷???? InnoDB ??????????????Ω??????
VARCHAR ??????????┑????? CHAR ???????
## 4. ????????
MySQL ?????????????????????????DATATIME ?? TIMESTAMP??
**DATATIME**
???????? 1001 ?? 9999 ????????????????????? 8 ????썷???
???????????
?????????MySQL ???????????????????????? DATATIME ??????퀛2008-01016 22:37:08???????? ANSI ????????????????????????
**TIMESTAMP**
?? UNIX ?????????????? 1970 ?? 1 ?? 1 ????????????????????????????????? 4 ????????????? 1970 ?? ?? 2038 ??
????????㏁??
MySQL ???? FROM_UNIXTIME() ?????? Unxi ??????????????????? UNIX_TIMESTAMP() ?????????????? Unix ??????
??????????????????????? TIMESTAMP ?㎒?????????????????????
????????? TIMESTAMP????????? DATETIME ???㏏??????
# ????
????????썷?????????????????????????????????????썷??????㎿?????????????????
???????????????????????????????????
??????㎡???????????????????????????????㏏???????㎒?????????????????㏏????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????≤?????????
## 1. ????????
### 1.1 B-Tree ????
B-Tree ?????????? MySQL ?썷?????????????????
???????????????????<3F>k?????????????????????????????????
????????????????????㏊?????????㏁????????B-Tree ?????????????????????╋?????????????㎌?????????????????????????
?????????????????????????????<3F>
???????????????㎒??????㎿??????????????????
### 1.2 ???????
????????????????????????
?? MySQL ????? Memory ??????????????????
InnoDB ?????????????????㎡????????????????????????????????????????????? B-Tree ???????????????????????????????? B-Tree ???????㏁?????????㎉??????????????????
?????????????????????????????????썷?????????????????????㎒???????????㎡???????????????㎒??㎒???????????????????????????????????????????????????????????????????????????????╋??????????????????????????㉡?????
### 1.3. ????????????R-Tree??
MyISAM ?썷??????????????????????????????썷??
??????????????????????????????????㏏?????????????????????????
### 1.4 ???????
MyISAM ?썷???????????????????????????㎒??????????????????????㎒????
??? MATCH AGAINST????????????? WHERE??
## 2. ?????????
- ????????????????????????????
- ??????????????????????????????
- ????? I/O ?????? I/O??
## 3. ???????
### 3.1 ????????
????㎿??????????㎿?????????????????????????????????????????????????
?????????????????? actor_id ?㎒???????
```sql
SELECT actor_id FROM sakila.actor WHERE actor_id + 1 = 5;
```
### 3.2 ??????
???? BLOB??TEXT ?? VARCHAR ??????㏊??????????????????????????????????
???????????????????? **?????????** ?????????????????????????????????????????????㏏???????????? 1 ???????????????┠???????????????
### 3.3 ????????
??????????????????????㎿????????????????????????????????????Ð??????????????㏊????? actor_id ?? file_id ???????????????
```sql
SELECT file_id, actor_ id FROM sakila.film_actor
WhERE actor_id = 1 OR film_id = 1;
```
### 3.4 ?????㎒????
?????????????????Ω?????뿨????????????????? customer_id ???????? staff_id ???????????? customer_id ?Ω??????????????도
```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
```
### 3.5 ???????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b9e9ae8c-e216-4c01-b267-a50dbeb98fa4.jpg)
????????????????????????????????????썷?????
????????????????㎊?????????????썷?????InnoDB ???????????????㎑???? B-Tree ???????㎡?
?????????????㎑???????????????????????????????????????????
**???**
1. ??????????????????????? I/O ??????
2. ???????????? B-Tree ?㏊??????????????
**???**
1. ?????????????????? I/O ??????????????????????????????????뿨????????????????
2. ???????????????????????????????????????????
3. ????????????????????????????㎔???????????┡?Ð?
4. ?????????????????㏊??썷????????????????????????????㏊????????÷?????????????
5. ????㎾????<3F>k????????????????????썷?????????????????????????????????????
### 3.6 ????????
??????????????????????┖????
## 4. B-Tree ?? B+Tree ???
### 4. 1 B-Tree
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5ed71283-a070-4b21-85ae-f2cbfd6ba6e1.jpg)
??????? B-Tree??????????????????????????? [key, data]??key ?????????data ????????? key ????????
B-Tree ???????????????????????
- ?????????????????????????? B-Tree ???????
- ???????㎒? key ????????????㏊?
- ?????????????????? key ????? key<sub>i</sub> ?? key<sub>i+1</sub>?????? null?????????????????? key ???? key<sub>i</sub> ??㎡?? key<sub>i+1</sub>??
?? B-Tree ?㎽? key ?????????????????????????????㎔?????????????????????? data????????????????????????????㎿????????????????? null ????????????????????????
???????????????????????? B-Tree ????????????????????????????????????????????????????????? B-Tree ?????
### 4.2 B+Tree
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/63cd5b50-d6d8-4df6-8912-ef4a1dd5ba13.jpg)
?? B-Tree ????B+Tree ??????????
- ??????????????? 2d ?????? 2d+1??
- ?????썷 data????썷 key????????썷???
### 4.3 ?????????????? B+Tree
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1ee5f0a5-b8df-43b9-95ab-c516c54ec797.jpg)
????????????????????????? B+Tree ????????? B+Tree ????????????????????????????????????????????????????????????????????????
### 4.4 ?????? B-Tree ?? B+Tree
?????????????????????????????????????????????????????? B-/+Tree ???????????
???????????썷????????<3F>?????????????????????????썷?????????????㎡????<3F>????썷???????????????????㏊?????㎡???? 4k??????????????????┡?????????
?????????????????????????????썷??????㏊???????????????????????????썷????????????????? I/O?????????????????????????????┕?????????????????????????????????????????????????????????????????쟝?????????????????????Ð????????????????????????????????????????????㎡?????????????????????????????? I/O ????????????B-Tree ????┢????????? h-1 ?? I/O??????????뿥???????????? O(h)=O(logdN)????????????㏊????? d ??????????????????? 100????? h ???㎡??????????? 3??????????????????h ????????????????????????????????????????????????????????㏏??????? B-Tree ????
B+Tree ????????????????????????? d ?㏁?????? B+Tree ????????? data ??????????㏀??????????㏀?????????
# ??????????
## 1. Explain
???????? SQL ???????????㎾???????????㏊?
- select_type : ?????????㎌?????????????????
- key : ????????
- rows : ????????
## 2. ??????????
???????????????????????????????????????????????????????㎡?
??<3F>????? SELECT * ???????????????????㎡?
## 3. ??????????
?????? LIMIT ???????????????㎉?㎡?
????????????????????????????????擄????????????????????????????????????????????<3F>k???????????????<3F>?㎌?????????? Explain ???????????? rows ????????????????
```sql
SELECT * FROM sakila.film_actor WHERE film_id = 1;
```
## 4. ????? DELETE ?? INSERT ???
??????????㎒???????????????????????????????????????????????????????㎡????????????
```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
```
# ???????
**1. ????????????**
????????????????㎡????㎉㎡????㎿???????????????????????????????????<3F>??㎉???????썷??????????????????썷???????????????????÷????????????
**2. ??≤??????????**
??????????????????????㎒???????????????㎒?????????????????????㎑????????????????????
**3. ????Ω?**
??????????<3F>??????㎐?????????????????????????壯??????????????? payDB?????????? userDB ???????????썷?????????㏁??????????㏁???
**4. ???Ω?**
????㎒??????????????썷??????????????㏊????틬 id ??????????????㎋????
**5. ????Ω??????Ω?????**
?????????㎒???????????????????????????????????Ω????????
??????????????????????????????????????Ω??
**6. ???Ω???????**
????????? merge ?썷???도
**7. ???????????????**
(1) ????????
????Ω???????????????썷??????????????????????????????????????????????????????????????????????????????????????????????Ŧ????㎰????????┚???????????????????????????????
(2) ?????????????
?????????????????????????????????????????????????????????????????????????????????????????????????┡?????????????????????????????????????????┑????????????????????㎔?┑?????㉯?
# ?????????????
?????????????㎋???????????????????㎋???????????????????????????????????????㎋?????????????????????????
## 1. ???????
**1.1 ??????????㎋????**
??????????????????????????-????????㎒?????????????????
**1.2 ???? IP ????? IP ?㏁?**
? MySQL ???????????? IP ??????? MySQL ????㏏???????? IP ??????????? MySQL ?????????
**1.3 ?㎌?????????**
???????????·????????????????????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/fabd5fa0-b75e-48d0-9e2c-31471945ceb9.jpg)
**1.4 ??????㎑?????????**
??????????????????㎭???????????????????
## 2. ??????
# ?┩?????
- ?????? 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)