需要打断点的函数如下
get_table_share、open_table_from_share、Table_cache::add_used_table
[root@ecs-test-node2 mysql-8.0.42]# gdb -p $(pgrep -x mysqld)
..
(gdb) break get_table_share
Breakpoint 1 at 0x34fb18b: get_table_share. (3 locations)
(gdb) break open_table_from_share
Breakpoint 2 at 0x37b5f77: file /data/mysql-8.0.42/sql/table.cc, line 2876.
(gdb) break Table_cache::add_used_table
Breakpoint 3 at 0x35166d7: file /data/mysql-8.0.42/sql/table_cache.h, line 348.
在终端中开启一个mysql客户端会话,查询一张表的记录
lily [(none)]> use summer
Database changed
lily [summer]> select * from emp where id = 1;
大致看一下函数的调用过程
(gdb) c
Continuing.
[Switching to Thread 0x7fac54166700 (LWP 1463531)]
Thread 36 "connection" hit Breakpoint 1, get_table_share (thd=0x7fabfc008050, db=0x7fabfc021e00 "summer",
table_name=0x7fabfc020a40 "emp", key=0x7fabfc021ba7 "summer", key_length=11, open_view=true,
open_secondary=false) at /data/mysql-8.0.42/sql/sql_base.cc:686
686 bool open_table_err = false;
(gdb) backtrace
#0 get_table_share (thd=0x7fabfc008050, db=0x7fabfc021e00 "summer", table_name=0x7fabfc020a40 "emp",
key=0x7fabfc021ba7 "summer", key_length=11, open_view=true, open_secondary=false)
at /data/mysql-8.0.42/sql/sql_base.cc:686
#1 0x00000000034fbe02 in get_table_share_with_discover (thd=0x7fabfc008050, table_list=0x7fabfc0217b0,
key=0x7fabfc021ba7 "summer", key_length=11, open_secondary=false, error=0x7fac54162c9c)
at /data/mysql-8.0.42/sql/sql_base.cc:903
#2 0x00000000035014e5 in open_table (thd=0x7fabfc008050, table_list=0x7fabfc0217b0, ot_ctx=0x7fac54162f70)
at /data/mysql-8.0.42/sql/sql_base.cc:3221
#3 0x000000000350573c in open_and_process_table (thd=0x7fabfc008050, lex=0x7fabfc21ee60, tables=0x7fabfc0217b0,
counter=0x7fabfc21eeb8, prelocking_strategy=0x7fac54163098, has_prelocking_list=false, ot_ctx=0x7fac54162f70)
at /data/mysql-8.0.42/sql/sql_base.cc:5053
#4 0x00000000035070b0 in open_tables (thd=0x7fabfc008050, start=0x7fac54163040, counter=0x7fabfc21eeb8, flags=0,
prelocking_strategy=0x7fac54163098) at /data/mysql-8.0.42/sql/sql_base.cc:5858
#5 0x0000000003508b26 in open_tables_for_query (thd=0x7fabfc008050, tables=0x7fabfc0217b0, flags=0)
at /data/mysql-8.0.42/sql/sql_base.cc:6747
#6 0x00000000036aab43 in Sql_cmd_dml::prepare (this=0x7fabfc107168, thd=0x7fabfc008050)
at /data/mysql-8.0.42/sql/sql_select.cc:541
#7 0x00000000036ab580 in Sql_cmd_dml::execute (this=0x7fabfc107168, thd=0x7fabfc008050)
at /data/mysql-8.0.42/sql/sql_select.cc:710
#8 0x00000000036266d2 in mysql_execute_command (thd=0x7fabfc008050, first_level=true)
at /data/mysql-8.0.42/sql/sql_parse.cc:4724
#9 0x0000000003628aae in dispatch_sql_command (thd=0x7fabfc008050, parser_state=0x7fac54164970)
at /data/mysql-8.0.42/sql/sql_parse.cc:5385
#10 0x000000000361e683 in dispatch_command (thd=0x7fabfc008050, com_data=0x7fac54165a60, command=COM_QUERY)
at /data/mysql-8.0.42/sql/sql_parse.cc:2055
#11 0x000000000361c5ca in do_command (thd=0x7fabfc008050) at /data/mysql-8.0.42/sql/sql_parse.cc:1440
#12 0x0000000003844598 in handle_connection (arg=0xcbc83d0)
at /data/mysql-8.0.42/sql/conn_handler/connection_handler_per_thread.cc:303
#13 0x00000000058bf53d in pfs_spawn_thread (arg=0xce22c50) at /data/mysql-8.0.42/storage/perfschema/pfs.cc:3050
#14 0x00007fac7cd991ca in start_thread (arg=<optimized out>) at pthread_create.c:479
#15 0x00007fac7b2b88d3 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
首先会建立连接
handle_connection(void * arg) (/data/mysql-8.0.42/sql/conn_handler/connection_handler_per_thread.cc:303)
之后会分发解析语句
mysql_execute_command(THD * thd, bool first_level) (/data/mysql-8.0.42/sql/sql_parse.cc:4724)
dispatch_sql_command(THD * thd, Parser_state * parser_state) (/data/mysql-8.0.42/sql/sql_parse.cc:5385)
之后会打开表
open_tables(THD * thd, Table_ref ** start, uint * counter, uint flags, Prelocking_strategy * prelocking_strategy) (/data/mysql-8.0.42/sql/sql_base.cc:5858)
打开表的主函数是open_tables
/**
Open all tables in list
*/
bool open_tables(THD *thd, Table_ref **start, uint *counter, uint flags,
Prelocking_strategy *prelocking_strategy)
在打开一张表的时候,MySQL会尝试从table definition cache这个缓存中去获取表的定义。如果TABLE_SHARE不存在,则会创建一份新的缓存。
对应的函数如下
/**
Get the TABLE_SHARE for a table.
*/
TABLE_SHARE *get_table_share(THD *thd, const char *db, const char *table_name,
const char *key, size_t key_length, bool open_view,
bool open_secondary)
在open_table函数中,会尝试获取TABLE_SHARE缓存。
if (!(share = get_table_share_with_discover(
thd, table_list, key, key_length,
flags & MYSQL_OPEN_SECONDARY_ENGINE, &error))) {
mysql_mutex_unlock(&LOCK_open);
..}
在上面的函数中,会最终调用get_table_share这个函数来获取缓存。
/**
Get a table share. If it didn't exist, try creating it from engine
For arguments and return values, see get_table_share()
*/
static TABLE_SHARE *get_table_share_with_discover(
THD *thd, Table_ref *table_list, const char *key, size_t key_length,
bool open_secondary, int *error)
{
TABLE_SHARE *share;
bool exists;
DBUG_TRACE;
share = get_table_share(thd, table_list->db, table_list->table_name, key,
key_length, true, open_secondary);
/*
If share is not NULL, we found an existing share.
*/
如果这缓存存在,会使用缓存中的TABLE_SHARE对象。
对应的函数如下
/**
Open a table based on a TABLE_SHARE
*/
int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias,
uint db_stat, uint prgflag, uint ha_open_flags,
TABLE *outparam, bool is_create_table,
const dd::Table *table_def_param)
如果是一张新表,则会把新的表对象添加到table cache中。
/* Add new TABLE object to table cache for this connection. */
Table_cache *tc = table_cache_manager.get_cache(thd);
tc->lock();
if (tc->add_used_table(thd, table)) {
tc->unlock();
goto err_lock;
}
tc->unlock();
MySQL 8.0 表打开与缓存机制
275

被折叠的 条评论
为什么被折叠?



