- Community Pick
While going through the MySQL manual and the MySQL Server Memory Usage documents, I noted the following quick points regarding how MySQL memory uses can be computed.
This post will summarise my understanding and, at the end, will provide a code snippet for the same memory usage calculations.
We have global buffers which are allocated irrespective of connections as and when MySQL server is started. Along with that MySQL server allocates memory to each thread to perform respective tasks.
So the formula goes:
MySQL Server Memory Usage = Sum of Global Buffers + (number of Connection * Per thread memory variables).
Global buffers include:
- key_buffer_size: key_buffer_size is the size of the buffer used for index blocks.
- innodb_buffer_pool_size: The size in bytes of the memory buffer InnoDB uses to cache data and indexes of its tables.
- innodb_additional_mem_poo
l_size: The size in bytes of a memory pool InnoDB uses to store data dictionary information and other internal data structures.
- innodb_log_buffer_size: The size in bytes of the buffer that InnoDB uses to write to the log files on disk.
- query_cache_size: The amount of memory allocated for caching query results.
Per thread variables include:
- read_buffer_size: Buffer memory used for sequential table scan.
- read_rnd_buffer_size: Memory used for random read buffer / sorting.
- sort_buffer_size: Memory allocated for sorting, Group By, Order By.
- join_buffer_size: The size of the buffer that is used for plain index scans, range index scans, and joins that do not use indexes and thus perform full table scans.
- thread_stack: The stack size for each thread.
- net_buffer_length: Connection buffer
- max_allowed_packet: Up to this size net_buffer_length can grow.
- tmp_table_size - temporary / hash tables in MySQL
Note that,
If size increases or if table have blob columns, instead of heap tables on-disk tables created.
Memories for variables read_buffer_size, sort_buffer_size, read_rnd_buffer_size, tmp_table_size are allocated as & when required. They are also de-allocated once the task is accomplished.
Of course this barely gives us an idea regarding the MySQL Server memory usage, but hopefully it gave you the basics of the concept!
This article is also available on my blog for further change or updation.
Note: For MySQL 5.1.* it will throw error 1064.
Consider a bug report. associated with the code.
Solution for MySQL 5.1+:
Changing the cursor declaration from
DECLARE CUR_GBLVAR CURSOR FOR SHOW GLOBAL VARIABLES;
to
DECLARE CUR_GBLVAR CURSOR FOR SELECT * FROM information_schema.GLOBAL_
will make stored procedure work fine.
Below is a stored procedure to calculate the same estimates of memory consumption using Global Variables.
by: mwvisa1 on 2009-12-17 at 10:22:05ID: 6887
Nice article, theGhost_k8.
Voted yes above!