其他
数据库诊断不了的,腾讯大神来“诊断”
| 作者 王文安,腾讯CSIG数据库专项的数据库工程师,主要负责腾讯云数据库 MySQL 的相关的工作,热爱技术,欢迎留言进行交流。
CREATE TABLE `stu` (
`id` int(11) NOT NULL,
`name` varchar(16) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_name` (`name`),
KEY `idx_age` (`age`),
KEY `idx_n_a` (`name`,`age`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
INSERT INTO `stu` VALUES (9,'adam',25),(7,'carlos',25),(1,'dave',19),(5,'sam',22),(3,'tom',22),(11,'zoe',29);
mysql> select sleep(3600) from stu;
mysql> analyze table stu;
+----------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+----------+---------+----------+----------+
| test.stu | analyze | status | OK |
+----------+---------+----------+----------+
1 row in set (0.00 sec)
mysql> select * from stu limit 1;
mysql> show processlist;
+-----+------+-----------------+--------------------+---------+------+-------------------------+-----------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+------+-----------------+--------------------+---------+------+-------------------------+-----------------------------+
| 457 | root | 127.0.0.1:48650 | sbtest | Sleep | 4860 | | NULL |
| 458 | root | 127.0.0.1:48652 | sbtest | Sleep | 4851 | | NULL |
| 473 | root | 127.0.0.1:49512 | performance_schema | Sleep | 4834 | | NULL |
| 477 | root | 127.0.0.1:52364 | test | Query | 26 | User sleep | select sleep(3600) from stu |
| 478 | root | 127.0.0.1:53124 | test | Query | 10 | Waiting for table flush | select * from stu limit 1 |
| 479 | root | 127.0.0.1:53944 | sbtest | Query | 0 | starting | show processlist |
| 480 | root | 127.0.0.1:53946 | sbtest | Sleep | 958 | | NULL |
+-----+------+-----------------+--------------------+---------+------+-------------------------+-----------------------------+
7 rows in set (0.00 sec)
mysql>
futex_abstimed_wait_cancelable,
__pthread_cond_wait_common,
__pthread_cond_timedwait,
MDL_wait::timed_wait,
TABLE_SHARE::wait_for_old_version,
open_table,
open_tables,
open_tables_for_query,
::??,
mysql_execute_command,
mysql_parse,
dispatch_command,
do_command,
handle_connection,
pfs_spawn_thread,
start_thread,clone
open_tables()
{
...
if (!(flags & MYSQL_OPEN_IGNORE_FLUSH))
{
if (share->has_old_version()) // 如果存在 old_version
{
release_table_share(share);
mysql_mutex_unlock(&LOCK_open);
MDL_deadlock_handler mdl_deadlock_handler(ot_ctx);
bool wait_result;
...
wait_result= tdc_wait_for_old_version(thd, table_list->db,
table_list->table_name,
ot_ctx->get_timeout(),
deadlock_weight);
thd->pop_internal_handler();
...
if (thd->open_tables && thd->open_tables->s->version != share->version)
//如果存在不同的version,那么需要释放掉所有该表的cache,然后reopen
{
release_table_share(share);
mysql_mutex_unlock(&LOCK_open);
(void)ot_ctx->request_backoff_action(Open_table_context::OT_REOPEN_TABLES,
NULL);
DBUG_RETURN(TRUE);
}
}
......
tdc_wait_for_old_version(THD *thd, const char *db, const char *table_name,
ulong wait_timeout, uint deadlock_weight)
{
TABLE_SHARE *share;
bool res= FALSE;
mysql_mutex_lock(&LOCK_open);
if ((share= get_cached_table_share(thd, db, table_name)) &&
share->has_old_version())
//在这里获取表并进行表的version判断,如果old_version一直存在的话,进入if代码
{
struct timespec abstime;
set_timespec(&abstime, wait_timeout);
res= share->wait_for_old_version(thd, &abstime, deadlock_weight);
}
mysql_mutex_unlock(&LOCK_open);
return res;
}
关于专栏
《腾讯云数据库专家服务》是由腾讯云数据库技术服务团队维护的社区专栏,涵盖了各类数据库的实际案例,最佳实践,版本特性等内容。目前专栏文章仍在持续丰富中,欢迎在文章末尾留言互动,给出宝贵的建议。
- End -
更多精彩
这个关于连接池的结论,你绝对想不到
98%的DBA不知道的数据库内存知识点