[TiDB Usage Environment] Production Environment / Testing / PoC
[TiDB Version]
[Reproduction Path] What operations were performed when the issue occurred
[Encountered Issue: Problem Phenomenon and Impact] According to the official course, data is stored in RocksDB. How is index data stored in RocksDB?
[Resource Configuration] Go to TiDB Dashboard - Cluster Info - Hosts and take a screenshot of this page
[Attachments: Screenshots/Logs/Monitoring]
KV Mapping of Index Data
For regular indexes, MySQL has the concept of non-clustered indexes, especially in InnoDB, where the child nodes record the primary key information and then retrieve the result data through a table lookup.
In TiDB, index creation is supported, so how is the index information stored? It supports both primary and secondary indexes (including unique and non-unique indexes), and the mapping method is similar to table data.
The design is as follows:
TiDB assigns an IndexID to each index in the table.
For primary and unique indexes, it needs to quickly locate the RowID based on the key value, which will be stored in the value.
Therefore, the generated key-value pairs are:
Key: tablePrefix{TableID}_indexPrefixSep{IndexID}_indexedColumnsValue
Value: RowID
Since the designed key contains indexedColumnsValue, which is the value of the queried field, it can be directly hit or retrieved through fuzzy search. Then, through the RowID in the value, the corresponding row record can be retrieved from the table data mapping.
For regular indexes, a key value may correspond to multiple rows, and the corresponding RowID needs to be queried based on the key value range.
Key: tablePrefix{TableID}_indexPrefixSep{IndexID}indexedColumnsValue{RowID}
Value: null
Based on the field value, a list of relevant keys can be retrieved, and then the row records can be obtained based on the RowID contained in the key.
You can use SHOW TABLE table_name INDEX index_index_name REGIONS; to check the distribution of regions. The key-value representation differs for primary keys, unique indexes, and secondary indexes.