????MySQL??????????????????????MySQL????????????????????????????в??????????????????????????????????????????????????MySQL?????Metadata Lock???????????hash_scan ?????????????????????TokuDB·?汾????????MariaDB·????????????????????????????????????
????????
????????MDL???????????????????bug#989????MySQL 5.1??????汾????????й????в???????漰???????б??Metatdata ???????????????ж????????????????У?
????Session 1: BEGIN;
????Session 1: INSERT INTO t1 VALUES (1);
????Session 2: Drop table t1; ——–SQLд??BINLOG
????Session 1: COMMIT; —–????д??BINLOG
???????????? binlog??????????DROP TABLE????INSERT????????????????ж??
??????MySQL 5.5?汾????????MDL?? ????????????漰???????б??MDL????????????????????????ζ?????????е?DROP TABLE ????????Session 1??????????????
?????????ù?5.5??????????MDL??????????????????????????????????????????mysqldump??????????????????????FLUSH TABLES WITH READ LOCK (??????FTWRL??д????)????????GLOBAL??MDL?????????????????“wait for global read lock”????????????????????????????????????????б????????DDL??????FTWRL??block??????????QUERY????block???????????????????????
????????????????Facebook?MySQL?????μ????滻??FTWRL ????????read view ??????????read view??μ?binlogλ??????Percona Server????????????????????FTWRL??????????????????percona?????????????????
????MDL?????bug#989?????????????μ???????е?MDL??????????????hash?????У??????????????????????????з????????????????????Facebook?????Mark Callaghan??report??bug#66473???????????Mark???MDL_map::mutex????????????????????????????????MySQL 5.6.8??????汾?У????????2???metadata_locks_hash_instances???????mdl hash???????(Rev:4350)??
?????????????????????????????????????????????somedb. someprefix1 … .somedb .someprefix8 ??hash key??????????hash??????????????????hash???????Ч????????hash???????????????????????????bug#66473????Dmitry Lenev???????
????Mark?????????????Innodb??hash????????my_hash_sort_bin?????Ч?? Oracle??????????????bug#68487???????????????MySQL5.6.15??hash key???????????????????fix ???????hash????????(Rev:5459)?????MurmurHash3????????mdl key??hash???
????MySQL 5.7 ??MDL???????
??????MySQL 5.7???MDL?????????????????????????????????????
??????????????MDL HASH????????????????????????+???????????key????з???????????????DML??????????????????hash?????????????????????MDL HASH???????????
??????????????????LOCK-FREE??HASH???洢MDL_lock??LF_HASH??????????????”Split-Ordered Lists: Lock-Free Extensible Hash Tables”????????????? ????????LF_HASH???類?????Performance Schema?????????????????顣??????????LF_HASH??MDL HASH???????????????????? ?????WL#7305?? PATCH(Rev:7249)
????????????????????????????DML/SELECT???DDL??????MDL???????????????????????????????DML??SELECT??????MDL??????
???????????DML/SELECT???????????????????LOCK-WORD??????????????FAST-PATH?????FAST-PATH????????????SLOW-PATH?????м?????
???????MDL??????MDL_lock????????????long long????????????????????????????????MDL_lock::m_fast_path_state ???????????????????sbtest1??????MDL_lock::m_fast_path_state??0??
????Session 1: BEGIN;
????Session 1: SELECT * FROM sbtest1 WHERE id =1; //m_fast_path_state = 1048576?? MDL ticket ????MDL_lock::m_granted????
????Session 2: BEGIN;
????Session 2: SELECT * FROM sbtest1 WHERE id =2; //m_fast_path_state=1048576+1048576=2097152????????FAST PATH
????Session 3: ALTER TABLE sbtest1 ENGINE = INNODB; //DDL??????MDL_SHARED_UPGRADABLE???????????unobtrusive lock???????????????????SQL??MDL?????????????????????????????????slow path????slow path???????MDL_lock::m_rwlock??д????m_fast_path_state = m_fast_path_state | MDL_lock::HAS_SLOW_PATH | MDL_lock::HAS_OBTRUSIVE
?????:DDL??????????????????MDL??????????????????????????????????????????????????????漰??????MDL_lock??????
????Session 4: SELECT * FROM sbtest1 WHERE id =3; // ???m_fast_path_state &HAS_OBTRUSIVE?????DDL???????????slow path??
????????????????????????MDL??????????????????????????OBTRUSIVE or UNOBTRUSIVE?????洢?????????m_unobtrusive_lock_increment?? ???????????????MDL???????????DML/SELECT??????????????????κζ?д????MUTEX?????????WL#7304?? WL#7306 ?? PATCH??Rev:7067??Rev:7129??(Rev:7586)
????????????????????MDL?????????????汾???????Server???????????????THR_LOCK ????Innodb?????????Щ??????????Innodb??????????????????????
?????????????е?????У?Innodb???????THR_LOCK?????LOCK TABLE tbname READ????????????μ?MDL?????????????????????????????????????????????μ?MDL?????Innodb??????м??д??????WL#6671??PATCH(Rev:8232)
?????????Server?????????????GET_LOCK????????????MDL??????????
??????????????GET_LOCK()??????????????????????????MDL?????????????MDL???????????????????????仯?????????????????????С??64??????????MDL?????????????????WL#1159?? PATCH(Rev:8356)
????MySQL·???????·hash_scan ??????????
????????????
?????????????????????TestCase??
????--source include/master-slave.inc
????--source include/have_binlog_format_row.inc
????connection slave;
????set global slave_rows_search_algorithms='TABLE_SCAN';
????connection master;
????create table t1(id int?? name varchar(20);
????insert into t1 values(1??'a');
????insert into t2 values(2?? 'b');
????......
????insert into t3 values(1000?? 'xxx');
????delete from t1;
????---source include/rpl_end.inc
???????? t1 ????????????rpl_hash_scan.test ????????????? t1 ???????????????????????????????? ‘delete from t1;’ ????t1???????????????????????t1?????????裬??? select count(*) from t1 = N?? ????????δ? t1 ??? ???????????? O(N + (N-1) + (N-2) + …. + 1) = O(N^2)???? replication ??????? hash_scan??binlog_format=row????????????????????? table_scan ???????????update_rows_log_event/delete_rows_log_event ???????????????????????????????????????? stack ???£?
????#0 Rows_log_event::do_table_scan_and_update
????#1 0x0000000000a3d7f7 in Rows_log_event::do_apply_event
????#2 0x0000000000a28e3a in Log_event::apply_event
????#3 0x0000000000a8365f in apply_event_and_update_pos
????#4 0x0000000000a84764 in exec_relay_log_event
????#5 0x0000000000a89e97 in handle_slave_sql (arg=0x1b3e030)
????#6 0x0000000000e341c3 in pfs_spawn_thread (arg=0x2b7f48004b20)
????#7 0x0000003a00a07851 in start_thread () from /lib64/libpthread.so.0
????#8 0x0000003a006e767d in clone () from /lib64/libc.so.6
????????????£????????????????????????????????????????????????
??????ν??????
????RDS ????????????????????????????????±???????????????????????а??????????????????????????????????????????У??????show create?? select * ??????????????????????????????????????????????;
?????????????????????5.6.6 ?????汾??????? slave_rows_search_algorithms ?????????????? apply_binlog_event?????????????????TABLE_SCAN??INDEX_SCAN??HASH_SCAN??????table_scan??index_scan???????????????????о?HASH_SCAN????????????????slave_rows_search_algorithms?????á?
????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)????????????С?????????????????????????Ч???
????hash_scan ????? bug
????bug???飺http://bugs.mysql.com/bug.php?id=72788
????bug???m_distinct_key_list ?е?index key ???????????????????????????????????????
????bug?????http://bazaar.launchpad.net/~mysql/mysql-server/5.7/revision/8494
?????????????
???????????????????£??????? hash_scan ???????Ч?????????????????????????θ??2??????????????????????????裬????????entry ????????????к?????????
???????event????????????????????????????????????????????С?й?????event ???С??????9000 bytes?? ??в?????????????size??
????hash_scan ????????????hash_scan ?????????????????Ч??????binlog_format=statement ?????? Query_log_event ????binlog_format=row ??????? Write_rows_log_event ?????????
????TokuDB·?汾???·7.5.0
????TokuDB??7.5.0??汾??????????????????汾????????????????????洢???氮???????
????a) shutdown????
?????????????TokuDB??shutdown????????С???????£??????????????shutdown?????TokuDB????????????checkpoint????????е??????????л?????????????
????????????????????tokudb_cache_size???????????е??????????shutdown?????????????????????????????е????
??????7.5.0?汾??TokuDB??????????????????????????????????????????
????BTW: TokuDB????????????????????н????????δ?????
????b) ??????????
??????????У?TokuDB????(internal node)?????message buffer???У?????????????
????1) FIFO????????{key?? value}
????2) OMT????????{key?? FIFO-offset}
????????FIFO?????????????????????OMT???????????(????key?鵽value)????????????????cache miss??????????????????
????1) ??????????????????
????2) ????FIFO??
????3) ????FIFO????OMT??(??????)
????????TokuDB????в????????(ji)??(shu)????????????3)?????С????????????????ζ????message buffer???????????OMT????????7.5.0?汾????OMT??FIFO-offset(??????)????????????????????????????
????c) ???д????
??????д??????????????????key??pivots?????(????)???д????????mesage buffer?????д?????(???????????????????·??)???????????????????????????????