Improve the table_cache of MySQL. There is a table_cache parameter in mysql. When the value is too large, the efficiency will decrease significantly. This is because open_cache hash is scanned.
Improve the table_cache of MySQL. There is a table_cache parameter in mysql. When the value is too large, the efficiency will decrease significantly. This is because open_cache hash is scanned.
In mysql, there is a table_cache parameter. When the value is too large, the efficiency will be significantly reduced. This is because the linear scan is used when the open_cache hash table is scanned. the time complexity is O (n). Someone on the mysql bug list provides a patch (? Id = 33948) To reduce the time to o (1). The basic idea is to add three pointers to the table instance to maintain an idle linked list.
First, let's analyze how mysql works when opening a table:
Table_cache is an important parameter in mysql. Because of the multi-thread mechanism, each thread opens the object descriptor that it needs, rather than sharing the opened object.
1. table_cache key (see create_table_def_key)
In the memory, table cache uses hash tables for storage. The key is database_name \ 0table_name \ 0 + (optional, used for temporary tables)
The temporary table is specially processed here, and additional information needs to be added to ensure that the temporary table is unique on the slave end.
Add 8 Bytes: the first 4 bytes are the master thread id, and the last 4 bytes are the slavb.
2. Processing When opening a table: open_table
***************
Necessary check: whether the thread stack is sufficient and whether thd is killed
**************
Global lock: lock_open
*************************
First, determine whether it is a temporary table.
*************************
Here is a very interesting logic. When you need to open a table, you always need to first look for the table from the temporary table chain table. That is to say, when a temporary table with the same name as the actual table exists, the temporary table is always operated.
If (! Table_list-> skip_temporary)
{
For (table = thd-> temporary_tables; table = table-> next)
{
**************************************** ******
Non-temporary table in pre-locked or lock_tables mode (thd-> locked_tables | thd-> prelocked_mode)
That is, the thread has opened or locked some tables and queried from thd-> open_tables. If no table exists, an error is returned.
**************************************** ******
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-> next)
{
**************************************** ***************
Normal situation:
1. First try to get the table from the table cache
2. When the TABLE instance is found to be nam-locked, or some threads are flush tables, we need to wait until the lock is released.
3. If such a TABLE does not exist, we need to create a TABLE and add it to the cache.
! Global locks are required for these operations: LOCK_open to protect table cache and table definitions on disks.
**************************************** ***************
If this is the first table opened by the query: Set thd-> version = refresh_version. In this way, when we open the remaining table, if the version changes, we need to back off, close all opened tables and reopen them.
Currently, refresh_version will only be changed by the flush tables command.
If (thd-> handler_tables)
Mysql_ha_flush (thd); // refresh (close and mark as re-open) All tables that require reopen
The process of querying table cache:
For (table = (TABLE *) hash_first (& open_cache, (uchar *) key, key_length, // search for the hash table based on the same key
& State );
Table & table-> in_use;
Table = (TABLE *) hash_next (& open_cache, (uchar *) key, key_length,
& State ))
{
**********************************
Flush tables marked for flush.