您好,登錄后才能下訂單哦!
怎么在MySQL中查看使用的內存?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
performance_schema有如下表記錄內存使用情況
mysql> show tables like '%memory%summary%'; +-------------------------------------------------+ | Tables_in_performance_schema (%memory%summary%) | +-------------------------------------------------+ | memory_summary_by_account_by_event_name | | memory_summary_by_host_by_event_name | | memory_summary_by_thread_by_event_name | | memory_summary_by_user_by_event_name | | memory_summary_global_by_event_name | +-------------------------------------------------+
每個內存統計表都有如下統計列:
* COUNT_ALLOC,COUNT_FREE:對內存分配和釋放內存函數的調用總次數
* SUM_NUMBER_OF_BYTES_ALLOC,SUM_NUMBER_OF_BYTES_FREE:已分配和已釋放的內存塊的總字節大小
* CURRENT_COUNT_USED:這是一個便捷列,等于COUNT_ALLOC - COUNT_FREE
* CURRENT_NUMBER_OF_BYTES_USED:當前已分配的內存塊但未釋放的統計大小。這是一個便捷列,等于SUM_NUMBER_OF_BYTES_ALLOC - SUM_NUMBER_OF_BYTES_FREE
* LOW_COUNT_USED,HIGH_COUNT_USED:對應CURRENT_COUNT_USED列的低和高水位標記
* LOW_NUMBER_OF_BYTES_USED,HIGH_NUMBER_OF_BYTES_USED:對應CURRENT_NUMBER_OF_BYTES_USED列的低和高水位標記
內存統計表允許使用TRUNCATE TABLE語句。使用truncate語句時有如下行為:
* 通常,truncate操作會重置統計信息的基準數據(即清空之前的數據),但不會修改當前server的內存分配等狀態。也就是說,truncate內存統計表不會釋放已分配內存
* 將COUNT_ALLOC和COUNT_FREE列重置,并重新開始計數(等于內存統計信息以重置后的數值作為基準數據)
* SUM_NUMBER_OF_BYTES_ALLOC和SUM_NUMBER_OF_BYTES_FREE列重置與COUNT_ALLOC和COUNT_FREE列重置類似
* LOW_COUNT_USED和HIGH_COUNT_USED將重置為CURRENT_COUNT_USED列值
* LOW_NUMBER_OF_BYTES_USED和HIGH_NUMBER_OF_BYTES_USED將重置為CURRENT_NUMBER_OF_BYTES_USED列值
* 此外,按照帳戶,主機,用戶或線程分類統計的內存統計表或memory_summary_global_by_event_name表,如果在對其依賴的accounts、hosts、users表執行truncate時,會隱式對這些內存統計表執行truncate語句
簡單來說,就是可以根據用戶、主機、線程、賬號、全局的維度對內存進行監控。同時庫sys也就這些表做了進一步的格式化,可以使得用戶非常容易的觀察到每個對象的內存開銷:
mysql> select event_name,current_alloc from sys.memory_global_by_current_bytes limit 10; +-----------------------------------------------------------------------------+---------------+ | event_name | current_alloc | +-----------------------------------------------------------------------------+---------------+ | memory/performance_schema/events_statements_history_long | 13.66 MiB | | memory/performance_schema/events_statements_history_long.sqltext | 9.77 MiB | | memory/performance_schema/events_statements_history_long.tokens | 9.77 MiB | | memory/performance_schema/events_statements_summary_by_digest.tokens | 9.77 MiB | | memory/performance_schema/table_handles | 9.06 MiB | | memory/performance_schema/events_statements_summary_by_thread_by_event_name | 8.67 MiB | | memory/sql/String::value | 6.02 MiB | | memory/performance_schema/memory_summary_by_thread_by_event_name | 5.62 MiB | | memory/performance_schema/events_statements_summary_by_digest | 4.88 MiB | | memory/sql/TABLE | 4.35 MiB | +-----------------------------------------------------------------------------+---------------+
默認情況下performance_schema只對performance_schema進行了內存開銷的統計。根據你的MySQL安裝代碼區域可能包括performance_schema、sql、client、innodb、myisam、csv、memory、blackhole、archive、partition和其他。
查看innodb相關的內存監控是否開啟,默認不開啟
mysql> SELECT * FROM performance_schema.setup_instruments -> WHERE NAME LIKE '%memory%';
+--------------------------------------------------------------------------------+---------+-------+ | NAME | ENABLED | TIMED | +--------------------------------------------------------------------------------+---------+-------+ | memory/performance_schema/mutex_instances | YES | NO | | memory/performance_schema/rwlock_instances | YES | NO | | memory/performance_schema/cond_instances | YES | NO | | memory/performance_schema/file_instances | YES | NO | | memory/performance_schema/socket_instances | YES | NO | | memory/performance_schema/metadata_locks | YES | NO | | memory/performance_schema/file_handle | YES | NO | | memory/performance_schema/accounts | YES | NO | | memory/performance_schema/events_waits_summary_by_account_by_event_name | YES | NO | | memory/performance_schema/events_stages_summary_by_account_by_event_name | YES | NO | | memory/performance_schema/events_statements_summary_by_account_by_event_name | YES | NO | | memory/performance_schema/events_transactions_summary_by_account_by_event_name | YES | NO | | memory/performance_schema/memory_summary_by_account_by_event_name | YES | NO | | memory/performance_schema/events_stages_summary_global_by_event_name | YES | NO | | memory/performance_schema/events_statements_summary_global_by_event_name | YES | NO | | memory/performance_schema/memory_summary_global_by_event_name | YES | NO | | memory/performance_schema/hosts | YES | NO | | memory/performance_schema/events_waits_summary_by_host_by_event_name | YES | NO | | memory/performance_schema/events_stages_summary_by_host_by_event_name | YES | NO | | memory/performance_schema/events_statements_summary_by_host_by_event_name | YES | NO | | memory/performance_schema/events_transactions_summary_by_host_by_event_name | YES | NO |
可以通過條件縮小范圍:
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 |
對所有可能的對象進行內存監控。因此,還需要做下面的設置:
mysql> update performance_schema.setup_instruments set enabled = 'yes' where name like 'memory%'; Query OK, 306 rows affected (0.00 sec) Rows matched: 376 Changed: 306 Warnings: 0
但是這種在線打開內存統計的方法僅對之后新增的內存對象有效,重啟數據庫后又會還原設置:
如想要對全局生命周期中的對象進行內存統計,必須在配置文件中進行設置,然后重啟:
[mysqld] performance-schema-instrument='memory/%=COUNTED'
可以使用sys庫下的memory_global_by_current_bytes表來查詢相同的底層數據,該模式表顯示了全局服務器內當前內存使用情況,按分配類型進行細分。
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
此sys模式查詢通過current_alloc()代碼區域聚合當前分配的內存:
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 | +---------------------------+---------------+
關于怎么在MySQL中查看使用的內存問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。