Menu:
optimizing-memory:: Optimizing Memory Use
This section discusses optimization techniques for the database server, primarily dealing with system configuration rather than tuning SQL statements. The information in this section is appropriate for DBAs who want to ensure performance and scalability across the servers they manage; for developers constructing installation scripts that include setting up the database; and people running MySQL themselves for development, testing, and so on who want to maximize their own productivity.
File: manual.info.tmp, Node: system-optimization, Next: disk-issues, Prev: optimizing-server, Up: optimizing-server
Some system-level factors can affect performance in a major way:
If you have enough RAM, you could remove all swap devices. Some operating systems use a swap device in some contexts even if you have free memory.
Avoid external locking for *note 'MyISAM': myisam-storage-engine. tables. The default is for external locking to be disabled. The '--external-locking' and '--skip-external-locking' options explicitly enable and disable external locking.
Disabling external locking does not affect MySQL's functionality as long as you run only one server. Just remember to take down the server (or lock and flush the relevant tables) before you run *note 'myisamchk': myisamchk. On some systems it is mandatory to disable external locking because it does not work, anyway.
The only case in which you cannot disable external locking is when you run multiple MySQL servers (not clients) on the same data, or if you run *note 'myisamchk': myisamchk. to check (not repair) a table without telling the server to flush and lock the tables first. Note that using multiple MySQL servers to access the same data concurrently is generally not recommended, except when using NDB Cluster.
The note 'LOCK TABLES': lock-tables. and note 'UNLOCK TABLES': lock-tables. statements use internal locking, so you can use them even if external locking is disabled.
File: manual.info.tmp, Node: disk-issues, Next: symbolic-links, Prev: system-optimization, Up: optimizing-server
This section describes ways to configure storage devices when you can devote more and faster storage hardware to the database server. For information about optimizing an 'InnoDB' configuration to improve I/O performance, see *note optimizing-innodb-diskio::.
Disk seeks are a huge performance bottleneck. This problem becomes more apparent when the amount of data starts to grow so large that effective caching becomes impossible. For large databases where you access data more or less randomly, you can be sure that you need at least one disk seek to read and a couple of disk seeks to write things. To minimize this problem, use disks with low seek times.
Increase the number of available disk spindles (and thereby reduce the seek overhead) by either symlinking files to different disks or striping the disks:
* Using symbolic links
This means that, for 'MyISAM' tables, you symlink the index
file and data files from their usual location in the data
directory to another disk (that may also be striped). This
makes both the seek and read times better, assuming that the
disk is not used for other purposes as well. See *note
symbolic-links::.
Symbolic links are not supported for use with 'InnoDB' tables.
However, it is possible to place 'InnoDB' data and log files
on different physical disks. For more information, see *note
optimizing-innodb-diskio::.
*
Striping
Striping means that you have many disks and put the first
block on the first disk, the second block on the second disk,
and the N-th block on the ('N MOD NUMBER_OF_DISKS') disk, and
so on. This means if your normal data size is less than the
stripe size (or perfectly aligned), you get much better
performance. Striping is very dependent on the operating
system and the stripe size, so benchmark your application with
different stripe sizes. See *note custom-benchmarks::.
The speed difference for striping is _very_ dependent on the
parameters. Depending on how you set the striping parameters
and number of disks, you may get differences measured in
orders of magnitude. You have to choose to optimize for
random or sequential access.
For reliability, you may want to use RAID 0+1 (striping plus mirroring), but in this case, you need 2 x N drives to hold N drives of data. This is probably the best option if you have the money for it. However, you may also have to invest in some volume-management software to handle it efficiently.
A good option is to vary the RAID level according to how critical a type of data is. For example, store semi-important data that can be regenerated on a RAID 0 disk, but store really important data such as host information and logs on a RAID 0+1 or RAID N disk. RAID N can be a problem if you have many writes, due to the time required to update the parity bits.
You can also set the parameters for the file system that the database uses:
If you do not need to know when files were last accessed (which is not really useful on a database server), you can mount your file systems with the '-o noatime' option. That skips updates to the last access time in inodes on the file system, which avoids some disk seeks.
On many operating systems, you can set a file system to be updated asynchronously by mounting it with the '-o async' option. If your computer is reasonably stable, this should give you better performance without sacrificing too much reliability. (This flag is on by default on Linux.)
Using NFS with MySQL
You should be cautious when considering whether to use NFS with MySQL. Potential issues, which vary by operating system and NFS version, include the following:
MySQL data and log files placed on NFS volumes becoming locked and unavailable for use. Locking issues may occur in cases where multiple instances of MySQL access the same data directory or where MySQL is shut down improperly, due to a power outage, for example. NFS version 4 addresses underlying locking issues with the introduction of advisory and lease-based locking. However, sharing a data directory among MySQL instances is not recommended.
Data inconsistencies introduced due to messages received out of order or lost network traffic. To avoid this issue, use TCP with 'hard' and 'intr' mount options.
Maximum file size limitations. NFS Version 2 clients can only access the lowest 2GB of a file (signed 32 bit offset). NFS Version 3 clients support larger files (up to 64 bit offsets). The maximum supported file size also depends on the local file system of the NFS server.
Using NFS within a professional SAN environment or other storage system tends to offer greater reliability than using NFS outside of such an environment. However, NFS within a SAN environment may be slower than directly attached or bus-attached non-rotational storage.
If you choose to use NFS, NFS Version 4 or later is recommended, as is testing your NFS setup thoroughly before deploying into a production environment.
File: manual.info.tmp, Node: symbolic-links, Next: optimizing-memory, Prev: disk-issues, Up: optimizing-server
Menu:
windows-symbolic-links:: Using Symbolic Links for Databases on Windows
You can move databases or tables from the database directory to other locations and replace them with symbolic links to the new locations. You might want to do this, for example, to move a database to a file system with more free space or increase the speed of your system by spreading your tables to different disks.
For 'InnoDB' tables, use the 'DATA DIRECTORY' clause of the note 'CREATE TABLE': create-table. statement instead of symbolic links, as explained in note innodb-create-table-external::. This new feature is a supported, cross-platform technique.
The recommended way to do this is to symlink entire database directories to a different disk. Symlink 'MyISAM' tables only as a last resort.
To determine the location of your data directory, use this statement:
SHOW VARIABLES LIKE 'datadir';
File: manual.info.tmp, Node: symbolic-links-to-databases, Next: symbolic-links-to-tables, Prev: symbolic-links, Up: symbolic-links
8.12.3.1 Using Symbolic Links for Databases on Unix ...................................................
On Unix, the way to symlink a database is first to create a directory on some disk where you have free space and then to create a soft link to it from the MySQL data directory.
$> mkdir /dr1/databases/test
$> ln -s /dr1/databases/test /PATH/TO/DATADIR
MySQL does not support linking one directory to multiple databases. Replacing a database directory with a symbolic link works as long as you do not make a symbolic link between databases. Suppose that you have a database 'db1' under the MySQL data directory, and then make a symlink 'db2' that points to 'db1':
$> cd /PATH/TO/DATADIR
$> ln -s db1 db2
The result is that, for any table 'tbl_a' in 'db1', there also appears to be a table 'tbl_a' in 'db2'. If one client updates 'db1.tbl_a' and another client updates 'db2.tbl_a', problems are likely to occur.
File: manual.info.tmp, Node: symbolic-links-to-tables, Next: windows-symbolic-links, Prev: symbolic-links-to-databases, Up: symbolic-links
8.12.3.2 Using Symbolic Links for MyISAM Tables on Unix .......................................................
Symlinks are fully supported only for 'MyISAM' tables. For files used by tables for other storage engines, you may get strange problems if you try to use symbolic links. For 'InnoDB' tables, use the alternative technique explained in *note innodb-create-table-external:: instead.
Do not symlink tables on systems that do not have a fully operational 'realpath()' call. (Linux and Solaris support 'realpath()'). To determine whether your system supports symbolic links, check the value of the 'have_symlink' system variable using this statement:
SHOW VARIABLES LIKE 'have_symlink';
The handling of symbolic links for 'MyISAM' tables works as follows:
In the data directory, you always have the table format ('.frm') file, the data ('.MYD') file, and the index ('.MYI') file. The data file and index file can be moved elsewhere and replaced in the data directory by symlinks. The format file cannot.
You can symlink the data file and the index file independently to different directories.
To instruct a running MySQL server to perform the symlinking, use the 'DATA DIRECTORY' and 'INDEX DIRECTORY' options to note 'CREATE TABLE': create-table. See note create-table::. Alternatively, if *note 'mysqld': mysqld. is not running, symlinking can be accomplished manually using 'ln -s' from the command line.
Note:
The path used with either or both of the 'DATA DIRECTORY' and 'INDEX DIRECTORY' options may not include the MySQL 'data' directory. (Bug #32167)
note 'myisamchk': myisamchk. does not replace a symlink with the data file or index file. It works directly on the file to which the symlink points. Any temporary files are created in the directory where the data file or index file is located. The same is true for the note 'ALTER TABLE': alter-table, note 'OPTIMIZE TABLE': optimize-table, and note 'REPAIR TABLE': repair-table. statements.
Note:
When you drop a table that is using symlinks, both the symlink and the file to which the symlink points are dropped. This is an extremely good reason not to run *note 'mysqld': mysqld. as the 'root' operating system user or permit operating system users to have write access to MySQL database directories.
If you rename a table with note 'ALTER TABLE ... RENAME': alter-table. or note 'RENAME TABLE': rename-table. and you do not move the table to another database, the symlinks in the database directory are renamed to the new names and the data file and index file are renamed accordingly.
If you use note 'ALTER TABLE ... RENAME': alter-table. or note 'RENAME TABLE': rename-table. to move a table to another database, the table is moved to the other database directory. If the table name changed, the symlinks in the new database directory are renamed to the new names and the data file and index file are renamed accordingly.
If you are not using symlinks, start note 'mysqld': mysqld. with the '--skip-symbolic-links' option to ensure that no one can use note 'mysqld': mysqld. to drop or rename a file outside of the data directory.
These table symlink operations are not supported:
*note 'ALTER TABLE': alter-table. ignores the 'DATA DIRECTORY' and 'INDEX DIRECTORY' table options.
As indicated previously, only the data and index files can be symbolic links. The '.frm' file must never be a symbolic link. Attempting to do this (for example, to make one table name a synonym for another) produces incorrect results. Suppose that you have a database 'db1' under the MySQL data directory, a table 'tbl1' in this database, and in the 'db1' directory you make a symlink 'tbl2' that points to 'tbl1':
$> cd /PATH/TO/DATADIR/db1
$> ln -s tbl1.frm tbl2.frm
$> ln -s tbl1.MYD tbl2.MYD
$> ln -s tbl1.MYI tbl2.MYI
Problems result if one thread reads 'db1.tbl1' and another thread updates 'db1.tbl2':
* The query cache is 'fooled' (it has no way of knowing that
'tbl1' has not been updated, so it returns outdated results).
* 'ALTER' statements on 'tbl2' fail.
File: manual.info.tmp, Node: windows-symbolic-links, Prev: symbolic-links-to-tables, Up: symbolic-links
8.12.3.3 Using Symbolic Links for Databases on Windows ......................................................
On Windows, symbolic links can be used for database directories. This enables you to put a database directory at a different location (for example, on a different disk) by setting up a symbolic link to it. Use of database symlinks on Windows is similar to their use on Unix, although the procedure for setting up the link differs.
Suppose that you want to place the database directory for a database named 'mydb' at 'D:'. To do this, create a symbolic link in the MySQL data directory that points to 'D:'. However, before creating the symbolic link, make sure that the 'D:' directory exists by creating it if necessary. If you already have a database directory named 'mydb' in the data directory, move it to 'D:'. Otherwise, the symbolic link is ineffective. To avoid problems, make sure that the server is not running when you move the database directory.
On Windows, you can create a symlink using the 'mklink' command. This command requires administrative privileges.
Make sure that the desired path to the database exists. For this example, we use 'D:', and a database named 'mydb'.
If the database does not already exist, issue 'CREATE DATABASE mydb' in the *note 'mysql': mysql. client to create it.
Stop the MySQL service.
Using Windows Explorer or the command line, move the directory 'mydb' from the data directory to 'D:', replacing the directory of the same name.
If you are not already using the command prompt, open it, and change location to the data directory, like this:
C:\> cd \PATH\TO\DATADIR
If your MySQL installation is in the default location, you can use this:
C:\> cd C:\ProgramData\MySQL\MySQL Server 5.7\Data
In the data directory, create a symlink named 'mydb' that points to the location of the database directory:
C:\> mklink /d mydb D:\data\mydb
Start the MySQL service.
After this, all tables created in the database 'mydb' are created in 'D:'.
Alternatively, on any version of Windows supported by MySQL, you can create a symbolic link to a MySQL database by creating a '.sym' file in the data directory that contains the path to the destination directory. The file should be named 'DB_NAME.sym', where DB_NAME is the database name.
Support for database symbolic links on Windows using '.sym' files is enabled by default. If you do not need '.sym' file symbolic links, you can disable support for them by starting *note 'mysqld': mysqld. with the '--skip-symbolic-links' option. To determine whether your system supports '.sym' file symbolic links, check the value of the 'have_symlink' system variable using this statement:
SHOW VARIABLES LIKE 'have_symlink';
To create a '.sym' file symlink, use this procedure:
Change location into the data directory:
C:\> cd \PATH\TO\DATADIR
In the data directory, create a text file named 'mydb.sym' that contains this path name: 'D:'
Note:
The path name to the new database and tables should be absolute. If you specify a relative path, the location is relative to the 'mydb.sym' file.
After this, all tables created in the database 'mydb' are created in 'D:'.
Note:
Because support for '.sym' files is redundant with native symlink support available using 'mklink', use of '.sym' files is deprecated; expect support for them to be removed in a future MySQL release.
The following limitations apply to the use of '.sym' files for database symbolic linking on Windows. These limitations do not apply for symlinks created using 'mklink'.
The symbolic link is not used if a directory with the same name as the database exists in the MySQL data directory.
The '--innodb_file_per_table' option cannot be used.
If you run *note 'mysqld': mysqld. as a service, you cannot use a mapped drive to a remote server as the destination of the symbolic link. As a workaround, you can use the full path ('\servername').
File: manual.info.tmp, Node: optimizing-memory, Prev: symbolic-links, Up: optimizing-server
Menu:
large-page-support:: Enabling Large Page Support
File: manual.info.tmp, Node: memory-use, Next: monitor-mysql-memory-use, Prev: optimizing-memory, Up: optimizing-memory
8.12.4.1 How MySQL Uses Memory ..............................
MySQL allocates buffers and caches to improve performance of database operations. The default configuration is designed to permit a MySQL server to start on a virtual machine that has approximately 512MB of RAM. You can improve MySQL performance by increasing the values of certain cache and buffer-related system variables. You can also modify the default configuration to run MySQL on systems with limited memory.
The following list describes some of the ways that MySQL uses memory. Where applicable, relevant system variables are referenced. Some items are storage engine or feature specific.
The 'InnoDB' buffer pool is a memory area that holds cached 'InnoDB' data for tables, indexes, and other auxiliary buffers. For efficiency of high-volume read operations, the buffer pool is divided into pages that can potentially hold multiple rows. For efficiency of cache management, the buffer pool is implemented as a linked list of pages; data that is rarely used is aged out of the cache, using a variation of the LRU algorithm. For more information, see *note innodb-buffer-pool::.
The size of the buffer pool is important for system performance:
* 'InnoDB' allocates memory for the entire buffer pool at server
startup, using 'malloc()' operations. The
'innodb_buffer_pool_size' system variable defines the buffer
pool size. Typically, a recommended 'innodb_buffer_pool_size'
value is 50 to 75 percent of system memory.
'innodb_buffer_pool_size' can be configured dynamically, while
the server is running. For more information, see *note
innodb-buffer-pool-resize::.
* On systems with a large amount of memory, you can improve
concurrency by dividing the buffer pool into multiple buffer
pool instances. The 'innodb_buffer_pool_instances' system
variable defines the number of buffer pool instances.
* A buffer pool that is too small may cause excessive churning
as pages are flushed from the buffer pool only to be required
again a short time later.
* A buffer pool that is too large may cause swapping due to
competition for memory.
All threads share the *note 'MyISAM': myisam-storage-engine. key buffer. The 'key_buffer_size' system variable determines its size.
For each 'MyISAM' table the server opens, the index file is opened once; the data file is opened once for each concurrently running thread that accesses the table. For each concurrent thread, a table structure, column structures for each column, and a buffer of size '3 * N' are allocated (where N is the maximum row length, not counting note 'BLOB': blob. columns). A note 'BLOB': blob. column requires five to eight bytes plus the length of the *note 'BLOB': blob. data. The 'MyISAM' storage engine maintains one extra row buffer for internal use.
The 'myisam_use_mmap' system variable can be set to 1 to enable memory-mapping for all 'MyISAM' tables.
If an internal in-memory temporary table becomes too large (as determined using the 'tmp_table_size' and 'max_heap_table_size' system variables), MySQL automatically converts the table from in-memory to on-disk format. On-disk temporary tables use the storage engine defined by the 'internal_tmp_disk_storage_engine' system variable. You can increase the permissible temporary table size as described in *note internal-temporary-tables::.
For note 'MEMORY': memory-storage-engine. tables explicitly created with note 'CREATE TABLE': create-table, only the 'max_heap_table_size' system variable determines how large a table can grow, and there is no conversion to on-disk format.
The note MySQL Performance Schema: performance-schema. is a feature for monitoring MySQL server execution at a low level. The Performance Schema dynamically allocates memory incrementally, scaling its memory use to actual server load, instead of allocating required memory during server startup. Once memory is allocated, it is not freed until the server is restarted. For more information, see note performance-schema-memory-model::.
Each thread that the server uses to manage client connections requires some thread-specific space. The following list indicates these and which system variables control their size:
* A stack ('thread_stack')
* A connection buffer ('net_buffer_length')
* A result buffer ('net_buffer_length')
The connection buffer and result buffer each begin with a size equal to 'net_buffer_length' bytes, but are dynamically enlarged up to 'max_allowed_packet' bytes as needed. The result buffer shrinks to 'net_buffer_length' bytes after each SQL statement. While a statement is running, a copy of the current statement string is also allocated.
Each connection thread uses memory for computing statement digests. The server allocates 'max_digest_length' bytes per session. See *note performance-schema-statement-digests::.
All threads share the same base memory.
When a thread is no longer needed, the memory allocated to it is released and returned to the system unless the thread goes back into the thread cache. In that case, the memory remains allocated.
Each request that performs a sequential scan of a table allocates a read buffer. The 'read_buffer_size' system variable determines the buffer size.
When reading rows in an arbitrary sequence (for example, following a sort), a random-read buffer may be allocated to avoid disk seeks. The 'read_rnd_buffer_size' system variable determines the buffer size.
All joins are executed in a single pass, and most joins can be done without even using a temporary table. Most temporary tables are memory-based hash tables. Temporary tables with a large row length (calculated as the sum of all column lengths) or that contain *note 'BLOB': blob. columns are stored on disk.
Most requests that perform a sort allocate a sort buffer and zero to two temporary files depending on the result set size. See *note temporary-files::.
Almost all parsing and calculating is done in thread-local and reusable memory pools. No memory overhead is needed for small items, thus avoiding the normal slow memory allocation and freeing. Memory is allocated only for unexpectedly large strings.
For each table having note 'BLOB': blob. columns, a buffer is enlarged dynamically to read in larger note 'BLOB': blob. values. If you scan a table, the buffer grows as large as the largest *note 'BLOB': blob. value.
MySQL requires memory and descriptors for the table cache. Handler structures for all in-use tables are saved in the table cache and managed as 'First In, First Out' (FIFO). The 'table_open_cache' system variable defines the initial table cache size; see *note table-cache::.
MySQL also requires memory for the table definition cache. The 'table_definition_cache' system variable defines the number of table definitions (from '.frm' files) that can be stored in the table definition cache. If you use a large number of tables, you can create a large table definition cache to speed up the opening of tables. The table definition cache takes less space and does not use file descriptors, unlike the table cache.
A 'FLUSH TABLES' statement or *note 'mysqladmin flush-tables': mysqladmin. command closes all tables that are not in use at once and marks all in-use tables to be closed when the currently executing thread finishes. This effectively frees most in-use memory. 'FLUSH TABLES' does not return until all tables have been closed.
The server caches information in memory as a result of note 'GRANT': grant, note 'CREATE USER': create-user, note 'CREATE SERVER': create-server, and note 'INSTALL PLUGIN': install-plugin. statements. This memory is not released by the corresponding note 'REVOKE': revoke, note 'DROP USER': drop-user, note 'DROP SERVER': drop-server, and note 'UNINSTALL PLUGIN': uninstall-plugin. statements, so for a server that executes many instances of the statements that cause caching, cached memory use is very likely to increase unless it is freed with 'FLUSH PRIVILEGES'.
'ps' and other system status programs may report that note 'mysqld': mysqld. uses a lot of memory. This may be caused by thread stacks on different memory addresses. For example, the Solaris version of 'ps' counts the unused memory between stacks as used memory. To verify this, check available swap with 'swap -s'. We test note 'mysqld': mysqld. with several memory-leakage detectors (both commercial and Open Source), so there should be no memory leaks.
File: manual.info.tmp, Node: monitor-mysql-memory-use, Next: large-page-support, Prev: memory-use, Up: optimizing-memory
8.12.4.2 Monitoring MySQL Memory Usage ......................................
The following example demonstrates how to use note Performance Schema: performance-schema. and note sys schema: sys-schema. to monitor MySQL memory usage.
Most Performance Schema memory instrumentation is disabled by default. Instruments can be enabled by updating the 'ENABLED' column of the Performance Schema *note 'setup_instruments': performance-schema-setup-instruments-table. table. Memory instruments have names in the form of 'memory/CODE_AREA/INSTRUMENT_NAME', where CODE_AREA is a value such as 'sql' or 'innodb', and INSTRUMENT_NAME is the instrument detail.
To view available MySQL memory instruments, query the Performance Schema *note 'setup_instruments': performance-schema-setup-instruments-table. table. The following query returns hundreds of memory instruments for all code areas.
mysql> SELECT * FROM performance_schema.setup_instruments
WHERE NAME LIKE '%memory%';
You can narrow results by specifying a code area. For example, you can limit results to 'InnoDB' memory instruments by specifying 'innodb' as the code area.
mysql> SELECT * FROM performance_schema.setup_instruments
WHERE NAME LIKE '%memory/innodb%';
+-------------------------------------------+---------+-------+
| NAME | ENABLED | TIMED |
+-------------------------------------------+---------+-------+
| memory/innodb/adaptive hash index | NO | NO |
| memory/innodb/buf_buf_pool | NO | NO |
| memory/innodb/dict_stats_bg_recalc_pool_t | NO | NO |
| memory/innodb/dict_stats_index_map_t | NO | NO |
| memory/innodb/dict_stats_n_diff_on_level | NO | NO |
| memory/innodb/other | NO | NO |
| memory/innodb/row_log_buf | NO | NO |
| memory/innodb/row_merge_sort | NO | NO |
| memory/innodb/std | NO | NO |
| memory/innodb/trx_sys_t::rw_trx_ids | NO | NO |
...
Depending on your MySQL installation, code areas may include 'performance_schema', 'sql', 'client', 'innodb', 'myisam', 'csv', 'memory', 'blackhole', 'archive', 'partition', and others.
To enable memory instruments, add a 'performance-schema-instrument' rule to your MySQL configuration file. For example, to enable all memory instruments, add this rule to your configuration file and restart the server:
performance-schema-instrument='memory/%=COUNTED'
Note:
Enabling memory instruments at startup ensures that memory allocations that occur at startup are counted.
After restarting the server, the 'ENABLED' column of the Performance Schema note 'setup_instruments': performance-schema-setup-instruments-table. table should report 'YES' for memory instruments that you enabled. The 'TIMED' column in the note 'setup_instruments': performance-schema-setup-instruments-table. table is ignored for memory instruments because memory operations are not timed.
mysql> SELECT * FROM performance_schema.setup_instruments
WHERE NAME LIKE '%memory/innodb%';
+-------------------------------------------+---------+-------+
| NAME | ENABLED | TIMED |
+-------------------------------------------+---------+-------+
| memory/innodb/adaptive hash index | NO | NO |
| memory/innodb/buf_buf_pool | NO | NO |
| memory/innodb/dict_stats_bg_recalc_pool_t | NO | NO |
| memory/innodb/dict_stats_index_map_t | NO | NO |
| memory/innodb/dict_stats_n_diff_on_level | NO | NO |
| memory/innodb/other | NO | NO |
| memory/innodb/row_log_buf | NO | NO |
| memory/innodb/row_merge_sort | NO | NO |
| memory/innodb/std | NO | NO |
| memory/innodb/trx_sys_t::rw_trx_ids | NO | NO |
...
Query memory instrument data. In this example, memory instrument data is queried in the Performance Schema *note 'memory_summary_global_by_event_name': performance-schema-memory-summary-tables. table, which summarizes data by 'EVENT_NAME'. The 'EVENT_NAME' is the name of the instrument.
The following query returns memory data for the 'InnoDB' buffer pool. For column descriptions, see *note performance-schema-memory-summary-tables::.
mysql> SELECT * FROM performance_schema.memory_summary_global_by_event_name
WHERE EVENT_NAME LIKE 'memory/innodb/buf_buf_pool'\G
EVENT_NAME: memory/innodb/buf_buf_pool
COUNT_ALLOC: 1
COUNT_FREE: 0
SUM_NUMBER_OF_BYTES_ALLOC: 137428992
SUM_NUMBER_OF_BYTES_FREE: 0
LOW_COUNT_USED: 0
CURRENT_COUNT_USED: 1
HIGH_COUNT_USED: 1
LOW_NUMBER_OF_BYTES_USED: 0
CURRENT_NUMBER_OF_BYTES_USED: 137428992
HIGH_NUMBER_OF_BYTES_USED: 137428992
The same underlying data can be queried using the note 'sys': sys-schema. schema note 'memory_global_by_current_bytes': sys-memory-global-by-current-bytes. table, which shows current memory usage within the server globally, broken down by allocation type.
mysql> SELECT * FROM sys.memory_global_by_current_bytes
WHERE event_name LIKE 'memory/innodb/buf_buf_pool'\G
*************************** 1. row ***************************
event_name: memory/innodb/buf_buf_pool
current_count: 1
current_alloc: 131.06 MiB
current_avg_alloc: 131.06 MiB
high_count: 1
high_alloc: 131.06 MiB
high_avg_alloc: 131.06 MiB
This *note 'sys': sys-schema. schema query aggregates currently allocated memory ('current_alloc') by code area:
mysql> SELECT SUBSTRING_INDEX(event_name,'/',2) AS
code_area, sys.format_bytes(SUM(current_alloc))
AS current_alloc
FROM sys.x$memory_global_by_current_bytes
GROUP BY SUBSTRING_INDEX(event_name,'/',2)
ORDER BY SUM(current_alloc) DESC;
+---------------------------+---------------+
| code_area | current_alloc |
+---------------------------+---------------+
| memory/innodb | 843.24 MiB |
| memory/performance_schema | 81.29 MiB |
| memory/mysys | 8.20 MiB |
| memory/sql | 2.47 MiB |
| memory/memory | 174.01 KiB |
| memory/myisam | 46.53 KiB |
| memory/blackhole | 512 bytes |
| memory/federated | 512 bytes |
| memory/csv | 512 bytes |
| memory/vio | 496 bytes |
+---------------------------+---------------+
For more information about note 'sys': sys-schema. schema, see note sys-schema::.
File: manual.info.tmp, Node: large-page-support, Prev: monitor-mysql-memory-use, Up: optimizing-memory
8.12.4.3 Enabling Large Page Support ....................................
Some hardware and operating system architectures support memory pages greater than the default (usually 4KB). The actual implementation of this support depends on the underlying hardware and operating system. Applications that perform a lot of memory accesses may obtain performance improvements by using large pages due to reduced Translation Lookaside Buffer (TLB) misses.
In MySQL, large pages can be used by *note 'InnoDB': innodb-storage-engine, to allocate memory for its buffer pool and additional memory pool.
Standard use of large pages in MySQL attempts to use the largest size supported, up to 4MB. Under Solaris, a 'super large pages' feature enables uses of pages up to 256MB. This feature is available for recent SPARC platforms. It can be enabled or disabled by using the '--super-large-pages' or '--skip-super-large-pages' option.
MySQL also supports the Linux implementation of large page support (which is called HugeTLB in Linux).
Before large pages can be used on Linux, the kernel must be enabled to support them and it is necessary to configure the HugeTLB memory pool. For reference, the HugeTBL API is documented in the 'Documentation/vm/hugetlbpage.txt' file of your Linux sources.
The kernels for some recent systems such as Red Hat Enterprise Linux may have the large pages feature enabled by default. To check whether this is true for your kernel, use the following command and look for output lines containing 'huge':
$> grep -i huge /proc/meminfo
AnonHugePages: 2658304 kB
ShmemHugePages: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Hugetlb: 0 kB
The nonempty command output indicates that large page support is present, but the zero values indicate that no pages are configured for use.
If your kernel needs to be reconfigured to support large pages, consult the 'hugetlbpage.txt' file for instructions.
Assuming that your Linux kernel has large page support enabled, configure it for use by MySQL using the following steps:
Determine the number of large pages needed. This is the size of the InnoDB buffer pool divided by the large page size, which we can calculate as 'innodb_buffer_pool_size' / 'Hugepagesize'. Assuming the default value for the 'innodb_buffer_pool_size' (128MB) and using the 'Hugepagesize' value obtained from '/proc/meminfo' (2MB), this is 128MB / 2MB, or 64 Huge Pages. We call this value P.
As system root, open the file '/etc/sysctl.conf' in a text editor, and add the line shown here, where P is the number of large pages obtained in the previous step:
vm.nr_hugepages=P
Using the actual value obtained previously, the additional line should look like this:
vm.nr_hugepages=66
Save the updated file.
As system root, run the following command:
$> sudo sysctl -p
Note:
On some systems the large pages file may be named slightly differently; for example, some distributions call it 'nr_hugepages'. In the event 'sysctl' returns an error relating to the file name, check the name of the corresponding file in '/proc/sys/vm' and use that instead.
To verify the large page configuration, check '/proc/meminfo' again as described previously. Now you should see some additional nonzero values in the output, similar to this:
$> grep -i huge /proc/meminfo
AnonHugePages: 2686976 kB
ShmemHugePages: 0 kB
HugePages_Total: 233
HugePages_Free: 233
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Hugetlb: 477184 kB
Optionally, you may wish to compact the Linux VM. You can do this using a sequence of commands, possibly in a script file, similar to what is shown here:
sync
sync
sync
echo 3 > /proc/sys/vm/drop_caches
echo 1 > /proc/sys/vm/compact_memory
See your operating platform documentation for more information about how to do this.
Check any configuration files such as 'my.cnf' used by the server, and make sure that 'innodb_buffer_pool_chunk_size' is set larger than the huge page size. The default for this variable is 128M.
Large page support in the MySQL server is disabled by default. To enable it, start the server with '--large-pages'. You can also do so by adding the following line to the '[mysqld]' section of the server 'my.cnf' file:
large-pages=ON
With this option enabled, 'InnoDB' uses large pages automatically for its buffer pool and additional memory pool. If 'InnoDB' cannot do this, it falls back to use of traditional memory and writes a warning to the error log: 'Warning: Using conventional memory pool'.
You can verify that MySQL is now using large pages by checking '/proc/meminfo' again after restarting *note 'mysqld': mysqld, like this:
$> grep -i huge /proc/meminfo
AnonHugePages: 2516992 kB
ShmemHugePages: 0 kB
HugePages_Total: 233
HugePages_Free: 222
HugePages_Rsvd: 55
HugePages_Surp: 0
Hugepagesize: 2048 kB
Hugetlb: 477184 kB
File: manual.info.tmp, Node: optimize-benchmarking, Next: thread-information, Prev: optimizing-server, Up: optimization