Get memory usage of mysql query during runtime Is possible to obtain used memory in a running query?

Something like:

mysql&gt; SELECT MEMORY_PEAK as memory; #return memory usage by mysql before run query
mysql&gt; SELECT id FROM table WHERE id&gt;2; #run query
mysql&gt; 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&gt; USAGE SELECT id FROM table WHERE id&gt;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).
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;