溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL中查詢字段數(shù)量多少會對查詢效率有影響

發(fā)布時間:2021-10-29 17:06:57 來源:億速云 閱讀:150 作者:小新 欄目:MySQL數(shù)據(jù)庫

這篇文章將為大家詳細講解有關MySQL中查詢字段數(shù)量多少會對查詢效率有影響,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

一、問題由來

我們知道執(zhí)行計劃的不同肯定會帶來效率的不同,但是在本例中執(zhí)行計劃完全一致,都是全表掃描,不同的只有字段個數(shù)而已。其次,測試中都使用了where條件進行過濾(Using where),過濾后沒有數(shù)據(jù)返回,我們常說的where過濾實際上是在MySQL層,當然某些情況下使用ICP會提前在Innodb層過濾數(shù)據(jù),這里我們先不考慮ICP,我會在后面的文章中詳細描述ICP的流程,本文也會給出where過濾的接口,供大家參考。

下面的截圖來自兩個朋友,感謝他們的測試和問題提出。另外對于大數(shù)據(jù)量訪問來講可能涉及到物理IO,首次訪問和隨后的訪問因為Innodb buffer的關系,效率不同是正常,需要多測試幾次。

測試1:

MySQL中查詢字段數(shù)量多少會對查詢效率有影響

MySQL中查詢字段數(shù)量多少會對查詢效率有影響

MySQL中查詢字段數(shù)量多少會對查詢效率有影響

測試2:

MySQL中查詢字段數(shù)量多少會對查詢效率有影響

我們通過這兩個測試,可以發(fā)現(xiàn)隨著字段的不斷減少,效率越來越高,并且主要的區(qū)別都在sending data 下面,這個狀態(tài)我曾經(jīng)大概描述過參考文章:
https://www.jianshu.com/p/46ad0aaf7ed7
https://www.jianshu.com/p/4cdec711adef

簡單的說Innodb數(shù)據(jù)的獲取和Innodb數(shù)據(jù)到MySQL層數(shù)據(jù)的傳遞都包含在其中。

二、簡單的流程介紹

下面我主要結合字段多少和全表掃描2個方面做一個簡單的流程介紹。實際上其中有一個核心接口就是row_search_mvcc,它大概包含了如下功能:

  • 通過預取緩存獲取數(shù)據(jù)

  • 打開事務

  • 定位索引位置(包含使用AHI快速定位)

  • 是否開啟readview

  • 通過持久化游標不斷訪問下一條數(shù)據(jù)

  • 加Innodb表鎖、加Innodb行鎖

  • 可見性判斷

  • 根據(jù)主鍵回表(可能回表需要加行鎖)

  • ICP優(yōu)化

  • SEMI update優(yōu)化

并且作為訪問數(shù)據(jù)的必須經(jīng)歷的接口,這個函數(shù)也是很值得大家細細研讀的。

1. 通過select字段構建read_set(MySQL層)

首先需要構建一個叫做read_set的位圖,來表示訪問的字段位置及數(shù)量。它和write set一起,在記錄binlog的Event的時候也會起著重要作用,可以參考我的《深入理解MySQL主從原理》中關于binlog_row_image參數(shù)一節(jié)。這里構建的主要接口為TABLE::mark_column_used函數(shù),每個需要訪問的字段都會調(diào)用它來設置自己的位圖。下面是其中的一段如下:

case MARK_COLUMNS_READ:
    bitmap_set_bit(read_set, field->field_index);

從棧幀來看這個構建read_set的過程位于狀態(tài)‘init’下面。棧幀見結尾棧幀1。

2. 初次訪問定位的時候還會構建一個模板(mysql_row_templ_t)(Innodb層)

本模板主要用于當Innodb層數(shù)據(jù)到MySQL層做轉(zhuǎn)換的時候使用,其中記錄了使用的字段數(shù)量、字段的字符集、字段的類型等等。接口build_template_field用于構建這個模板。棧幀見結尾棧幀2。
但是需要注意的是,這里構建模板就會通過我們上面說的read_set去判斷到底有多少字段需要構建到模板中,然后才會調(diào)用build_template_field函數(shù)。如下是最重要的代碼,它位于build_template_needs_field接口中。

bitmap_is_set(table->read_set, static_cast<uint>(i)

可以看到這里正在測試本字段是否出現(xiàn)在了read_set中,如果不在則跳過這個字段。下面是函數(shù)build_template_needs_field的注釋:

Determines if a field is needed in a m_prebuilt struct 'template'.
@return field to use, or NULL if the field is not needed */

到這里我們需要訪問的字段已經(jīng)確立下來了

3. 初次定位數(shù)據(jù),定位游標到主鍵索引的第一行記錄,為全表掃描做好準備(Innodb層)

對于這種全表掃描的執(zhí)行方式,定位數(shù)據(jù)就變得簡單了,我們只需要找到主鍵索引的第一條數(shù)據(jù)就好了,它和平時我們使用(ref/range)定位方式不同,不需要二分法的支持。因此對于全表掃描的初次定位調(diào)用函數(shù)為btr_cur_open_at_index_side_func,而不是通常我們說的btr_pcur_open_with_no_init_func。
如果大概看一下函數(shù)btr_cur_open_at_index_side_func的功能,我們很容易看到,它就是通過B+樹結構,定位到葉子結點的開頭第一個塊,然后調(diào)用函數(shù)page_cur_set_before_first,將游標放到了所有記錄的開頭,目的只有一個為全表掃描做好準備。棧幀見結尾棧幀3。
注意這里正是通過我們row_search_mvcc調(diào)用下去的。

4. 獲取Innodb層的第一條數(shù)據(jù)(Innodb層)

拿到了游標過后就可以獲取數(shù)據(jù)了,這里也很簡單代碼就是一句如下:

rec = btr_pcur_get_rec(pcur);//獲取記錄 從持久化游標   整行數(shù)據(jù)

但是需要注意的是這里獲取的數(shù)據(jù)只是一個指針,言外之意可以理解為整行數(shù)據(jù),其格式也是原始的Innodb數(shù)據(jù),其中還包含了一些偽列比如(rollback ptr和trx id)。這里實際上和訪問的字段個數(shù)無關。

5. 將第一行記錄轉(zhuǎn)換為MySQL格式(Innodb層)

這一步完成后我們可以認為記錄已經(jīng)返回給了MySQL層,這里就是實際的數(shù)據(jù)拷貝了,并不是指針,整個過程放到了函數(shù)row_sel_store_mysql_rec中。
我們前面的模板(mysql_row_templ_t)也會在這里發(fā)揮它的作用,這是一個字段過濾的過程,我們先來看一個循環(huán)

for (i = 0; i < prebuilt->n_template; i++)

其中prebuilt->n_template就是字段模板的個數(shù),我們前面已經(jīng)說過了,通過read_set的過濾,對于我們不需要的字段是不會建立模板的。因此這里的模板數(shù)量是和我們訪問的字段個數(shù)一樣的。

然后在這個循環(huán)下面會調(diào)用row_sel_store_mysql_field_func然后調(diào)用row_sel_field_store_in_mysql_format_func將字段一個一個轉(zhuǎn)換為MySQL的格式。我們來看一下其中一種類型的轉(zhuǎn)換如下:

    case DATA_INT:
        /* Convert integer data from Innobase to a little-endian
        format, sign bit restored to normal */
        ptr = dest + len;
        for (;;) {
            ptr--;
            *ptr = *data;//值拷貝 內(nèi)存拷貝
            if (ptr == dest) {
                break;
            }
            data++;
        }

我們可以發(fā)現(xiàn)這是一種實際的轉(zhuǎn)換,也就是需要花費內(nèi)存空間的。棧幀見結尾棧幀4。
到這里我們大概知道了,查詢的字段越多那么著這里轉(zhuǎn)換的過程越長,并且這里都是實際的內(nèi)存拷貝

最終這行數(shù)據(jù)會存儲到row_search_mvcc的形參 buffer中返回給MySQL層,這個形參的注釋如下:

@param[out]    buf        buffer for the fetched row in MySQL format
6.對第一條數(shù)據(jù)進行where過濾(MySQL層)

拿到數(shù)據(jù)后當然還不能作為最終的結果返回給用戶,我們需要在MySQL層做一個過濾操作,這個條件比較位于函數(shù)evaluate_join_record的開頭,其中比較就是下面一句話

found= MY_TEST(condition->val_int()); //進行比較 調(diào)用到 條件和 返回會記錄的比較

如果和條件不匹配將會返回False。這里比較會最終調(diào)用Item_func的各種方法,如果等于則是Item_func_eq,棧幀見結尾棧幀5。

7.訪問下一條數(shù)據(jù)

上面我已經(jīng)展示了訪問第一條數(shù)據(jù)的大體流程,接下面需要做的就是繼續(xù)訪問下去,如下:

  • 移動游標到下一行

  • 訪問數(shù)據(jù)

  • 根據(jù)模板轉(zhuǎn)換數(shù)據(jù)返回給MySQL層

  • 根據(jù)where條件過濾

整個過程會持續(xù)到全部主鍵索引數(shù)據(jù)訪問完成。但是需要注意的是上層接口有些變化,由ha_innobase::index_first會變?yōu)閔a_innobase::rnd_next,統(tǒng)計數(shù)據(jù)由Handler_read_first變?yōu)镠andler_read_rnd_next,這點可以參考我的文章:
https://www.jianshu.com/p/25fed8f1f05e
并且row_search_mvcc的流程肯定也會有變化。這里不再熬述。但是實際的獲取數(shù)據(jù)轉(zhuǎn)換過程和過濾過程并沒有改變。

注意了這些步驟除了步驟1,基本都處于sending data下面。

三、回到問題本身

好了到這里我們大概知道全表掃描的訪問數(shù)據(jù)的流程了,我們就來看看一下在全表掃描流程中字段的多少到底有哪些異同點:

不同點:
  • 構建的read_set 不同,字段越多read_set中為‘1’的位數(shù)越多

  • 建立的模板不同,字段越多模板數(shù)量越多

  • 每行數(shù)據(jù)轉(zhuǎn)換為MySQL格式的時候不同,字段越多模板越多,那么循環(huán)轉(zhuǎn)換每個字段的循環(huán)次數(shù)也就越多,并且這是每行都要處理的。

  • 返回給MySQL層的行內(nèi)存消耗越大

相同點:
  • 訪問的行數(shù)一致

  • 訪問的流程一致

  • where過濾的方式一致

在整個不同點中,我認為最耗時的部分應該是每行數(shù)據(jù)轉(zhuǎn)換為MySQL格式的消耗最大,因為每行每個字段都需要做這樣的轉(zhuǎn)換,這也剛好是除以sending data狀態(tài)下面。我們線上大于10個字段的表比比皆是,如果我們只需要訪問其中的少量字段,我們最好還是寫實際的字段而不是‘*’,來規(guī)避這個問題。

四、寫在最后

雖然本文中以全表掃描為列進行了解釋,但是實際上任何情況下我們都應該縮減訪問字段的數(shù)量,應該只訪問需要的字段。

五、備用棧幀

棧幀1 read_set構建

#0  TABLE::mark_column_used (this=0x7ffe7c996c50, thd=0x7ffe7c000b70, field=0x7ffe7c997c88, mark=MARK_COLUMNS_READ)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/table.cc:6344
#1  0x00000000015449b4 in find_field_in_table_ref (thd=0x7ffe7c000b70, table_list=0x7ffe7c0071f0, name=0x7ffe7c006a38 "id", length=2, item_name=0x7ffe7c006a38 "id", 
    db_name=0x0, table_name=0x0, ref=0x7ffe7c006bc0, want_privilege=1, allow_rowid=true, cached_field_index_ptr=0x7ffe7c0071a0, register_tree_change=true, 
    actual_table=0x7fffec0f46d8) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/sql_base.cc:7730
#2  0x0000000001544efc in find_field_in_tables (thd=0x7ffe7c000b70, item=0x7ffe7c0070c8, first_table=0x7ffe7c0071f0, last_table=0x0, ref=0x7ffe7c006bc0, 
    report_error=IGNORE_EXCEPT_NON_UNIQUE, want_privilege=1, register_tree_change=true) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/sql_base.cc:7914
#3  0x0000000000faadd8 in Item_field::fix_fields (this=0x7ffe7c0070c8, thd=0x7ffe7c000b70, reference=0x7ffe7c006bc0)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/item.cc:5857
#4  0x00000000015478ee in setup_fields (thd=0x7ffe7c000b70, ref_pointer_array=..., fields=..., want_privilege=1, sum_func_list=0x7ffe7c005d90, allow_sum_func=true, 
    column_update=false) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/sql_base.cc:9047
#5  0x000000000161419d in st_select_lex::prepare (this=0x7ffe7c005c30, thd=0x7ffe7c000b70) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/sql_resolver.cc:190

棧幀2 構建模板

#0  build_template_field (prebuilt=0x7ffe7c99b880, clust_index=0x7ffe7c999c20, index=0x7ffe7c999c20, table=0x7ffe7c996c50, field=0x7ffe7c997c88, i=0, v_no=0)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/handler/ha_innodb.cc:7571
#1  0x00000000019d1dc1 in ha_innobase::build_template (this=0x7ffe7c997610, whole_row=false)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/handler/ha_innodb.cc:8034
#2  0x00000000019d60f5 in ha_innobase::change_active_index (this=0x7ffe7c997610, keynr=0)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/handler/ha_innodb.cc:9805
#3  0x00000000019d682b in ha_innobase::rnd_init (this=0x7ffe7c997610, scan=true)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/handler/ha_innodb.cc:10031
#4  0x0000000000f833b9 in handler::ha_rnd_init (this=0x7ffe7c997610, scan=true) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/handler.cc:3096
#5  0x00000000014e24d1 in init_read_record (info=0x7ffe7cf47d60, thd=0x7ffe7c000b70, table=0x7ffe7c996c50, qep_tab=0x7ffe7cf47d10, use_record_cache=1, 
    print_error=true, disable_rr_cache=false) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/records.cc:315

棧幀3 全表掃描初次定位棧幀

#0  page_cur_set_before_first (block=0x7fff4d02f4a0, cur=0x7ffe7c99bab0) at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/include/page0cur.ic:99
#1  0x0000000001c5187f in btr_cur_open_at_index_side_func (from_left=true, index=0x7ffe7c999c20, latch_mode=1, cursor=0x7ffe7c99baa8, level=0, 
    file=0x239d388 "/root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/include/btr0pcur.ic", line=562, mtr=0x7fffec0f3570)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/btr/btr0cur.cc:2422
#2  0x0000000001b6e9c9 in btr_pcur_open_at_index_side (from_left=true, index=0x7ffe7c999c20, latch_mode=1, pcur=0x7ffe7c99baa8, init_pcur=false, level=0, 
    mtr=0x7fffec0f3570) at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/include/btr0pcur.ic:562
#3  0x0000000001b79a35 in row_search_mvcc (buf=0x7ffe7c997b50 "\377", mode=PAGE_CUR_G, prebuilt=0x7ffe7c99b880, match_mode=0, direction=0)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/row/row0sel.cc:5213
#4  0x00000000019d5493 in ha_innobase::index_read (this=0x7ffe7c997610, buf=0x7ffe7c997b50 "\377", key_ptr=0x0, key_len=0, find_flag=HA_READ_AFTER_KEY)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/handler/ha_innodb.cc:9536
#5  0x00000000019d66ea in ha_innobase::index_first (this=0x7ffe7c997610, buf=0x7ffe7c997b50 "\377")
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/handler/ha_innodb.cc:9977
#6  0x00000000019d6934 in ha_innobase::rnd_next (this=0x7ffe7c997610, buf=0x7ffe7c997b50 "\377")
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/handler/ha_innodb.cc:10075
#7  0x0000000000f83725 in handler::ha_rnd_next (this=0x7ffe7c997610, buf=0x7ffe7c997b50 "\377")
    at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/handler.cc:3146
#8  0x00000000014e2b3d in rr_sequential (info=0x7ffe7cf47d60) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/records.cc:521

棧幀4 MySQL格式的轉(zhuǎn)換

#0  row_sel_field_store_in_mysql_format_func (dest=0x7ffe7c997b51 "", templ=0x7ffe7c9a27f8, index=0x7ffe7c999c20, field_no=0, data=0x7fff4daec0a1 "\200", len=4, 
    prebuilt=0x7ffe7c99b880, sec_field=18446744073709551615) at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/row/row0sel.cc:2888
#1  0x0000000001b754b9 in row_sel_store_mysql_field_func (mysql_rec=0x7ffe7c997b50 "\377", prebuilt=0x7ffe7c99b880, rec=0x7fff4daec0a1 "\200", index=0x7ffe7c999c20, 
    offsets=0x7fffec0f3a80, field_no=0, templ=0x7ffe7c9a27f8, sec_field_no=18446744073709551615)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/row/row0sel.cc:3255
#2  0x0000000001b75c85 in row_sel_store_mysql_rec (mysql_rec=0x7ffe7c997b50 "\377", prebuilt=0x7ffe7c99b880, rec=0x7fff4daec0a1 "\200", vrow=0x0, rec_clust=0, 
    index=0x7ffe7c999c20, offsets=0x7fffec0f3a80, clust_templ_for_sec=false) at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/row/row0sel.cc:3434
#3  0x0000000001b7bd61 in row_search_mvcc (buf=0x7ffe7c997b50 "\377", mode=PAGE_CUR_G, prebuilt=0x7ffe7c99b880, match_mode=0, direction=0)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/row/row0sel.cc:6123
#4  0x00000000019d5493 in ha_innobase::index_read (this=0x7ffe7c997610, buf=0x7ffe7c997b50 "\377", key_ptr=0x0, key_len=0, find_flag=HA_READ_AFTER_KEY)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/handler/ha_innodb.cc:9536
#5  0x00000000019d66ea in ha_innobase::index_first (this=0x7ffe7c997610, buf=0x7ffe7c997b50 "\377")
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/handler/ha_innodb.cc:9977
#6  0x00000000019d6934 in ha_innobase::rnd_next (this=0x7ffe7c997610, buf=0x7ffe7c997b50 "\377")
    at /root/mysqlall/percona-server-locks-detail-5.7.22/storage/innobase/handler/ha_innodb.cc:10075
#7  0x0000000000f83725 in handler::ha_rnd_next (this=0x7ffe7c997610, buf=0x7ffe7c997b50 "\377")
    at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/handler.cc:3146
#8  0x00000000014e2b3d in rr_sequential (info=0x7ffe7cf47d60) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/records.cc:521
#9  0x0000000001584264 in join_init_read_record (tab=0x7ffe7cf47d10) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/sql_executor.cc:2487
#10 0x0000000001581349 in sub_select (join=0x7ffe7cf47660, qep_tab=0x7ffe7cf47d10, end_of_records=false)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/sql_executor.cc:1277
#11 0x0000000001580cce in do_select (join=0x7ffe7cf47660) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/sql_executor.cc:950

棧幀5 String的等值比較

#0  Arg_comparator::compare_string (this=0x7ffe7c0072f0) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/item_cmpfunc.cc:1669
#1  0x0000000000fde1e4 in Arg_comparator::compare (this=0x7ffe7c0072f0) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/item_cmpfunc.h:92
#2  0x0000000000fcb0a1 in Item_func_eq::val_int (this=0x7ffe7c007218) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/item_cmpfunc.cc:2507
#3  0x0000000001581af9 in evaluate_join_record (join=0x7ffe7c0077d8, qep_tab=0x7ffe7cb1dc70)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/sql_executor.cc:1492
#4  0x000000000158145a in sub_select (join=0x7ffe7c0077d8, qep_tab=0x7ffe7cb1dc70, end_of_records=false)
    at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/sql_executor.cc:1297
#5  0x0000000001580cce in do_select (join=0x7ffe7c0077d8) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/sql_executor.cc:950
#6  0x000000000157eb8a in JOIN::exec (this=0x7ffe7c0077d8) at /root/mysqlall/percona-server-locks-detail-5.7.22/sql/sql_executor.cc:199

關于“MySQL中查詢字段數(shù)量多少會對查詢效率有影響”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

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

AI