溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Innodb中怎么查詢存儲(chǔ)引擎查

發(fā)布時(shí)間:2021-08-04 17:43:20 來源:億速云 閱讀:156 作者:Leah 欄目:數(shù)據(jù)庫(kù)

本篇文章給大家分享的是有關(guān)Innodb中怎么查詢存儲(chǔ)引擎查,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。


源碼分析
查詢于存儲(chǔ)引擎的實(shí)現(xiàn)密切相關(guān),因此,以下內(nèi)容主要針對(duì)Innodb存儲(chǔ)引擎的查詢處理進(jìn)行深入研究。對(duì)于查詢輸出的入口點(diǎn),本文從do_select()(sql\sql_select.cc)函數(shù)開始。該函數(shù)主要用于查詢匹配的結(jié)果,并將查詢結(jié)果通過socket傳輸,或者寫到數(shù)據(jù)表中。
首先看一下調(diào)用邏輯,如下所示:

do_select(): 查詢?nèi)肟诤瘮?shù)。
| sub_select(): 查詢部分join的記錄。循環(huán)調(diào)用ha_innobase::rnd_next()和evaluate_join_record()獲取并處理該部分的每條記錄。(sql\sql_select.cc:11705)
| | evaluate_join_record(): 處理一條查詢記錄。(sql\sql_select.cc:11758)
| | rr_sequential():調(diào)用ha_innobase::rnd_next()讀取下一條記錄。(sql\records.cc:452)
| | | ha_innobase::rnd_next(): 讀取下一條記錄。(storage\innobase\handler\ha_innodb.cc:6141)
| | | | ha_innobase::general_fetch(): 從給定的索引位置獲取下一條或上一條記錄。(storage\innobase\handler\ha_innodb.cc:5948)
| | | | | row_search_for_mysql(): 從數(shù)據(jù)庫(kù)中查詢一條記錄。以下分為6個(gè)階段分別處理各個(gè)部分。(storage\innobase\row\row0sel.c:3369)
| | | | | | 第一階段:釋放自適應(yīng)hash索引的鎖。
| | | | | | | rw_lock_get_writer()函數(shù)用于獲取讀寫鎖。如果獲取失敗,釋放目前的讀寫鎖。(storage\innobase\include\sync0rw.ic:122)
| | | | | | 第二階段:從預(yù)讀的cache中獲取記錄。
| | | | | | | row_sel_pop_cached_row_for_mysql():函數(shù)用于從cache中讀取一行記錄,(storage\innobase\row\row0sel.c:3167)
| | | | | | | | row_sel_copy_cached_field_for_mysql(): 函數(shù)讀取每個(gè)字段。(storage\innobase\row\row0sel.c:3134)
| | | | | | 第三階段:使用自適應(yīng)hash索引快速查找。
| | | | | | | row_sel_try_search_shortcut_for_mysql()函數(shù)使用hash索引獲取聚集索引的記錄。(storage\innobase\row\row0sel.c:3293)
| | | | | | | | row_sel_store_mysql_rec()函數(shù)將獲取的innobase格式的行記錄轉(zhuǎn)化為mysql格式。(storage\innobase\row\row0sel.c:2692)
| | | | | | | | | row_sel_field_store_in_mysql_format()函數(shù)將innobase格式的行記錄中的每個(gè)字段轉(zhuǎn)化為mysql格式。(storage\innobase\row\row0sel.c:2535)
| | | | | | 第四階段:打開并恢復(fù)索引的游標(biāo)位置。
| | | | | | | sel_restore_position_for_mysql(): 恢復(fù)索引的游標(biāo)位置。(storage\innobase\row\row0sel.c:3070)
| | | | | | | | btr_pcur_restore_position_func(): 恢復(fù)一個(gè)持久化游標(biāo)的位置。(storage\innobase\btr\btr0pcur.c:208)
| | | | | | | | | btr_cur_get_index(): 獲取索引。(storage\innobase\include\btr0pcur.ic:51)
| | | | | | | | | buf_page_optimistic_get():
| | | | | | | | | btr_pcur_get_rec(): 獲取持久化游標(biāo)的記錄。(storage\innobase\include\btr0pcur.ic:104)
| | | | | | | | | | btr_cur_get_rec (): 獲取當(dāng)前游標(biāo)位置的記錄。(storage\innobase\include\btr0pcur.ic:104)
| | | | | | | | | rec_get_offsets_func(): 獲取記錄中每個(gè)字段的偏移。(storage\innobase\rem\rem0rec.c:524)
| | | | | | | | btr_pcur_move_to_next(): 移動(dòng)持久化游標(biāo)到下一條記錄。(storage\innobase\include\btr0pcur.ic:342)
| | | | | | 第五階段:查找匹配的記錄。
| | | | | | | page_rec_is_infimum(): 查看當(dāng)前記錄是否是該頁(yè)的infinum記錄。infinum記錄表示比任何鍵值都小的記錄。(storage\innobase\include\page0page.ic:415)
| | | | | | | page_rec_is_supermum(): 查看當(dāng)前記錄是否是該頁(yè)的supermum記錄。supermum記錄表示比任何鍵值都大的記錄。(storage\innobase\include\page0page.ic:403)
| | | | | | | rec_get_next_offs(): 獲取相同頁(yè)中下一條記錄的偏移量。(storage\innobase\include\rem0rec.ic:325)
| | | | | | | rec_get_offsets_func(): 獲取記錄中每個(gè)字段的偏移。(storage\innobase\rem\rem0rec.c:524)
| | | | | | | rec_offs_validate():驗(yàn)證記錄的偏移量。(storage\innobase\rem\rem0rec.c:954)
| | | | | | | row_sel_store_mysql_rec()函數(shù)將獲取的innobase格式的行記錄轉(zhuǎn)化為mysql格式。(storage\innobase\row\row0sel.c:2692)
| | | | | | | | row_sel_field_store_in_mysql_format()函數(shù)將innobase格式的行記錄中的每個(gè)字段轉(zhuǎn)化為mysql格式。(storage\innobase\row\row0sel.c:2535)
| | | | | | | | btr_pcur_store_position(): 存儲(chǔ)游標(biāo)的位置。(storage\innobase\btr\btr0pcur.c:89)
| | | | | | | | | btr_pcur_get_block(): 獲取持久化游標(biāo)的緩沖塊。(storage\innobase\include\btr0pcur.ic:90)
| | | | | | | | | btr_pcur_get_page_cur(): 獲取持久化游標(biāo)的頁(yè)的游標(biāo)。(storage\innobase\include\btr0pcur.ic:64)
| | | | | | | | | page_cur_get_rec(): 獲取游標(biāo)位置的記錄。(storage\innobase\include\page0cur.ic:76)
| | | | | | | | | dict_index_copy_rec_order_prefix(): 拷貝記錄。(storage\innobase\dict\dict0dict.c:4185)
| | | | | | | | | | rec_copy_prefix_to_buf(): 拷貝記錄的字段到緩存buffer中。(storage\innobase\rem\rem0rec.c:1383)
| | | | | | | | | | dict_index_get_nth_field(): 獲取第n個(gè)字段的起始地址。(storage\innobase\include\dict0dict.ic:620)
| | | | | | | | | | dict_field_get_col(): 獲取第n個(gè)字段的值。(storage\innobase\include\dict0dict.ic:663)
| | | | | | 第六階段:移動(dòng)游標(biāo)到下一個(gè)索引記錄。
| | | | | | | btr_pcur_move_to_next(): 移動(dòng)持久化游標(biāo)到下一條記錄。(storage\innobase\include\btr0pcur.ic:342)
| | | | | | | mtr_commit(): 提交事務(wù)。(storage\innobase\mtr\mtr0mtr.c:247)

 
       從以上查詢邏輯,可以清晰的看出,MySQL的查詢時(shí)如何進(jìn)行的。概言之,如果可以從自適應(yīng)hash索引(在內(nèi)存中)中得到結(jié)果,獲取結(jié)果從innobase格式轉(zhuǎn)化為mysql格式并輸出;否則,根據(jù)索引的游標(biāo)位置,獲取當(dāng)前頁(yè)中的記錄,并拷貝當(dāng)前記錄到內(nèi)存,同樣將結(jié)果轉(zhuǎn)化為mysql格式輸出。
       從以上內(nèi)容中,很難看出輸出結(jié)果有什么規(guī)律。實(shí)際上,輸出的結(jié)果是有一定規(guī)律的,這種規(guī)律與innodb存儲(chǔ)引擎的設(shè)計(jì)和存儲(chǔ)密切相關(guān)。
innodb存儲(chǔ)引擎的存儲(chǔ)是按照B+索引將主鍵作為鍵值進(jìn)行聚集存儲(chǔ)的,如果不指定主鍵,系統(tǒng)會(huì)隱藏建立一個(gè)主鍵。innodb存儲(chǔ)引擎中所有的葉子節(jié)點(diǎn)為數(shù)據(jù)記錄,并且數(shù)據(jù)記錄邏輯上可以順序訪問。并且innodb存儲(chǔ)引擎的數(shù)據(jù)存儲(chǔ)和獲取是按照頁(yè)來進(jìn)行的。因此,在查詢時(shí),會(huì)將整個(gè)頁(yè)的數(shù)據(jù)加載到內(nèi)存中,innodb存儲(chǔ)引擎的默認(rèn)頁(yè)大小為16K。
實(shí)驗(yàn)測(cè)試
通過以上分析,不難理解,在查詢時(shí),簡(jiǎn)單查詢的輸出結(jié)果是一般按照B+索引的存儲(chǔ)順序排列的。為了進(jìn)一步的驗(yàn)證,進(jìn)行一下兩個(gè)實(shí)驗(yàn)
實(shí)驗(yàn)1
以簡(jiǎn)單的帶有主鍵的數(shù)據(jù)表student,表定義如下表所示。測(cè)試語句以簡(jiǎn)單的select * from student;為例,進(jìn)行測(cè)試。
 

CREATE TABLE `student` (
  `std_id` int(11) NOT NULL,
  `std_name` varchar(20) NOT NULL DEFAULT '""',
  `std_spec` varchar(20) NOT NULL DEFAULT '""',
  `std_***` tinyint(4) NOT NULL DEFAULT '0',
  `std_age` tinyint(3) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`std_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 
測(cè)試結(jié)果如下所示:
 

mysql> select * from student;
+------------+----------+-------------+---------+---------+
| std_id     | std_name | std_spec    | std_*** | std_age |
+------------+----------+-------------+---------+---------+
| 2012072301 | aaa      | computer    |       0 |      20 |
| 2012072303 | ccc      | computer    |       1 |      21 |
| 2012072304 | ddd      | computer    |       0 |      20 |
| 2012072305 | eee      | information |       0 |      22 |
| 2012072306 | fff      | computer    |       1 |      20 |
| 2012072307 | ggg      | computer    |       0 |      20 |
| 2012072308 | hhh      | computer    |       0 |      21 |
| 2012072309 | iii      | automatic   |       0 |      20 |
| 2012072310 | abc      | computer    |       1 |      20 |
| 2012072311 | kkk      | computer    |       0 |      18 |
| 2012072312 | lll      | computer    |       0 |      20 |
| 2012072313 | mmm      | computer    |       0 |      20 |
| 2012072314 | nnn      | computer    |       1 |      20 |
| 2012072315 | ooo      | information |       0 |      20 |
| 2012072316 | ppp      | computer    |       0 |      19 |
| 2012072317 | qqq      | computer    |       1 |      20 |
| 2012072318 | rrr      | information |       0 |      20 |
| 2012072319 | sss      | computer    |       1 |      20 |
| 2012072320 | ttt      | computer    |       0 |      20 |
| 2012072321 | uuu      | automatic   |       0 |      23 |
| 2012072322 | vvv      | computer    |       0 |      20 |
| 2012072323 | www      | computer    |       1 |      20 |
| 2012072324 | xxx      | computer    |       0 |      25 |
| 2012072325 | yyy      | automatic   |       0 |      20 |
| 2012072326 | zzz      | computer    |       1 |      20 |
| 2012080811 | bbb      | information |       0 |      20 |
+------------+----------+-------------+---------+---------+

 
實(shí)驗(yàn)2
       同樣以student表為例,將主鍵為2012080811的記錄更新操作,操作如下:
 

update student set std_id=2012072302 where std_id=2012080811;

 
       然后,在進(jìn)行測(cè)試,測(cè)試結(jié)果如下所示:
 

+------------+----------+-------------+---------+---------+
| std_id     | std_name | std_spec    | std_*** | std_age |
+------------+----------+-------------+---------+---------+
| 2012072301 | aaa      | computer    |       0 |      20 |
| 2012072302 | bbb      | information |       0 |      20 |
| 2012072303 | ccc      | computer    |       1 |      21 |
| 2012072304 | ddd      | computer    |       0 |      20 |
| 2012072305 | eee      | information |       0 |      22 |
| 2012072306 | fff      | computer    |       1 |      20 |
| 2012072307 | ggg      | computer    |       0 |      20 |
| 2012072308 | hhh      | computer    |       0 |      21 |
| 2012072309 | iii      | automatic   |       0 |      20 |
| 2012072310 | abc      | computer    |       1 |      20 |
| 2012072311 | kkk      | computer    |       0 |      18 |
| 2012072312 | lll      | computer    |       0 |      20 |
| 2012072313 | mmm      | computer    |       0 |      20 |
| 2012072314 | nnn      | computer    |       1 |      20 |
| 2012072315 | ooo      | information |       0 |      20 |
| 2012072316 | ppp      | computer    |       0 |      19 |
| 2012072317 | qqq      | computer    |       1 |      20 |
| 2012072318 | rrr      | information |       0 |      20 |
| 2012072319 | sss      | computer    |       1 |      20 |
| 2012072320 | ttt      | computer    |       0 |      20 |
| 2012072321 | uuu      | automatic   |       0 |      23 |
| 2012072322 | vvv      | computer    |       0 |      20 |
| 2012072323 | www      | computer    |       1 |      20 |
| 2012072324 | xxx      | computer    |       0 |      25 |
| 2012072325 | yyy      | automatic   |       0 |      20 |
| 2012072326 | zzz      | computer    |       1 |      20 |
+------------+----------+-------------+---------+---------+

以上就是Innodb中怎么查詢存儲(chǔ)引擎查,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI