• GreatSQL社区原创内容未经受权不得随便应用,转载请分割小编并注明起源。
MySQL 的表有很多种,对表的操作次要是增删改查,明天来浅谈一下这些操作的底层代码和流程,以下以 tmp table为例子,为了更好的阐明操作流程,该表没有建设 primary key。

1.首先创立一张 tmp table,第三个参数 is_virtual=false 代表这不是虚构表,如果这个值设置为true那阐明这是虚构表,次要给存储过程建设长期表来存储参数的,这种虚构表没有handler,只能存储列信息和单行列值,不能寄存多行值,不能进行增删操作。

Table *table = create_tmp_table_from_fields(thd, *get_field_list(), false, options, table_alias);

创立表过程中会做以下操作:

1、初始化表:table->init_tmp_table2、用传进来的create_field 信息创立表的列:make_field3、创立表增删改查用的操作句柄:setup_tmp_table_handler

2、查找操作

table->file->ha_rnd_init(true) handler 初始化int rec = table->file->ha_rnd_next(table->record[0]) 从第一行开始查找,一次指针向下跳一行,table->record[0]用于存储查到的值对后果的判断:  switch (rec) {    case 0: {      查找胜利能够取出查到的值,此时该行值在table->field[col],col代表第几列      break;    }    case HA_ERR_END_OF_FILE:      if (table->file->ha_rnd_end()) return true; 查找到最初一行退出      break;    default:      查找出错解决

3、插入一行,这个操作绝对简略只有两步

for (ulonglong u = 0; u < upper_bound; ++u)    if (table->field[i]->store(u, true)) return true; 首先循环把值u存入第i列的record[0]if (write_row()) return true;间接把值从record[0]写入表即可

3、更新一行

  if (table->file->ha_rnd_init(true)) return true; 初始化handler  for (int i = 0; i < offset_table + 1; i++) {    if (table->file->ha_rnd_next(table->record[1])) return true; 让指针跳到指定要更新的那一行,留神这里该行的值存在record[1]  }  memcpy(table->record[0], table->record[1], (size_t)table->s->reclength);把值从record[1]拷贝到record[0]  (*value)->save_in_field(get_field(i), false); 把value值存入第i列的record[0],留神此时别的列值还是表里查到的值,这样record[0]就是新的值  if (table->file->ha_update_row(table->record[1], table->record[0])) 更新数据,这里record[1]是旧的值即表里的值,record[0]是新的值即待更新的值    return true;  if (table->file->ha_rnd_end()) return true; 完结本次handler

4、删除一行

table->file->ha_rnd_init(true) handler初始化  for (int i = 0; i < offset_table + 1; i++) {    if (table->file->ha_rnd_next(table->record[0])) return true; 让指针跳到指定要删除的那一行  }ret = table->file->ha_delete_row(table->record[0]); 删除以后行if (table->file->ha_rnd_end()) return true; 完结本次handler

5、敞开表

      close_tmp_table(table); 敞开长期表      free_tmp_table(table);  开释表资源

6、关上表

open_tmp_table(TABLE *table)这个关上表

以上是没有主键和索引的长期表操作,如果是有主键的表就波及到索引的查问操作,这期不波及这个知识点,下期再谈。

Enjoy GreatSQL :)

文章举荐:

面向金融级利用的GreatSQL正式开源
https://mp.weixin.qq.com/s/cI...

Changes in GreatSQL 8.0.25 (2021-8-18)
https://mp.weixin.qq.com/s/qc...

MGR及GreatSQL资源汇总
https://mp.weixin.qq.com/s/qX...

GreatSQL MGR FAQ
https://mp.weixin.qq.com/s/J6...

在Linux下源码编译装置GreatSQL/MySQL
https://mp.weixin.qq.com/s/WZ...

# 对于 GreatSQL

GreatSQL是由万里数据库保护的MySQL分支,专一于晋升MGR可靠性及性能,反对InnoDB并行查问个性,是实用于金融级利用的MySQL分支版本。

Gitee:

https://gitee.com/GreatSQL/Gr...

GitHub:

https://github.com/GreatSQL/G...

Bilibili:

https://space.bilibili.com/13...

微信&QQ群:

可搜寻增加GreatSQL社区助手微信好友,发送验证信息“加群”退出GreatSQL/MGR交换微信群

QQ群:533341697

微信小助手:wanlidbc

本文由博客一文多发平台 OpenWrite 公布!