HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Get memory usage of mysql query during runtime

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
duringqueryruntimeusagegetmysqlmemory

Problem

Is possible to obtain used memory in a running query?

Something like:

mysql> SELECT MEMORY_PEAK as memory; #return memory usage by mysql before run query
mysql> SELECT id FROM table WHERE id>2; #run query
mysql> SELECT MEMORY_PEAK as memory; #return memory usage by mysql after run query


and return something like (return 17kb):

+----------+
|  memory  |
+----------+
|  20100   |
+----------+

+------+
|  id  |
+------+
|  3   |
+------+

+----------+
|  memory  |
+----------+
|  2026767 |
+----------+


or

mysql> USAGE SELECT id FROM table WHERE id>2;

and return something like (return 17kb):

+---------+
|  USAGE  |
+---------+
|  16767  |
+---------+


Is this possible?

I need it to do various tests and see which engines queries and performs better (personal experience).

Solution

In MySQL 5.7, assuming you're using mostly INNODB tables, you can see how much memory is currently being used by running this query:

SHOW ENGINE INNODB STATUS


You can see the maximum amount of memory your DB will ever use by running this query:

SELECT ( @@key_buffer_size
+ @@query_cache_size
+ @@innodb_buffer_pool_size
+ @@innodb_log_buffer_size
+ @@max_connections * ( 
    @@read_buffer_size
    + @@read_rnd_buffer_size
    + @@sort_buffer_size
    + @@join_buffer_size
    + @@binlog_cache_size
    + @@thread_stack
    + @@tmp_table_size )
) / (1024 * 1024 * 1024) AS MAX_MEMORY_GB;

Code Snippets

SHOW ENGINE INNODB STATUS
SELECT ( @@key_buffer_size
+ @@query_cache_size
+ @@innodb_buffer_pool_size
+ @@innodb_log_buffer_size
+ @@max_connections * ( 
    @@read_buffer_size
    + @@read_rnd_buffer_size
    + @@sort_buffer_size
    + @@join_buffer_size
    + @@binlog_cache_size
    + @@thread_stack
    + @@tmp_table_size )
) / (1024 * 1024 * 1024) AS MAX_MEMORY_GB;

Context

StackExchange Database Administrators Q#56454, answer score: 7

Revisions (0)

No revisions yet.