7.4.6.3 by default, the index Cache Management System of MySQL4.1 adopts the LRU policy to select the cache block to be cleared. However, it also supports a more comprehensive method called the midpoint insertion policy. When the midpoint insertion policy is used, the LRU chain is split into two halves: one hot sub-chain and one warm sub-chain. The two-half Split points are not fixed, but Cache Management
7.4.6.3 by default, the index Cache Management System of MySQL 4.1 uses the LRU policy to select the cache block to be cleared. However, it also supports a more comprehensive method called the midpoint insertion policy. When the midpoint insertion policy is used, the LRU chain is split into two halves: one hot sub-chain and one warm sub-chain. The two-half Split points are not fixed, but Cache Management
7.4.6.3 midpoint insertion Policy
By default, the index Cache Management System of MySQL 4.1 uses the LRU policy to select the cache block to be cleared. However, it also supports a more comprehensive method called "midpoint insertion policy ".
When the midpoint insertion policy is used, the LRU chain is split into two halves: one hot sub-chain and one warm sub-chain. The two-half splitting points are not fixed, but the Cache Management System will pay attention not to let the warm sub-chain part be "Too short" and always include at least all the cache blocks.key_cache_pision_limitRatio.key_cache_pision_limitIs the component part of the cache struct variable. Therefore, this parameter value can be set for each cache.
When an index block is read into the cache from a table, it is first placed at the end of the Wen sub-chain. After a certain click rate (access to this block) is reached, it will be upgraded to the hot sub-chain. Currently, to increase the click-through rate of a block (3) is the same for each block. In the future, we will allow the click rate to depend on the level of the index block node corresponding to the B tree: The click rate to be improved as required by the index block containing non-leaf nodes will be a little lower, the value of the block that contains the B Index Tree of the leaf node is high.
The upgraded block is placed at the end of the hot sub-chain. This block is continuously circulating in the hot sub-chain. If the block remains at the beginning of the sub-chain for a long enough time, it will be downgraded to a warm-up sub-chain. This time is the component of the index cache struct variable.key_cache_age_thresholdValue.
This threshold value is described as follows. An index cache containsNBlocks. The blocks starting with the hot sub-chain are smallerN*key_cache_age_threshold/100After the second access, it will be moved to the starting position of the wensub-chain. It first becomes a candidate object to be deleted, because the block to be replaced still starts from the beginning of the wensub-chain.
The dot-Point Insertion policy can always maintain more valuable blocks in the cache. If you prefer to adopt the LRU policy, you only needkey_cache_pision_limitThe default value is 100.
The dot-Point Insertion policy can help improve the performance when performing queries that require effective index scanning, which will correspond to all valuable nodes in the B tree. To avoid this, you must setkey_cache_pision_limitIt is much less than 100 to adopt the dot insertion policy. When the index scanning operation is performed, the valuable frequently-clicked nodes will be retained in the hot sub-chain.
7.4.6.4 pre-load indexes
If the index cache contains enough blocks to store all indexes, or at least enough to store all non-leaf nodes, it makes sense to load the index cache before use. Pre-load the index block into the index cache buffer in a very effective way: Read the index block sequentially from the disk.
There is no pre-load, and the index block required for the query still needs to be put into the cache. Although index blocks must be kept in the cache, they can be randomly read from the disk, rather than sequentially, because there is enough buffer.
To pre-load the cache, you can useLOAD INDEX INTO CACHEStatement. The following statement preloads the table:t1Andt2Index node (Block ):
mysql> LOAD INDEX INTO CACHE t1, t2 IGNORE LEAVES;
+---------+--------------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+---------+--------------+----------+----------+
| test.t1 | preload_keys | status | OK |
| test.t2 | preload_keys | status | OK |
+---------+--------------+----------+----------+
Add ModifierIGNORE LEAVESOnly the index blocks of non-leaf nodes are pre-loaded. Therefore, the preceding statement Loadst1But only loadt2Non-leaf node block.
If you useCACHE INDEXThe statement points the index to an index cache and pre-stores the index block in that cache. Otherwise, the index block will only be loaded into the default cache.
7.4.6.5 index cache size
MySQL 4.1 introduces new variables cached for each indexkey_cache_block_size. This variable can specify the block size of each index cache. You can use it to adjust the performance of index file I/O operations.
When the size of the read buffer is the same as that of the local operating system I/O buffer, the maximum I/O operation performance is achieved. However, setting the size of the index node is the same as that of the I/O buffer, which may not necessarily achieve the best overall performance. When reading large leaf nodes, the server will read a lot of unnecessary data, which greatly hinders reading other leaf nodes.
Currently, the index block size of a data table cannot be controlled. This size creates an index file on the server'. Myi'It has been set according to the definition of the index size of the data table. In many cases, it is set to the same size as the I/O buffer. In the future, you can change its value and use all variables.key_cache_block_size.
7.4.6.6 rebuilding the index Cache
The index cache can be rebuilt by modifying its parameter value at any time, for example:
mysql> SET GLOBAL cold_cache.key_buffer_size=4*1024*1024;
If you set the index cache struct variable component variablekey_buffer_sizeOrkey_cache_block_sizeIf any value is different from its current value, the server will clear the original cache and recreate the cache based on the new variable value. If the cache contains any 'dirty' index blocks, the server will save them before recreating the cache. Resetting other index cache variables does not recreate the cache.
When the cache is rebuilt, the server will refresh all the 'dir' buffered content to the disk. The cached content is invalid. However, rebuilding does not block queries that need to use indexes directed to the cache. On the contrary, the server uses the local file system cache to directly access data table indexes. The file system cache is not as efficient as the index cache. Therefore, it is foreseeable that queries will be slow at this time. Once the cache is re-built, the index pointing to it can be used again, and the file system cache will no longer be used to access the index.
7.4.7 how does MySQL count the number of opened tables?
Execute Commandmysqladmin statusYou can see the following results:
Uptime: 426 Running threads: 1 Questions: 11082
Reloads: 1 Open tables: 12
If there are only 6 data tables,Open tablesThe value is indeed 12, which may be a bit confusing.
MySQL is multi-threaded, So multiple clients may initiate a request to query a table at the same time. To minimize the different states of multiple client threads on the same table, open data tables for each concurrent thread separately. This will occupy some memory, but it usually improves performance. If yesMyISAMFor each client that opens a data table, an additional file descriptor is required to open the data file (however, the index file descriptor can be shared among all threads ).ISAMThe storage engine shares these operations.
For more information, see "7.4.8 How MySQL Opens and Closes Tables" in the next chapter ".
7.4.8 how to enable and disable a data table in MySQL
System Variablestable_cache,max_connectionsAndmax_tmp_tablesThis affects the maximum number of files that can be kept open on the server. Increase one or more of these variables to increase the file descriptor limit that the operating system can open at each processing. Many operating systems can increase the limit on the number of opened files, but each system has different methods. Check the operating system documentation to determine whether the limits can be improved and how to proceed.
table_cacheAndmax_connectionsRelated. For example, if there are 200 concurrent connections, there must be at least200 * NLarge table cache,NIs the maximum number of tables in a table connection. You also need to retain some additional file descriptors for temporary tables and temporary files.
Make sure that the operating system can be settable_cacheTo process the number of opened files. Iftable_cacheIf it is set too high, MySQL may use up all file descriptors and reject new connections, so it will not be able to execute the query and become unreliable. Therefore, you must considerMyISAMThe storage engine uses two file descriptors for each independently opened table. You canmysqld_safeWhen adding--open-files-limitParameter to increase the number of file descriptors opened by MySQL. For details, see "A.2.17 File Not Found ".
Saved in cachetable_cacheOpen tables. The default value is 64.mysqldBy modifying--table_cacheParameters. Note: MySQL may temporarily open more tables than this number for query.
In the following cases, an unused table is disabled and deleted from the cache:
- If the cache is full and a thread tries to open a table that is not in the cache.
- If the cache already contains more
table_cacheAnd the thread no longer needs to use the table.
- When the table is refreshed. When you submit
FLUSH TABLESStatement or executionmysqladmin flush-tablesOrmysqladmin refreshCommand.
When the table cache is full, the server follows these steps to allocate a new cache table:
- Tables that are not currently in use are released, in the order of least recently used.
- If a new table is to be opened but the cache is full and no table is released, the cache will be expanded as needed.
When the cache is temporarily expanded and a table changes from being used to not used, the table is closed and released from the cache.
Each concurrent access opens a table. This means that when two threads access a table at the same time, or a thread accesses the table twice in the same query (for example, connecting itself in a table connection ), this table needs to be opened twice. Each concurrent open request requests a table in the table cache. When a table is opened for the first time, two file descriptors are required: one for the data file and the other for the index file. To open a new table, you only need one file for the data file. The descriptor of the index file is shared among all threads.
If you useHANDLER tbl_name OPENWhen the statement opens the table, a dedicated table object is assigned to the thread. This table object is not shared with other threads unless you callHANDLER tbl_name CLOSEStatement or thread ends, otherwise it will not be closed; in this case, it will be put back into the table index (if the index is not full ). For details, see "14.1.3HANDLERSyntax ".
Check
mysqldStatus variableOpened_tablesTo determine whether the table index is too small:
mysql> SHOW STATUS LIKE 'Opened_tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Opened_tables | 2741 |
+---------------+-------+
If the value is large, but there is no need to execute a lotFLUSH
TABLESStatement. You only need to increase the table index cache size. For more information, see "5.2.3 Server System Variables" and "5.2.4 Server Status Variables ".
7.4.9 disadvantages of creating too many tables in some databases
If there are manyMyISAMOrISAMTable, open, close, and create operations will slow down. If you execute
SELECTStatement. When the table cache is full, this will incur some overhead, because each table needs to be opened and others must be closed. You can increase the table cache to reduce this overhead.
Optimize MySQL (7) optimization to MySQL (9 )›