MST

星途 面试题库

面试题:MySQL中information_schema库如何获取表的索引性能相关信息

在MySQL的information_schema库中,如何通过查询获取某个具体表的索引使用频率、索引大小等性能相关信息?请写出对应的SQL语句。
15.5万 热度难度
数据库MySQL

知识考点

AI 面试

面试题答案

一键面试

在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'替换为你实际的表名。

注意:

  1. performance_schema需要开启才能使用table_statistics表的相关功能,并且该库会有一定性能开销。
  2. 索引大小计算方式可能因存储引擎不同而有差异,上述total_size只是一种近似计算方式。