面试题答案
一键面试在MySQL的information_schema
库中,可以通过以下SQL语句获取某个具体表的索引使用频率、索引大小等性能相关信息:
SELECT
stat.table_schema,
stat.table_name,
stat.index_name,
stat.used,
-- 索引大小(近似,单位为字节,不同存储引擎计算方式可能有差异)
(data_length + index_length) AS total_size
FROM
performance_schema.table_statistics stat
JOIN
information_schema.tables info
ON
stat.table_schema = info.table_schema
AND stat.table_name = info.table_name
WHERE
stat.table_schema = 'your_database_name'
AND stat.table_name = 'your_table_name';
上述SQL通过连接performance_schema.table_statistics
(包含索引使用频率信息)和information_schema.tables
(包含表大小相关信息)来获取指定数据库和表的相关性能信息。请将'your_database_name'
替换为你实际的数据库名,'your_table_name'
替换为你实际的表名。
注意:
performance_schema
需要开启才能使用table_statistics
表的相关功能,并且该库会有一定性能开销。- 索引大小计算方式可能因存储引擎不同而有差异,上述
total_size
只是一种近似计算方式。