View which TiKV nodes store and distribute table data

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 查看表数据存储、分布在哪些tikv节点上

| username: niegang

[TiDB Usage Environment] Production Environment
[TiDB Version] V4.0.16
[Reproduction Path] SQL query did not return results
[Problem Encountered] Want to know the data distribution of a certain table and which KV nodes it is stored on
[Resource Configuration] Cluster with 14 TiKV nodes (16c 64G 2T *14)
[Attachment: Screenshot/Log/Monitoring]

| username: 啦啦啦啦啦 | Original post link

Try SHOW TABLE xxx REGIONS

| username: Kongdom | Original post link

Database: INFORMATION_SCHEMA
Tables: TIKV_REGION_PEERS, TIKV_REGION_STATUS, TIKV_STORE_STATUS

| username: niegang | Original post link

After executing SHOW TABLE xxx REGIONS, you can only see the following information without the region’s corresponding TiKV node information.

| username: tidb菜鸟一只 | Original post link

SELECT DISTINCT a.db_name, a.`TABLE_NAME`, a.`REGION_ID`, c.`STORE_ID`, b.`ADDRESS`
FROM INFORMATION_SCHEMA.`TIKV_REGION_STATUS` a
JOIN INFORMATION_SCHEMA.`TIKV_STORE_STATUS` b ON c.`STORE_ID` = b.`STORE_ID`
JOIN INFORMATION_SCHEMA.`TIKV_REGION_PEERS` c ON a.`REGION_ID` = c.`REGION_ID`
WHERE c.`IS_LEADER` = '1'
AND a.`TABLE_NAME` = 'b';
| username: 啦啦啦啦啦 | Original post link

With the LEADER_STORE_ID, you can know which TiKV it is on.

| username: wzf0072 | Original post link

This SQL can query the node information where Region IS_LEADER is located. You can remove c.IS_LEADER = '1':

SELECT DISTINCT a.db_name, a.TABLE_NAME, a.REGION_ID, c.STORE_ID, b.ADDRESS 
FROM INFORMATION_SCHEMA.TIKV_REGION_STATUS a, INFORMATION_SCHEMA.TIKV_STORE_STATUS b, INFORMATION_SCHEMA.TIKV_REGION_PEERS c
WHERE
-- c.IS_LEADER='1'AND 
c.STORE_ID = b.STORE_ID
AND a.REGION_ID = c.REGION_ID
AND table_name = 'b'
ORDER BY a.REGION_ID

| username: 我是咖啡哥 | Original post link

To hide TiFlash nodes, add r.IS_LEARNER = 0.

SELECT
  rs.DB_NAME,
  rs.TABLE_NAME,
  s.ADDRESS,
  r.IS_LEADER,
  r.IS_LEARNER,
  COUNT(r.REGION_ID)
FROM
  INFORMATION_SCHEMA.TIKV_REGION_PEERS r,
  INFORMATION_SCHEMA.TIKV_STORE_STATUS s,
  INFORMATION_SCHEMA.tikv_region_status rs
WHERE r.STORE_ID = s.STORE_ID
  AND r.REGION_ID = rs.REGION_ID
  AND rs.DB_NAME = 'xxx'
  AND rs.TABLE_NAME = 'tablename'
  AND r.IS_LEARNER = 0
GROUP BY rs.DB_NAME,
  rs.TABLE_NAME,
  s.ADDRESS,
  r.IS_LEADER,
  r.IS_LEARNER
ORDER BY r.IS_LEADER;

is_leader can be used to check if the region is a leader.

| username: niegang | Original post link

Execution successful! Impressive.

| username: niegang | Original post link

Execution failed…

| username: niegang | Original post link

Alright~ Thanks

| username: niegang | Original post link

I’ll give it a try.

| username: 我是咖啡哥 | Original post link

There is an issue with the quotation marks before and after the table name. :grinning:

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.