题主假定按照主键检索。我们假定是等值查询。范围查询和表遍历情形可以在文末经推导得出。

primary key

A set of columns—and by implication, the index based on this set of columns—that can uniquely identify every row in a table. As such, it must be a unique index that does not contain any NULL values.

InnoDB requires that every table has such an index (also called the clustered index or cluster index), and organizes the table storage based on the column values of the primary key.

以上说明存储引擎是按照主键聚簇存储的。

With the exception of spatial indexes,InnoDBindexes are B-tree data structures. Spatial indexes use R-trees, which are specialized data structures for indexing multi-dimensional data. Index records are stored in the leaf pages of their B-tree or R-tree data structure. The default size of an index page is 16KB.

以上说明数据是按照B+树组织的。主键按照逻辑索引被存储在叶子结点上。所以我们只能继续去那里找数据。

An InnoDB page has seven parts:

Fil Header

Page Header

Infimum + Supremum Records

User Records

Free Space

Page Directory

Fil Trailer

以上说明物理页中没有现成的主键数据簇。

Similarly,InnoDBdoes not want to insert new rows according to the B-tree's key order (that would involve expensive shifting of large amounts of data), so it inserts new rows right after the end of the existing rows (at the top of the Free Space part) or wherever there's space left by a deleted row.

以上说明用户数据物理上在页内形成一个堆。

But by definition the records of a B-tree must be accessible in order by key value, so there is a record pointer in each record (the "next" field in the Extra Bytes) which points to the next record in key order. In other words, the records are a one-way linked list. SoInnoDBcan access rows in key order when searching.

以上说明这个物理堆形成一个逻辑有序的链表。假设这个查询可以命中。(细节) 存储引擎通过在页目录中二分查找到一个大致定位。并且最终找到了这条记录。接下来,我们要看看如何在物理记录中找到主键列数据。

The chart below shows the three parts of a physical record.

NameSizeField Start Offsets(F*1) or (F*2) bytesExtra Bytes6 bytesField Contentsdepends on content

Legend: The letter 'F' stands for 'Number Of Fields'.

The meaning of the parts is as follows:

The FIELD START OFFSETS is a list of numbers containing the information "where a field starts".

The EXTRA BYTES is a fixed-size header.

The FIELD CONTENTS contains the actual data.

以上说明一个物理记录分为三部分。乍一看我们也许需要继续查找主键在什么位置。

Clustered indexes

The clustered key (PRIMARY KEY) has one of the more complex record structures:

Cluster Key Fields: The cluster key fields, concatenated together (literally). InnoDB just concatenates the raw bytes of its internal storage formats per column type together into a single byte stream.

以上说明主键在物理记录中聚簇,可以直接提取。至此题主应该了解等值查询究竟都经历了些什么。至于范围查询和表遍历,找到第一个记录之后按照链表结构依次提交给游标即可。

所以主键的遍历也仅仅是不用在非键列中再查询一次而已。当然在索引和聚簇数据结构的帮助下,主键的查询投影开销一定是比较小的。

References:

mysql 扫描所有字段_select扫描mysql innodb表时,select只输出主键列,会不会扫描全表?...相关推荐

  1. mysql 增加主键列_MySQL添加列、删除列,创建主键等常用操作总结

    一. 列常用操作 ① 添加新的一列test_column,并将其作为主键,FIRST将其放在表中第一行,auto_increement是自动增长 alter table test_table add  ...

  2. mysql唯一索引和联合索引的区别_mysql中,索引,主键,唯一索引,联合索引的区别...

    索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针. 普通索引(由关键字KEY或INDEX定义的索引)的唯一任务是加快对数据的访问速度. ...

  3. mysql in和or扫描全表_MySQL对OR条件查询不支持优化,会进行全表扫描

    MySQL对OR条件查询不支持优化,会进行全表扫描:explain extended select * from like1 where name like 'abc%';例如:+----+----- ...

  4. mysql增加布尔字段_JDBC对MySQL数据库布尔字段的操作方法

    本文实例讲述了JDBC对MySQL数据库布尔字段的操作方法.分享给大家供大家参考.具体分析如下: 在Mysql数据库如果要使用布尔字段,而应该设置为BIT(1)类型 此类型在Mysql中不能通过MyS ...

  5. mysql 查看数据库字段是否存在,mysql查询某张表是否存在某个字段和判断是否存在某个表名...

    Mysql 1.判断一个表是否存在 语法: SELECT table_name FROM information_schema.TABLES WHERE table_name ='表名'; sql例子 ...

  6. mysql中在表中insert数据时,有重复主键id时,变成update

    MySQL 自4.1版以后开始支持INSERT - ON DUPLICATE KEY UPDATE语法 例如:  id name sex age  1 kathy male 23  2 Javer f ...

  7. mysql建表时建外键约束_数据库建表时一定要设置外键约束关系吗?

    如果被引用的表需要做分库分表,那么无法建立外键约束. 使用外键会降低数据库性能,这个说法并不细致.需要详细分析. 建立外键,那么一般引用字段上需要建立索引.如果不建立索引,被引用的表上删除数据,会全表 ...

  8. mysql 主键列_MySQL列属性 之 主键

    MySQL列属性 之 主键 主键 主键:primary key, 表中主要的键,每张表可以设置主键,主键可以是一个字段或者多个字段.多个字段联合起来做主键叫复合主键.主键是用来唯一标识一条记录的,不能 ...

  9. mysql 查询主键和主键列,查询Mysql表名、主键、列名

    Mysql有几个内部表,存放schema,表名,主键,索引,列名等信息. 此次做数仓迁移,用到这些,顺手整理一下: select t.table_schema,t.table_name,concat( ...

最新文章

  1. MQTT消息长度限制
  2. 【分布式事务】面试官问我:MySQL中的XA事务崩溃了如何恢复??
  3. GoogleNet - Going deeper with convolutions
  4. hive值乘以0.01保留一位小数_Hive窗口函数01-SUM、MIN、MAX、AVG
  5. python123.io作业_Python自动化开发学习3
  6. 产品观念:更好的捕鼠器_故事很重要:为什么您需要成为更好的讲故事的人
  7. Django的核心思想ORM
  8. 8-3:C++继承之继承中的作用域,隐藏,重定义和静态成员
  9. Clubhouse的不可能三角
  10. 半岛电视台员工遭iOS iMessage app零点击0day 漏洞利用攻击
  11. js动态生成表格(添加删除行操作)
  12. DataGridView使用技巧十二:DataGridView Error图标表示的设定
  13. 开机后显示服务器正在启动,电脑开机后卡在Windows正在启动界面上怎么办?
  14. 搜狗输入法自动打开问题
  15. web服务器服务不可用
  16. #离散#SSL 1231 VIJOS 1238 容易的网络游戏
  17. 7-115 计算油费
  18. 基于arcgis访问postgis的方法
  19. PS定义图案 和LOGO总结
  20. 通过url访问云服务器上的视频图片资源

热门文章

  1. 数据归一化 - MinMaxScaler()/MaxAbsScaler() - Python代码
  2. python中如何输入矩阵_python - 如何向矩阵中添加向量_numpy_酷徒编程知识库
  3. 对话系统聊天机器人的设计艺术(上)
  4. 深度学习在美团推荐平台排序中的运用
  5. 图谱实战 | 徐美兰:深度应用驱动的医学知识图谱构建
  6. 容器中用uwsgi协议部署注意的问题以及用flask部署
  7. 打造工业级推荐系统(三):推荐系统的工程实现与架构优化
  8. Linux基础命令---文本显示od
  9. 剥开比原看代码07:比原节点收到“请求区块数据”的信息后如何应答?
  10. 浮动在IE6,7下的一些问题