Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

On the location method of query in innodb

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

For the original reprint, please indicate the source. Version 5.7.14 involves the source file page0cur.ccpage0page.hpage0page.ccrem0cmp.cc.

Why do we talk about location method, because in innodb, such as an insert statement, we need to locate where to insert (PAGE_CUR_LE), such as a query statement, we need to locate to the first location where the data needs to be read, so location method is the foundation of the query. After finding the location of the record, it is actually stored in a structure called page_cur_t, which is temporarily called cursor cursor.

Struct page_cur_t {const dict_index_t* index; rec_t* rec; / *!

< pointer to a record on page */ ulint* offsets; buf_block_t* block; /*!< pointer to the block containing rec */}; 其中包含了本index的数据字典类容、实际的数据、记录所在块的信息等,下面我具体谈一下定位方法,同时结合源码来看它具体的实现。 我们先来明确一下概念: 记录(rec):通常为存储在内存中物理记录的完全拷贝,通常用一个unsigned char* 指针指向整个记录元组(dtuple):物理记录的逻辑体现,他就复杂得多,但是一个记录(rec)对应一个元组(dtuple),由dtuple_t结构体表示,其中每一个field由一个dfield_t结构体表示,数据存储在dfied_t的一个叫做void* data的指针中 可自行参考运维内参等其他书籍,这里就在简单描述到这里,本文会出现相关术语。 一、查询模式(search mode) 在innodb中的常用的search mode有如下几个 /* Page cursor search modes; the values must be in this order! */enum page_cur_mode_t { PAGE_CUR_UNSUPP = 0, PAGE_CUR_G = 1, PAGE_CUR_GE = 2, PAGE_CUR_L = 3, PAGE_CUR_LE = 4,}; PAGE_CUR_G(>

) PAGE_CUR_GE (> =) PAGE_CUR_L (=) PAGE_CUR_GE is taken from high (

< )PAGE_CUR_L和(mtype) { case DATA_FIXBINARY: case DATA_BINARY: case DATA_INT: case DATA_SYS_CHILD: case DATA_SYS: break; case DATA_BLOB: if (type->

Prtype & DATA_BINARY_TYPE) {break;} default: ret = cmp_data (type- > mtype, type- > prtype, dtuple_b_ptr, dtuple_f_len, rec_b_ptr, rec_f_len); if (! ret) {goto next_field } cur_bytes = 0; goto order_resolved;}

You can refer to the source code for details. I won't explain too much here.

Third, re-analysis of the method of intra-block binary query.

Why is it called reanalysis, because if the internal parameters of the operation and maintenance staff have already analyzed this function, here we mainly analyze the impact of the query pattern on the implementation of dichotomy, and use the diagram to explain that you will have new insights! Of course, if you don't know what slot is, please refer to the internal reference of operation and maintenance.

To put it simply, page_cur_search_with_match_bytes will call the cmp_dtuple_rec_with_match_bytes function to compare tuples with records, and the intra-block comparison method is to quickly narrow the scope by dichotomy search of all slot to a certain slot, and then use a similar dichotomy search method inside slot to wait for records. Let's mainly analyze the class dichotomy within slot. Because it is the perfect embodiment of the two rules in our query pattern, the following simplified code snippet and the comments I wrote:

/ * Perform linear search until the upper and lower records come to distance 1 of each other. * / while (page_rec_get_next_const (low_rec)! = up_rec) {/ / end the loop if the difference between low_rec and up_rec is 1, otherwise continue mid_rec = page_rec_get_next_const (low_rec) / / instead of dividing by 2 as mid_rec, you simply fetch the next row, because rec is a single linked list, so it's obviously easy to complete ut_pair_min (& cur_matched_fields, & cur_matched_bytes, low_matched_fields, low_matched_bytes, up_matched_fields, up_matched_bytes). Offsets = rec_get_offsets (mid_rec, index, offsets_, dtuple_get_n_fields_cmp (tuple), & heap); / / get the offset array of each field of the record cmp = cmp_dtuple_rec_with_match_bytes (tuple, mid_rec, index, offsets, & cur_matched_fields, & cur_matched_bytes) / / compare 0 to equal 1 tuple is larger than record-1 record is larger than tuple, and outgoing field and bytes if (cmp > 0) {/ / if tuple is larger than mid_rec record low_rec_match://, of course, simply assign mid_rec pointer to low_rec low_rec = mid_rec; low_matched_fields = cur_matched_fields Low_matched_bytes = cur_matched_bytes;} else if (cmp) {/ / if the tuple is less than the mid_rec record up_rec_match://, of course, simply assign the mid_rec pointer to the up_rec. This step can skip many records up_rec = mid_rec; up_matched_fields = cur_matched_fields Up_matched_bytes = cur_matched_bytes } / / the judgment of equality below is very critical. 1 algorithm / / if the tuple is equal to mid_rec else if (mode = = PAGE_CUR_G | | mode = = PAGE_CUR_LE / / if it is > (PAGE_CUR_G) and = (PAGE_CUR_GE) and PAGE_CUR_G and > = PAGE_CUR_GE are all fetched High / / if it is < PAGE_CUR_L and

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report