溫馨提示×

溫馨提示×

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

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

slave_rows_search_algorithms參數hash_scan的實現方法

發布時間:2020-08-18 04:14:02 來源:ITPUB博客 閱讀:636 作者:chenfeng 欄目:MySQL數據庫

slave_rows_search_algorithms由三個值的組合組成:TABLE_SCAN,INDEX_SCAN, HASH_SCAN。

TABLE_SCAN,INDEX_SCAN  (默認配置,表示如果有索引就用索引,否則使用全表掃描)


HASH_SCAN可以部分解決無主鍵表導致的復制延遲問題。


當表上無主鍵或唯一鍵時,那么對于在該表上做的DML,如果是以ROW模式復制,則每一個行記錄前鏡像在備庫都可能

產生一次全表掃描(或者二級索引掃描), 大多數情況下,這種開銷都是非常不可接受的,并且會產生大量的延遲。

hash_scan的實現方法

簡單的講,在apply rows_log_event時,會將 log_event 中對行的更新緩存在兩個結構中,分別

是:m_hash, m_distinct_key_list。 m_hash:主要用來緩存更新的行記錄的起始位置,

是一個hash表; m_distinct_key_list:如果有索引,則將索引的值push 到m_distinct_key_list,如果表沒有索引,

則不使用這個List結構; 其中預掃描整個調用過程如下: Log_event::apply_event


Rows_log_event::do_apply_event

   Rows_log_event::do_hash_scan_and_update 

     Rows_log_event::do_hash_row  (add entry info of changed records)

       if (m_key_index < MAX_KEY) (index used instead of table scan)

         Rows_log_event::add_key_to_distinct_keyset ()

當一個event 中包含多個行的更改時,會首先掃描所有的更改,將結果緩存到m_hash中,如果該表有索引,則將索引的值

緩存至m_distinct_key_list List 中,如果沒有,則不使用這個緩存結構,

而直接進行全表掃描。


執行stack如下:


#0 handler::ha_delete_row 

#1 0x0000000000a4192b in Delete_rows_log_event::do_exec_row 

#2 0x0000000000a3a9c8 in Rows_log_event::do_apply_row

#3 0x0000000000a3c1f4 in Rows_log_event::do_scan_and_update 

#4 0x0000000000a3c5ef in Rows_log_event::do_hash_scan_and_update 

#5 0x0000000000a3d7f7 in Rows_log_event::do_apply_event 

#6 0x0000000000a28e3a in Log_event::apply_event

#7 0x0000000000a8365f in apply_event_and_update_pos

#8 0x0000000000a84764 in exec_relay_log_event 

#9 0x0000000000a89e97 in handle_slave_sql

#10 0x0000000000e341c3 in pfs_spawn_thread

#11 0x0000003a00a07851 in start_thread () 

#12 0x0000003a006e767d in clone ()

執行過程說明:


Rows_log_event::do_scan_and_update


open_record_scan()

    do

      next_record_scan()

        if (m_key_index > MAX_KEY)

           ha_rnd_next();

        else

           ha_index_read_map(m_key from m_distinct_key_list)       

        entry= m_hash->get()

        m_hash->del(entry);

        do_apply_row()

    while (m_hash->size > 0);

從執行過程上可以看出,當使用hash_scan時,只會全表掃描一次,雖然會多次遍歷m_hash這個hash表,但是這個掃描

是O(1),所以,代價很小,因此可以降低掃描次數,提高執行效率。


向AI問一下細節

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

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女