Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit e2391e4

Browse files
committed
Add a memory summary per thread, to use with the the processlist view in 5.7
1 parent b7b7ce8 commit e2391e4

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

views/p_s/memory_by_thread.sql

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License as published by
5+
the Free Software Foundation; version 2 of the License.
6+
7+
This program is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License
13+
along with this program; if not, write to the Free Software
14+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15+
16+
/*
17+
* View: memory_by_user_by_current_bytes
18+
*
19+
* Summarizes memory use by user using the 5.7 Performance Schema instrumentation.
20+
*
21+
* When the user found is NULL, it is assumed to be a "background" thread.
22+
*
23+
* mysql> select * from sys.memory_by_thread_by_current_bytes limit 5;
24+
* +-----------+----------------+--------------------+-------------------+-------------------+-------------------+-----------------+
25+
* | thread_id | user | current_count_used | current_allocated | current_avg_alloc | current_max_alloc | total_allocated |
26+
* +-----------+----------------+--------------------+-------------------+-------------------+-------------------+-----------------+
27+
* | 1 | sql/main | 29333 | 166.02 MiB | 5.80 KiB | 131.13 MiB | 196.00 MiB |
28+
* | 55 | root@localhost | 175 | 1.04 MiB | 6.09 KiB | 350.86 KiB | 67.37 MiB |
29+
* | 58 | root@localhost | 236 | 368.13 KiB | 1.56 KiB | 312.05 KiB | 130.34 MiB |
30+
* | 904 | root@localhost | 32 | 18.00 KiB | 576 bytes | 16.00 KiB | 6.68 MiB |
31+
* | 970 | root@localhost | 12 | 16.80 KiB | 1.40 KiB | 16.00 KiB | 1.20 MiB |
32+
* +-----------+----------------+--------------------+-------------------+-------------------+-------------------+-----------------+
33+
*
34+
*/
35+
36+
CREATE OR REPLACE
37+
ALGORITHM = TEMPTABLE
38+
DEFINER = 'root'@'localhost'
39+
SQL SECURITY INVOKER
40+
VIEW memory_by_thread_by_current_bytes (
41+
thread_id,
42+
user,
43+
current_count_used,
44+
current_allocated,
45+
current_avg_alloc,
46+
current_max_alloc,
47+
total_allocated
48+
) AS
49+
SELECT thread_id,
50+
IF(t.name = 'thread/sql/one_connection',
51+
CONCAT(t.processlist_user, '@', t.processlist_host),
52+
REPLACE(t.name, 'thread/', '')) user,
53+
SUM(mt.current_count_used) AS current_count_used,
54+
sys.format_bytes(SUM(mt.current_number_of_bytes_used)) AS current_allocated,
55+
sys.format_bytes(IFNULL(SUM(mt.current_number_of_bytes_used) / NULLIF(SUM(current_count_used), 0), 0)) AS current_avg_alloc,
56+
sys.format_bytes(MAX(mt.current_number_of_bytes_used)) AS current_max_alloc,
57+
sys.format_bytes(SUM(mt.sum_number_of_bytes_alloc)) AS total_allocated
58+
FROM performance_schema.memory_summary_by_thread_by_event_name AS mt
59+
JOIN performance_schema.threads AS t USING (thread_id)
60+
GROUP BY thread_id, IF(t.name = 'thread/sql/one_connection',
61+
CONCAT(t.processlist_user, '@', t.processlist_host),
62+
REPLACE(t.name, 'thread/', ''))
63+
ORDER BY SUM(current_number_of_bytes_used) DESC;
64+
65+
/*
66+
* View: x$memory_by_user_by_current_bytes
67+
*
68+
* Summarizes memory use by user
69+
*
70+
* When the user found is NULL, it is assumed to be a "background" thread.
71+
*
72+
* mysql> select * from sys.x$memory_by_thread_by_current_bytes limit 5;
73+
* +-----------+----------------+--------------------+-------------------+-------------------+-------------------+-----------------+
74+
* | thread_id | user | current_count_used | current_allocated | current_avg_alloc | current_max_alloc | total_allocated |
75+
* +-----------+----------------+--------------------+-------------------+-------------------+-------------------+-----------------+
76+
* | 1 | sql/main | 29333 | 174089450 | 5934.9351 | 137494528 | 205523135 |
77+
* | 55 | root@localhost | 173 | 1074664 | 6211.9306 | 359280 | 72248413 |
78+
* | 58 | root@localhost | 240 | 377099 | 1571.2458 | 319536 | 169483870 |
79+
* | 1152 | root@localhost | 30 | 56949 | 1898.3000 | 16391 | 1010024 |
80+
* | 1154 | root@localhost | 34 | 56369 | 1657.9118 | 16391 | 1958771 |
81+
* +-----------+----------------+--------------------+-------------------+-------------------+-------------------+-----------------+
82+
*
83+
*/
84+
85+
CREATE OR REPLACE
86+
ALGORITHM = TEMPTABLE
87+
DEFINER = 'root'@'localhost'
88+
SQL SECURITY INVOKER
89+
VIEW x$memory_by_thread_by_current_bytes (
90+
thread_id,
91+
user,
92+
current_count_used,
93+
current_allocated,
94+
current_avg_alloc,
95+
current_max_alloc,
96+
total_allocated
97+
) AS
98+
SELECT t.thread_id,
99+
IF(t.name = 'thread/sql/one_connection',
100+
CONCAT(t.processlist_user, '@', t.processlist_host),
101+
REPLACE(t.name, 'thread/', '')) user,
102+
SUM(mt.current_count_used) AS current_count_used,
103+
SUM(mt.current_number_of_bytes_used) AS current_allocated,
104+
IFNULL(SUM(mt.current_number_of_bytes_used) / NULLIF(SUM(current_count_used), 0), 0) AS current_avg_alloc,
105+
MAX(mt.current_number_of_bytes_used) AS current_max_alloc,
106+
SUM(mt.sum_number_of_bytes_alloc) AS total_allocated
107+
FROM performance_schema.memory_summary_by_thread_by_event_name AS mt
108+
JOIN performance_schema.threads AS t USING (thread_id)
109+
GROUP BY thread_id, IF(t.name = 'thread/sql/one_connection',
110+
CONCAT(t.processlist_user, '@', t.processlist_host),
111+
REPLACE(t.name, 'thread/', ''))
112+
ORDER BY SUM(mt.current_number_of_bytes_used) DESC;

0 commit comments

Comments
 (0)