Skip to content

Instantly share code, notes, and snippets.

@MatiasFernandez
Last active May 7, 2021 20:10
Show Gist options
  • Save MatiasFernandez/e46f44b2fc06b8592af4fa4b6db06ba6 to your computer and use it in GitHub Desktop.
Save MatiasFernandez/e46f44b2fc06b8592af4fa4b6db06ba6 to your computer and use it in GitHub Desktop.
MySQL: Useful queries
-- List running queries
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST WHERE command = "Query" ORDER BY TIME DESC;
-- Indexes size
SELECT database_name, table_name, index_name, stat_value AS pages,
round(stat_value*@@innodb_page_size/1024/1024, 2) size_in_mb
FROM mysql.innodb_index_stats
WHERE stat_name = 'size'
ORDER BY 4 DESC;
-- Number of cached pages in innodb buffer pool
select count(*) from information_schema.innodb_buffer_page;
-- How much of each index is currently cached in innodb buffer pool
select
table_name as Table_Name, index_name as Index_Name,
count(*) as Page_Count, sum(data_size)/1024/1024 as Size_in_MB
from information_schema.innodb_buffer_page
group by table_name, index_name
order by Size_in_MB desc;
-- Page types cached in innodb pool
select
page_type as Page_Type,
sum(data_size)/1024/1024 as Size_in_MB
from information_schema.innodb_buffer_page
group by page_type
order by Size_in_MB desc;
-- See table estimated size (based on statistics)
SELECT
ENGINE AS 'Engine',
ROW_FORMAT AS 'Row Format',
TABLE_ROWS AS 'Est. # rows',
AUTO_INCREMENT AS 'Max. auto-inc ID',
AVG_ROW_LENGTH AS 'Est. avg row size (bytes)',
DATA_LENGTH / 1024 / 1024 AS 'Est. data size (MB)',
INDEX_LENGTH / 1024 / 1024 AS 'Est. index size (MB)',
(DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024 AS 'Est. total size (MB)'
FROM
information_schema.TABLES
WHERE
TABLE_NAME = "table_name";
-- List MySQL variables
SHOW VARIABLES;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment