简体   繁体   English

如何检查数据库中索引的内容?

[英]How to check contents of an index in a database?

Is there any way that I can see what is stored in those indexes?有什么方法可以查看这些索引中存储的内容吗?

I understand the query mentioned below lists down all the schema indexes and their details:我理解下面提到的查询列出了所有架构索引及其详细信息:

SELECT DISTINCT
    TABLE_NAME,
    INDEX_NAME
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = 'your_schema';

(taken from Mark Byers's response to How to see indexes for a database or table in MySQL? ) (摘自 Mark Byers 在 MySQL 中对 How to see indexes for a database or table 的回应?

I'll answer for MySQL only, becausePostgreSQL's information_schema does not have a table called statistics .我只会回答 MySQL,因为PostgreSQL 的 information_schema没有名为statistics的表。

SELECT <column> FROM <table> shows the values that are indexed. SELECT <column> FROM <table>显示索引的值。 In other words, the leaf nodes of the index are exactly the values in the columns the index was created for.换句话说,索引的节点正是为其创建索引的列中的值。

If you want to see the index data structure itself, MySQL provides no access to this.如果您想查看索引数据结构本身,MySQL 不提供对此的访问权限。 It depends on the table's storage engine.这取决于表的存储引擎。

There are some tools developed by an InnoDB expert Jeremy Cole that can inspect the internal structure of index pages. InnoDB 专家 Jeremy Cole 开发了一些工具,可以检查索引页的内部结构。 See https://blog.jcole.us/innodb/ for his blogs on this subject and https://github.com/jeremycole/innodb_ruby for his tools.有关此主题的博客,请参阅https://blog.jcole.us/innodb/ ,有关他的工具,请参阅 https ://github.com/jeremycole/innodb_ruby

You can use the query you mentioned to retrieve a list of indexes and their details for a specific schema.您可以使用您提到的查询来检索索引列表及其特定模式的详细信息。 You can also use the following query to get more detailed information about a specific index:您还可以使用以下查询来获取有关特定索引的更多详细信息:

SHOW INDEX FROM your_table_name WHERE Key_name = 'your_index_name';

This query should return the following information about the specified index:此查询应返回有关指定索引的以下信息:

Non_unique: Whether the index is unique or not Key_name: The name of the index Seq_in_index: The sequence number of the column within the index Column_name: The name of the column that is indexed Collation: The collation of the indexed column Cardinality: The number of unique values in the indexed column Sub_part: The length of indexed prefix for the column Packed: Information about the packed state of the index Null: Whether the indexed column can contain NULL values Index_type: The type of the index Comment: Additional information about the index Non_unique:索引是否唯一 Key_name:索引的名称 seq_in_index:列在索引中的序号 Column_name:被索引的列的名称 Collation:被索引列的排序规则索引列中的唯一值 Sub_part:列的索引前缀的长度 Packed:关于索引的 packed state 的信息 Null:索引列是否可以包含 NULL 值 Index_type:索引的类型 Comment:关于索引的附加信息

You can also use the following query to get the indexes for all tables in a specific schema:您还可以使用以下查询获取特定模式中所有表的索引:

SELECT TABLE_NAME, INDEX_NAME, COLUMN_NAME 
FROM INFORMATION_SCHEMA.STATISTICS 
WHERE TABLE_SCHEMA = 'your_schema'
ORDER BY TABLE_NAME, INDEX_NAME;

This should give you the table name, index name and column name that the index is on.这应该为您提供索引所在的表名、索引名和列名。

You can also use the following query to get the indexes for all tables in a specific schema and see the column in the index您还可以使用以下查询获取特定模式中所有表的索引并查看索引中的列

SELECT t.table_name,i.index_name, group_concat(i.column_name) as columns
FROM information_schema.statistics i
JOIN information_schema.tables t on t.table_name = i.table_name
WHERE i.table_schema = 'your_schema'
GROUP BY i.table_name,i.index_name
ORDER BY t.table_name,i.index_name;

This should give you the table name, index name and columns that the index is on.这应该为您提供表名、索引名和索引所在的列。

In any case, you should replace "your_schema" with the name of the schema you want to get the information for and "your_table_name" with the name of the table you want to get information for.在任何情况下,您都应该将“your_schema”替换为您想要获取其信息的模式的名称,并将“your_table_name”替换为您想要获取其信息的表的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM