推荐阅读:MySQL最全整理(面试题+笔记+导图),面试大厂不再被MySql难倒!
小史是一个应届生,虽然学的是电子专业,但是自己业余时间看了很多互联网与编程方面的书,一心想进BAT互联网公司。
![74aa240846d049b63c81cafcac955e85.png](https://img-blog.csdnimg.cn/img_convert/74aa240846d049b63c81cafcac955e85.png)
话说两个多月前,小史通过了A厂的一面,两个多月后的今天,小史终于等到了A厂的二面。
简单的自我介绍后,面试官看了看小史的简历,开始发问了。
![7962ebf31bcfee31d89a1095ac8af2ec.png](https://img-blog.csdnimg.cn/img_convert/7962ebf31bcfee31d89a1095ac8af2ec.png)
【面试现场】
![bcb1c9c5f436c7ce33d70776abdcaa40.png](https://img-blog.csdnimg.cn/img_convert/bcb1c9c5f436c7ce33d70776abdcaa40.png)
![1587799cb00547ecbf850a6909224149.png](https://img-blog.csdnimg.cn/img_convert/1587799cb00547ecbf850a6909224149.png)
小史:没问题,这个项目前端用的react+webpack,后端用的nginx+SpringBoot+Redis+MySql,前后端分离的,最后用docker进行容器化部署。主要模块有师生系统、课程系统、成绩系统、选课系统等。
![8c2e336ed87c98a5f80fe8803f7d7abc.png](https://img-blog.csdnimg.cn/img_convert/8c2e336ed87c98a5f80fe8803f7d7abc.png)
![1c5155ae72eb844a63c8b2c59fe6679e.png](https://img-blog.csdnimg.cn/img_convert/1c5155ae72eb844a63c8b2c59fe6679e.png)
![c6940a11531c4f1c8472865e9c469739.png](https://img-blog.csdnimg.cn/img_convert/c6940a11531c4f1c8472865e9c469739.png)
![fb781b57ecec72942780b73cce131ba1.png](https://img-blog.csdnimg.cn/img_convert/fb781b57ecec72942780b73cce131ba1.png)
![ff5bb5b9fcc9f5217eba348551ac2775.png](https://img-blog.csdnimg.cn/img_convert/ff5bb5b9fcc9f5217eba348551ac2775.png)
这个项目的架构和说辞,小史早已背得溜溜的。
![95659b012faa8a9681e83c2c86cda838.png](https://img-blog.csdnimg.cn/img_convert/95659b012faa8a9681e83c2c86cda838.png)
小史:底层mysql是存储,redis是缓存,dao层操作mysql,cache层操作redis,service层处理业务逻辑,rest api层为前端提供rest接口。前端这边用react进行模块化,webpack打包部署。网关nginx进行负载均衡。mysql、redis、nginx和springboot应用都放在docker里部署。
![7949dfa9dfadfc77d607bc198e428e27.png](https://img-blog.csdnimg.cn/img_convert/7949dfa9dfadfc77d607bc198e428e27.png)
![f6210fa108d5cf036aaef7ec60570e20.png](https://img-blog.csdnimg.cn/img_convert/f6210fa108d5cf036aaef7ec60570e20.png)
![1b022cd87b30417321e6609a3a13675d.png](https://img-blog.csdnimg.cn/img_convert/1b022cd87b30417321e6609a3a13675d.png)
![1679141bb1d9073691fa73d04345d58c.png](https://img-blog.csdnimg.cn/img_convert/1679141bb1d9073691fa73d04345d58c.png)
![475f3ffeef2e78f64a352d7d2259797d.png](https://img-blog.csdnimg.cn/img_convert/475f3ffeef2e78f64a352d7d2259797d.png)
![96d1cf47c2ce5ae1eef2528d77a3e53c.png](https://img-blog.csdnimg.cn/img_convert/96d1cf47c2ce5ae1eef2528d77a3e53c.png)
![ad7878e2d6cd5f0084ab25485aeda14b.png](https://img-blog.csdnimg.cn/img_convert/ad7878e2d6cd5f0084ab25485aeda14b.png)
![c274a3cf592d06610751fea5f5eb0dfd.png](https://img-blog.csdnimg.cn/img_convert/c274a3cf592d06610751fea5f5eb0dfd.png)
![d8df963b022e58e113183a00ab5018d6.png](https://img-blog.csdnimg.cn/img_convert/d8df963b022e58e113183a00ab5018d6.png)
![07b9bd57b05565406962e84e16e7d8fb.png](https://img-blog.csdnimg.cn/img_convert/07b9bd57b05565406962e84e16e7d8fb.png)
![9e3d2c0256017584fa54c3cec87c5cb4.png](https://img-blog.csdnimg.cn/img_convert/9e3d2c0256017584fa54c3cec87c5cb4.png)
![6a56f5822682953e4d9de9e70597e30b.png](https://img-blog.csdnimg.cn/img_convert/6a56f5822682953e4d9de9e70597e30b.png)
![00b33bf262d5178cf8ff039072ccc78b.png](https://img-blog.csdnimg.cn/img_convert/00b33bf262d5178cf8ff039072ccc78b.png)
![58b5b88058deb154a48bec2b0a872d1e.png](https://img-blog.csdnimg.cn/img_convert/58b5b88058deb154a48bec2b0a872d1e.png)
题目:为什么MySQL数据库要用B+树存储索引?
小史听到这个题目,陷入了回忆。
【前段时间的饭局】
话说吕老师给小史讲完人工智能后,他们一起回家吃小史姐姐做的饭去了。
![0752fd2db46de55c28edd845faa85ef1.png](https://img-blog.csdnimg.cn/img_convert/0752fd2db46de55c28edd845faa85ef1.png)
![6d41cb4385a1ee3e0e8cfb4d84856799.png](https://img-blog.csdnimg.cn/img_convert/6d41cb4385a1ee3e0e8cfb4d84856799.png)
【饭后】
![089a660ec7907dd7517a720748f65e8c.png](https://img-blog.csdnimg.cn/img_convert/089a660ec7907dd7517a720748f65e8c.png)
![ecec40cb34cbdb0182ac28b68d66de13.png](https://img-blog.csdnimg.cn/img_convert/ecec40cb34cbdb0182ac28b68d66de13.png)
吕老师:面试的时候一定是往深了问,不精通的话容易吃亏。不过面试时一般都是根据项目来问,项目中用到的技术,一定要多看看原理,特别是能和数据结构和算法挂钩的那部分。
![b1a15e8aff968f867d717b9274ba5bdf.png](https://img-blog.csdnimg.cn/img_convert/b1a15e8aff968f867d717b9274ba5bdf.png)
![ac0c22b1be4f2366639e5d96c2ab760e.png](https://img-blog.csdnimg.cn/img_convert/ac0c22b1be4f2366639e5d96c2ab760e.png)
![4d085f733062744c63ab2d27884140cd.png](https://img-blog.csdnimg.cn/img_convert/4d085f733062744c63ab2d27884140cd.png)
小史:树的话,无非就是前中后序遍历、二叉树、二叉搜索树、平衡二叉树,更高级一点的有红黑树、B树、B+树,还有之前你教我的字典树。
【红黑树】
![28d007c614a9de5ea112b9f28a3cc6bd.png](https://img-blog.csdnimg.cn/img_convert/28d007c614a9de5ea112b9f28a3cc6bd.png)
一听到红黑树,小史头都大了,开始抱怨了起来。
![39ce2d435c30fd7b210057b4685ea67f.png](https://img-blog.csdnimg.cn/img_convert/39ce2d435c30fd7b210057b4685ea67f.png)
小史:红黑树看过很多遍了,但是每次都记不住,它的规则实在是太多了,光定义就有四五条规则,还有插入删除的时候,需要调整树,复杂得很。
![ad032d3ccbfd79318c23212fc525f8d7.png](https://img-blog.csdnimg.cn/img_convert/ad032d3ccbfd79318c23212fc525f8d7.png)
吕老师:小史,问你红黑树,并不是让你背诵它的定义,或者让你手写一个红黑树,而是想问问你它为什么这样设计,它的使用场景有哪些。
![b0e7c30672653ac0739bb3be7b3ff778.png](https://img-blog.csdnimg.cn/img_convert/b0e7c30672653ac0739bb3be7b3ff778.png)
![37384acc88caef8b965c3975b084d430.png](https://img-blog.csdnimg.cn/img_convert/37384acc88caef8b965c3975b084d430.png)
![732d17d7b2995c33165966715c7af3ab.png](https://img-blog.csdnimg.cn/img_convert/732d17d7b2995c33165966715c7af3ab.png)
![5db84d1b6e2bb5e60dcd72621caf6622.png](https://img-blog.csdnimg.cn/img_convert/5db84d1b6e2bb5e60dcd72621caf6622.png)
![47547578b125c585c1e29c37ae8ea330.png](https://img-blog.csdnimg.cn/img_convert/47547578b125c585c1e29c37ae8ea330.png)
![4fbd2530033f9d0ee7fa81726ee9bd70.png](https://img-blog.csdnimg.cn/img_convert/4fbd2530033f9d0ee7fa81726ee9bd70.png)
![2c3192a033842c09793e5498fd01f892.png](https://img-blog.csdnimg.cn/img_convert/2c3192a033842c09793e5498fd01f892.png)
![938b48aa10d387873c128f1f2dcc2f85.png](https://img-blog.csdnimg.cn/img_convert/938b48aa10d387873c128f1f2dcc2f85.png)
![4f4e655c707b506263ea52f05c4256d9.png](https://img-blog.csdnimg.cn/img_convert/4f4e655c707b506263ea52f05c4256d9.png)
![c18a155136e9b31a83834472c1f7677c.png](https://img-blog.csdnimg.cn/img_convert/c18a155136e9b31a83834472c1f7677c.png)
![adbfcdcca1811d108d32b98f0098ac96.png](https://img-blog.csdnimg.cn/img_convert/adbfcdcca1811d108d32b98f0098ac96.png)
![2b817cfd1dd57d6ed630eda101a8c5d2.png](https://img-blog.csdnimg.cn/img_convert/2b817cfd1dd57d6ed630eda101a8c5d2.png)
![c143d47e7e6191383950547dca965ede.png](https://img-blog.csdnimg.cn/img_convert/c143d47e7e6191383950547dca965ede.png)
![5232263c0fbb6267decd34ccab50a6bf.png](https://img-blog.csdnimg.cn/img_convert/5232263c0fbb6267decd34ccab50a6bf.png)
![a3b641d302df76eb1d464370df61f8dc.png](https://img-blog.csdnimg.cn/img_convert/a3b641d302df76eb1d464370df61f8dc.png)
【B树】
![fef79dbc81c5889a592b1aaf3491ca0a.png](https://img-blog.csdnimg.cn/img_convert/fef79dbc81c5889a592b1aaf3491ca0a.png)
![b0a160400c343ddcf0f57d763d648f75.png](https://img-blog.csdnimg.cn/img_convert/b0a160400c343ddcf0f57d763d648f75.png)
![028522b4f1e933ce1905cba96ebbf5e9.png](https://img-blog.csdnimg.cn/img_convert/028522b4f1e933ce1905cba96ebbf5e9.png)
![d7a82174de6346d03a95540970ce3ad2.png](https://img-blog.csdnimg.cn/img_convert/d7a82174de6346d03a95540970ce3ad2.png)
![d2bfb74cf620ce2075a9a1fc105d33ab.png](https://img-blog.csdnimg.cn/img_convert/d2bfb74cf620ce2075a9a1fc105d33ab.png)
![cc02964fe6be6777a1cf15ab03d82e26.png](https://img-blog.csdnimg.cn/img_convert/cc02964fe6be6777a1cf15ab03d82e26.png)
![b2f4e7f4ed29d4a70fbd212d9df7a5ff.png](https://img-blog.csdnimg.cn/img_convert/b2f4e7f4ed29d4a70fbd212d9df7a5ff.png)
![9c3ba488385912bbf4eb035490686454.png](https://img-blog.csdnimg.cn/img_convert/9c3ba488385912bbf4eb035490686454.png)
![4cbaa4af8adf319e19c753ebe0b707ef.png](https://img-blog.csdnimg.cn/img_convert/4cbaa4af8adf319e19c753ebe0b707ef.png)
![27ca890b779bec7c36578311b9e21078.png](https://img-blog.csdnimg.cn/img_convert/27ca890b779bec7c36578311b9e21078.png)
![719d55c5af8f2b33107d3b965a75ad0f.png](https://img-blog.csdnimg.cn/img_convert/719d55c5af8f2b33107d3b965a75ad0f.png)
![29e223e040445bb905af17649abdca94.png](https://img-blog.csdnimg.cn/img_convert/29e223e040445bb905af17649abdca94.png)
![cd871a0c6fcec81284a8af3e9c4e5f50.png](https://img-blog.csdnimg.cn/img_convert/cd871a0c6fcec81284a8af3e9c4e5f50.png)
![82faf3e805e5e4da47a14fd5e4274c48.png](https://img-blog.csdnimg.cn/img_convert/82faf3e805e5e4da47a14fd5e4274c48.png)
![203420f72b0bb6db364080a2a39687ce.png](https://img-blog.csdnimg.cn/img_convert/203420f72b0bb6db364080a2a39687ce.png)
![435fe6221fcec34afe9bd7dd81fdc698.png](https://img-blog.csdnimg.cn/img_convert/435fe6221fcec34afe9bd7dd81fdc698.png)
![a98423a4d24026d4a87ad76e88080c50.png](https://img-blog.csdnimg.cn/img_convert/a98423a4d24026d4a87ad76e88080c50.png)
吕老师:小史,你要知道,文件系统和数据库的索引都是存在硬盘上的,并且如果数据量大的话,不一定能一次性加载到内存中。
![090e17cb7279baf6abfa02ea2a03e432.png](https://img-blog.csdnimg.cn/img_convert/090e17cb7279baf6abfa02ea2a03e432.png)
两个月前,小史面试没考虑内存情况差点挂了
![a9761278fbbbd66fc78f591f33507c7b.png](https://img-blog.csdnimg.cn/img_convert/a9761278fbbbd66fc78f591f33507c7b.png)
![45debff045909368da02f4c90e896be2.png](https://img-blog.csdnimg.cn/img_convert/45debff045909368da02f4c90e896be2.png)
![fe713b9eaa2fcdbb85bcebb4d6ccfd55.png](https://img-blog.csdnimg.cn/img_convert/fe713b9eaa2fcdbb85bcebb4d6ccfd55.png)
![3fe8abd88a9ca782f6d611d391c3dbc7.png](https://img-blog.csdnimg.cn/img_convert/3fe8abd88a9ca782f6d611d391c3dbc7.png)
![0c3b003169924450ebc079c5c087b6ee.png](https://img-blog.csdnimg.cn/img_convert/0c3b003169924450ebc079c5c087b6ee.png)
![e656f302c85158c1a6dc2819df45833a.png](https://img-blog.csdnimg.cn/img_convert/e656f302c85158c1a6dc2819df45833a.png)
【B+树】
![d9893078d92f25f999bfcb799fecf53d.png](https://img-blog.csdnimg.cn/img_convert/d9893078d92f25f999bfcb799fecf53d.png)
![11c8c9a2bc127891e2f69438b10dc163.png](https://img-blog.csdnimg.cn/img_convert/11c8c9a2bc127891e2f69438b10dc163.png)
![d572477fc5725b1beb6fed3e3cf91d91.png](https://img-blog.csdnimg.cn/img_convert/d572477fc5725b1beb6fed3e3cf91d91.png)
![d3d1497ec286eef3b82031461c2c5e5e.png](https://img-blog.csdnimg.cn/img_convert/d3d1497ec286eef3b82031461c2c5e5e.png)
![9456897a84517162a165b331105292d7.png](https://img-blog.csdnimg.cn/img_convert/9456897a84517162a165b331105292d7.png)
![cd84575a12118f0e7b9acb697775a163.png](https://img-blog.csdnimg.cn/img_convert/cd84575a12118f0e7b9acb697775a163.png)
![8e2311b364c691a9b90509d68af7984e.png](https://img-blog.csdnimg.cn/img_convert/8e2311b364c691a9b90509d68af7984e.png)
吕老师:这也是和业务场景相关的,你想想,数据库中select数据,不一定只选一条,很多时候会选多条,比如按照id排序后选10条。
![0b5fc56456989d4597262ec740d3652d.png](https://img-blog.csdnimg.cn/img_convert/0b5fc56456989d4597262ec740d3652d.png)
小史:我明白了,如果是多条的话,B树需要做局部的中序遍历,可能要跨层访问。而B+树由于所有数据都在叶子结点,不用跨层,同时由于有链表结构,只需要找到首尾,通过链表就能把所有数据取出来了。
![e52a6ada1265c688162956224c22c948.png](https://img-blog.csdnimg.cn/img_convert/e52a6ada1265c688162956224c22c948.png)
![79b9f6fa995d8232e61c052047ff9a5d.png](https://img-blog.csdnimg.cn/img_convert/79b9f6fa995d8232e61c052047ff9a5d.png)
【回到现场】
![58b5b88058deb154a48bec2b0a872d1e.png](https://img-blog.csdnimg.cn/img_convert/58b5b88058deb154a48bec2b0a872d1e.png)
![c3057a15fa38535de30357690c699f0b.png](https://img-blog.csdnimg.cn/img_convert/c3057a15fa38535de30357690c699f0b.png)
小史:这和业务场景有关。如果只选一个数据,那确实是hash更快。但是数据库中经常会选择多条,这时候由于B+树索引有序,并且又有链表相连,它的查询效率比hash就快很多了。
![05416dba2a198dccf7007549c4c00e73.png](https://img-blog.csdnimg.cn/img_convert/05416dba2a198dccf7007549c4c00e73.png)
小史:而且数据库中的索引一般是在磁盘上,数据量大的情况可能无法一次装入内存,B+树的设计可以允许数据分批加载,同时树的高度较低,提高查找效率。
![b936a2f4e5e238aef33cd1738cb97523.png](https://img-blog.csdnimg.cn/img_convert/b936a2f4e5e238aef33cd1738cb97523.png)
HR和小史简单地聊了聊基本情况,这次面试就结束了。
小史走后,面试官在系统中写下了面试评语:
![4890bbd3f6f71a6fc70f019f73a4a6c2.png](https://img-blog.csdnimg.cn/img_convert/4890bbd3f6f71a6fc70f019f73a4a6c2.png)
几天后,小史收到了A厂的offer。
![627ab5b6f93f05e8c0a22c4fcee4f7c5.png](https://img-blog.csdnimg.cn/img_convert/627ab5b6f93f05e8c0a22c4fcee4f7c5.png)
![9541c42e81fac031952190c59d6167b5.png](https://img-blog.csdnimg.cn/img_convert/9541c42e81fac031952190c59d6167b5.png)