mysql的select
· 4 min read
为什么想看select的代码
有一个场景,遇到一个表只有十多万,但是表大小有几十g,为什么呢?因为有个字段是longtext.
放了很多很长的文本.发现select * from table limit 1000
就已经读不出来了
一个简单的select语句
select id from test wher id < 100 ;
这个流程究竟发生了什么?
第一步其实和php差不多,先是编译原理的前端几步lex和parse 第二步就是逻辑优化和物理优化, 其实可以想成是常用的编程语言的常量折叠或者数据流图的分析,就是编译时的优化 第三步也就是语义动作了,也就是真正的执行过程也可以想做是运行时: 但是条件对于读来说是不可见的,条件是作用于索引上 , 然后返回所有行 , 再根据列筛选出来,然后再join和排序,对于这个sql来说,他唯一作用就是通过索引读出内容,内存和硬盘对于他来说是不存在的
首先是读表,这个表是从ibd文件来的.所以终究是需要调用系统调用读文件,那么linux用系统调用是pread
索引在哪保存? 保存在表空间里面 内容保存到哪里? 保存到表空间里面 关联是在哪里发生? 发生在从索引从表空间读出来 关联是整整一行关联吗? 是的. 二级索引怎么读的? 通过二级索引读一级索引,一级索引读内容.
所以实际上是有一个语义上的层是: sql -> 语义动作 作用于索引 -> 索引访问表空间(有点像交换空间或者物理内存和虚拟内存的关系一样一样, 需要的时候才从硬盘硬盘加载)