改进MySQL的table_cache_MySQL
bitsCN.com
以下为本人在工作中的碎碎念,记录的比较凌乱……
........................................................................
在mysql里面有一个参数table_cache,当设置过大时,会产生明显的效率下降。这是因为扫描open_cache哈希表时,使用的线性扫描,时间复杂度为O(n),mysql的bug list上有人提供了一个patch(http://bugs.mysql.com/bug.php?id=33948),可以把时间降到o(1),其基本思想是为table实例增加三个指针,来维护一个空闲链表。
首先,我们分析一下mysql在打开一个表时如何工作:
在mysql里,table_cache是一个比较重要的参数。由于多线程机制,每个线程独自打开自己需要的标的文件描述符,而不是共享已经打开的。
1. table_cache key (见create_table_def_key)
在内存里,table cache使用hash表来存储,key为 database_name/0table_name/0+(可选的,用于临时表)
这里对于临时表会做特殊处理,需要增加额外的信息来保证临时表在slave端是唯一的
增加8个字节:前4个字节为master thread id,后4个字节为slavb
2.打开表时候的处理:open_table
***************
必要的检查:线程栈是否足够,thd是否被kill
**************
全局锁:lock_open
*************************
首先判断是否是临时表
*************************
这里有一段很有意思的逻辑,当需要打开表时,总是先从临时表链表中查找表。也就是说,当存在一个与实际表同名的临时表时,会总是操作临时表
if (!table_list->skip_temporary)
{
for (table= thd->temporary_tables; table ; table=table->next)
{
**********************************************
非临时表,且处于pre-locked 或lock_tables mode(thd->locked_tables || thd->prelocked_mode)
即该线程已经打开或锁定了一些表,从thd->open_tables里查询,当不存在时,返回error
**********************************************
if (thd->locked_tables || thd->prelocked_mode)
{ // Using table locks
TABLE *best_table= 0;
int best_distance= INT_MIN;
for (table=thd->open_tables; table ; table=table->next)
{
*******************************************************
正常情况:
1. 首先尝试从table cache中取table
2. 当找到的TABLE实例是nam-locked的,或者一些线程正在flush tables,我们需要等待,直到锁释放
3. 如果不存在这样的TABLE,我们需要创建TABLE,并将其加入到cache中
!这些操作都需要全局锁:LOCK_open,来保护table cache和磁盘上的表定义
*******************************************************
如果这是该query打开的第一个表:设置thd->version = refresh_version,这样,当我们打开剩余表的过程中,如果version发生了变化,则需要back off,关闭所有已经打开的并重新打开表
目前refresh_version只会被FLUSH TABLES命令改变
if (thd->handler_tables)
mysql_ha_flush(thd); //刷新(关闭并标记为re-open)所有需要reopen的表
查询table cache的过程:
for (table= (TABLE*) hash_first(&open_cache, (uchar*) key, key_length, //基于同一个key来查找hash表
&state);
table && table->in_use ;
table= (TABLE*) hash_next(&open_cache, (uchar*) key, key_length,
&state))
{
**********************************
flush tables marked for flush.
Normally, table->s->version contains the value of
refresh_version from the moment when this table was
(re-)opened and added to the cache.
If since then we did (or just started) FLUSH TABLES
statement, refresh_version has been increased.
For "name-locked" TABLE instances, table->s->version is set
to 0 (see lock_table_name for details).
In case there is a pending FLUSH TABLES or a name lock, we
need to back off and re-start opening tables.
If we do not back off now, we may dead lock in case of lock
order mismatch with some other thread:
c1: name lock t1; -- sort of exclusive lock
c2: open t2; -- sort of shared lock
c1: name lock t2; -- blocks
c2: open t1; -- blocks
*********************************
if (table->needs_reopen_or_name_lock()) //Is this instance of the table should be reopen or represents a name-lock?
{}
}
if (table)
************
从unused_tables链表中移除刚找到的table
************
else
***********
创建一个新的table实例,并插入到open cache中
***********
while (open_cache.records > table_cache_size && unused_tables) //当cache满时,从中释放未使用的TABLE实例
hash_delete(&open_cache,(uchar*) unused_tables);
if (table_list->create) //创建一个新表
{
*******
检查表是否存在:check_if_table_exists
*******
在table cache的hash中创建一个placeholder(占位符):table_cache_insert_placeholder
将占位符链到open tables list上:
table->open_placeholder= 1;
table->next= thd->open_tables;
thd->open_tables= table;
return table
}
创建一个新的table实例
分配内存table=(TABLE*) my_malloc(sizeof(*table),MYF(MY_WME))
error= open_unireg_entry(thd, table, table_list, alias, key, key_length,
mem_root, (flags & OPEN_VIEW_NO_PARSE));
如果是视图or error
my_hash_insert(&open_cache,(uchar*) table)
------------------------------------------------
patch:http://bugs.mysql.com/bug.php?id=33948
增加3个指针:
hash_head:
hash_prev: always point to unused table cached items
hash_next: always point to used table cached items
修改的函数:
free_cache_entry //释放一个表的内存。
close_thread_table //move one table to free list
reopen_name_locked_table //重新打开表,保持链表结构
table_cache_insert_placeholder
open_table
------------------------------------------------------------------------
总结:
增加了三个指针:
hash_head:
hash_prev:
hash_next:
!.............................!head!.........................!
head的左边为空闲item链表
head的右边为占用的item链表
所有item通过hash_prev和hash_next进行双向指针
右边的item的hash_head指向head
操作链表:
1)插入新空闲item:在head节点前加入
2)插入新的被占用item:在head后面加入
3)从链表中删除item:
---若该item为head,修改head右侧的item的hash_head指向head->next
---否则,直接删除item,并释放内存。。
查询空闲节点:
1) 找到head
2) 检测head是否in_use,为False则table = head, true则找到table = head->prev
3)当table 不为NULL时,表示找到一个item,将其插入到head右侧
3) table依旧为NULL---->创建新item,将其插入到head右侧
------------------------------
转载请注明:印风
bitsCN.com
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Analysis of front-end engineer responsibilities: What is the main job? With the rapid development of the Internet, front-end engineers play a very important professional role, playing a vital role as a bridge between users and website applications. So, what do front-end engineers mainly do? This article will analyze the responsibilities of front-end engineers, let us find out. 1. Basic responsibilities of front-end engineers Website development and maintenance: Front-end engineers are responsible for the front-end development of the website, including writing the website’s HTML, CSS and JavaScr

When we talk about programming languages and jobs, one programming language that comes to our mind is Java. Most companies around the world use Java. It's popular and there are many job opportunities. If you want to get a job with the help of Java skills in 2023, then this is good for you as Java skills can get you a job quickly. Plus, it can quickly advance your career. There is no magic trick that will make you find a job quickly. But your skills are like magic to you. Choose a job that satisfies you and a good environment that can greatly enhance your career. If you are a newbie and have experience, Java also provides you with a good job. Many companies use Java as the main program for their development. it

Jobs you can find by learning Java: 1. Enterprise-level application development; 2. Website development; 3. Android development; 4. Embedded field; 5. Big data and cloud computing; 6. Game development; 7. Scientific applications; 8. Software development and maintenance; 9. System and network programming; 10. Security and encryption; 11. Education and training; 12. Consulting and consulting. Detailed introduction: 1. Enterprise-level application development. Java occupies a very important position in enterprise-level application development. Using Java, various complex enterprise-level applications can be developed, such as OA systems and so on.

Possible jobs: 1. Enterprise-level application development; 2. Website development; 3. Mobile application development; 4. Game development; 5. Big data analysis; 6. Scientific computing and artificial intelligence; 7. Embedded system development; 8 , financial industry application development, etc. Detailed introduction: 1. Enterprise-level application development: Java occupies an important position in enterprise-level application development and can be used to develop large and complex systems, such as ERP, CRM, etc. These systems usually need to process large amounts of data, support high concurrency, and ensure system stability; 2. Website development: including front-end and back-end, etc.

The successive release of large models such as ChatGPT has made many people feel stressed and afraid that AI will soon take over their jobs. In this regard, OpenAI has also published a study showing that the impact of ChatGPT covers all income classes, and high-income jobs may face greater risks. What are the facts? Should we automate all jobs, even the satisfying ones? It's one of several questions raised recently by the Future of Life Institute, which has called for a moratorium on large-scale artificial intelligence experiments and has now been signed by more than 10,000 people, including Elon Musk, Steve Wozniak and Andrew Yang.

The jobs that C language can do in the future: 1. System development; 2. Game development; 3. Network development; 4. Application development; 5. Compiler development; 6. Algorithm engineer; 7. Network security; 8. Hardware development; 9 , Education field; 10. Data analysis and machine learning; 11. Software development and maintenance; 12. Software testing. Detailed introduction: 1. System development. C language is a common language for system-level programming. It can be used to develop operating systems and embedded systems. If you master C language, you can become a system development engineer; 2. Game development, etc.

Caching helps reduce database network calls when executing queries. Level 1 cache and session linking. It is implemented implicitly. The first level cache exists until the session object exists. Once the session object is terminated/closed, there will be no cached objects. Second level cache works for multiple session objects. It is linked with the session factory. Second level cache objects are available to all sessions using a single session factory. These cache objects will be terminated when a specific session factory is closed. To implement the second level cache we need to add the following dependencies to use the second level cache. <!--https://mvnrepository.com/artifact/net.sf.ehcache/ehcache--><de

How to use MySQL database skills to find your dream job? Abstract: In today's information age, skills such as database management and data analysis are increasingly valued. MySQL database is one of the most commonly used relational databases. Mastering MySQL database skills provides a great opportunity to find your ideal job. This article will explain how to use MySQL database skills to find your dream job, and provide some code examples to demonstrate the application of MySQL database. Introduction: MySQL is an open source relational database management system.
