当前位置:网站首页>MYSQLg高级------回表
MYSQLg高级------回表
2022-08-11 03:26:00 【你若不离不弃,我必生死相依】
查看回表之前大家需要 先对 聚簇索引和非聚簇索引 innoDB和MyISAM有一定的了解; 有兴趣看自行查看 :地址
一、什么是回表?
这先要从InnoDB的索引实现说起,InnoDB有两大类索引:
聚集索引(clustered index)
普通索引(secondary index)
主键索引包含该行所有数据,普通索引包含的只有该索引和id
其实非聚集索引 的过程就是所谓的回表;
通俗的来讲就是:如果select 所需要获得列中有非索引列,一次索引查询不能获取所有的信息,需要到表中找到相应列的ID; 在根据ID去去查询
对应表中具体的列的数据,这个过程就是回表;而根据一次索引查询就能获取所有列的信息,就不需要回表;(也就是聚集索引)
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`deptId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`) ,
KEY `fk_dept_id`(`deptId`)
)ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8;
INSERT INTO `atguigudb`.`user` (`id`, `name`, `deptId`) VALUES (1, '小闫', 10);
INSERT INTO `atguigudb`.`user` (`id`, `name`, `deptId`) VALUES (2, '老闫', 10);
INSERT INTO `atguigudb`.`user` (`id`, `name`, `deptId`) VALUES (3, '小闫01', 10);
INSERT INTO `atguigudb`.`user` (`id`, `name`, `deptId`) VALUES (4, '小闫02', 10);
INSERT INTO `atguigudb`.`user` (`id`, `name`, `deptId`) VALUES (5, '小闫03', 10);
执行下面的就不需要回表,因为根据主键查询方式,只需要查询ID这颗B+树即可;主键是唯一的根据这个唯一索引,MYSQL就能确定搜索到这条记录;
id为主键索引,主键索引就是聚集索引
聚集索引的叶子节点包含整给行的记录,一次查询就能获取所有的信息,故不需要回表
# 直接访问id 找到对应的值
select id,name,deptId FROM USER WHERE name='3';
下面这个sql就是需要回表的:因为name 是普通的索引,他的查询的方式,需要先查询name的索引树,然后得到id的主键为3,再到id索引树进行一次查询。即先定位主键值,再定位记录,再这个过程中虽然使用了索引,但实际上底层进行了两次索引的查询,这给过程就是回表;
#非聚簇索引 根据值找到id 根据id找到对应的值
select id,name,deptId FROM USER WHERE name="小闫01";
select id,name,deptId FROM USER WHERE name='3';

详细的 请查看 博客
希望可以帮助大家更好的理解吧;不足之处,请大家批评指正;
边栏推荐
- 调试技巧总结
- DOM树的遍历-----修改样式,选择元素,创建和删除节点
- Window function application of sum and count
- Roewe imax8ev cube battery security, what blackening and swelling are hidden behind it?
- DOM-DOM tree, a DOM tree has three types of nodes
- The "top pillar" slides, and new growth is extremely difficult to shoulder the heavy responsibility. Is Ali "squatting" to jump higher?
- Talk about the understanding of RPC
- this question in js
- 互换性与测量技术——表面粗糙度选取和标注方法
- 构建程序化交易系统需要注意什么问题?
猜你喜欢

CSDN blog replacement skin

Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use

JS-DOM element object

Environment configuration of ESP32 (arduino arduino2.0 VScode platform which is easy to use?)

Official release丨VS Code 1.70

Idea (preferred) cherry-pick operation

Is Redis old?Performance comparison between Redis and Dragonfly

你不知道的 console.log 替代品

Getting Started with Raspberry Pi (5) System Backup

阿里低代码框架 lowcode-engine 之自定义物料篇
随机推荐
Goodbye Chongqing paper invoices!The issuance of electronic invoices for accommodation expenses will soon completely replace the invoices of hotels, catering and gas stations
What has programmatic trading changed?
Multi-merchant mall system function disassembly 26 lectures - platform-side distribution settings
大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?
console.log alternatives you didn't know about
重庆纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
js中的this问题
SQL 开发的十个高级概念
typedef定义结构体数组类型
Idea (优选)cherry-pick操作
df和df -lh的意思
IDE编译报错:Dangling metacharacter
荣威imax8ev魔方电池安全感,背后隐藏着哪些黑化膨胀?
7 sorting algorithms that are often tested in interviews
“京台高铁”亮相百度地图,真能在2035年建成吗?
[Pdf generated automatically bookmarks]
互换性与测量技术-公差原则与选用方法
论文精度 —— 2017 CVPR《High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis》
Realization of vending machine function based on FPGA state machine
Paper Accuracy - 2017 CVPR "High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis"