# TiDB METRICS_SCHEMA - 常用指标

> TiDB 8.5.4 METRICS_SCHEMA
> 
> 参考: https://docs.pingcap.com/zh/tidb/stable/metrics-schema/

## 说明

**常用指标**是深入分析和性能调优时需要的指标，包括：

- SQL 解析编译详情
- KV 请求重试
- DDL/GC/Schema 操作
- 统计信息更新
- TiKV Raft/调度/快照详情
- PD 调度详情

**使用场景**：
- 性能深入分析
- 调优参数
- 问题根因定位

## 统计

| 组件 | 数量 |
|------|------|
| TiDB | 25 |
| TiKV | 7 |
| PD | 5 |
| Go Runtime | 2 |
| **总计** | **39** |

---

## Go Runtime

### GC

#### `go_gc_cpu_usage`

**说明**: The fraction of TiDB/PD available CPU time used by the GC since the program started.

- **Labels**: `instance,job`
- **Quantile**: `-`

**PromQL**:
```promql
go_memstats_gc_cpu_fraction{$LABEL_CONDITIONS}
```

### 协程/线程

#### `go_threads`

**说明**: Total threads TiDB/PD process created currently

- **Labels**: `instance,job`
- **Quantile**: `-`

**PromQL**:
```promql
go_threads{$LABEL_CONDITIONS}
```

## PD

### Operator

#### `pd_operator_finish_duration`

**说明**: The quantile time consumed when the operator is finished

- **Labels**: `type`
- **Quantile**: `0.99`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(pd_schedule_finish_operators_duration_seconds_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le,type))
```

### gRPC

#### `pd_grpc_completed_commands_duration`

**说明**: The quantile time consumed of completing each kind of gRPC commands

- **Labels**: `instance,grpc_method`
- **Quantile**: `0.99`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(grpc_server_handling_seconds_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le,grpc_method,instance))
```

### 热点

#### `pd_hotspot_status`

**说明**: -

- **Labels**: `instance,address,store,type`
- **Quantile**: `-`

**PromQL**:
```promql
pd_hotspot_status{$LABEL_CONDITIONS}
```

### 调度

#### `pd_scheduler_balance_leader`

**说明**: The leader movement details among TiKV instances

- **Labels**: `instance,address,store,type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(delta(pd_scheduler_balance_leader{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (address,store,instance,type)
```

#### `pd_scheduler_balance_region`

**说明**: The Region movement details among TiKV instances

- **Labels**: `instance,address,store,type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(delta(pd_scheduler_balance_region{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (address,store,instance,type)
```

## TiDB

### DDL 操作

#### `tidb_ddl_duration`

**说明**: The quantile of TiDB DDL duration statistics

- **Labels**: `instance,type`
- **Quantile**: `0.95`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(tidb_ddl_handle_job_duration_seconds_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le, type,instance))
```

#### `tidb_ddl_opm`

**说明**: The quantile of executed DDL jobs per minute

- **Labels**: `instance,type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(rate(tidb_ddl_handle_job_duration_seconds_count{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (type,instance)
```

#### `tidb_ddl_waiting_jobs_num`

**说明**: TiDB ddl request in queue

- **Labels**: `instance,type`
- **Quantile**: `-`

**PromQL**:
```promql
tidb_ddl_waiting_jobs{$LABEL_CONDITIONS}
```

### GC

#### `tidb_gc_action_result_opm`

**说明**: kv storage garbage collection results including failed and successful ones

- **Labels**: `instance,type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(increase(tidb_tikvclient_gc_action_result{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (type,instance)
```

#### `tidb_gc_config`

**说明**: kv storage garbage collection config including gc_life_time and gc_run_interval

- **Labels**: `instance,type`
- **Quantile**: `-`

**PromQL**:
```promql
tidb_tikvclient_gc_config{$LABEL_CONDITIONS}
```

#### `tidb_gc_duration`

**说明**: The quantile of kv storage garbage collection time durations

- **Labels**: `instance,stage`
- **Quantile**: `0.95`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(tidb_tikvclient_gc_seconds_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le,instance,stage))
```

### KV 请求

#### `tidb_kv_backoff_duration`

**说明**: The quantile of kv backoff time durations(second)

- **Labels**: `instance,type`
- **Quantile**: `0.95`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(tidb_tikvclient_backoff_seconds_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le,type,instance))
```

#### `tidb_kv_backoff_ops`

**说明**: kv storage backoff times

- **Labels**: `instance,type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(rate(tidb_tikvclient_backoff_seconds_count{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (type,instance)
```

#### `tidb_kv_region_error_ops`

**说明**: kv region error times

- **Labels**: `instance,type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(rate(tidb_tikvclient_region_err_total{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (type,instance)
```

#### `tidb_kv_request_duration`

**说明**: The quantile of kv requests durations by store

- **Labels**: `instance,type,store`
- **Quantile**: `0.95`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(tidb_tikvclient_request_seconds_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le,type,store,instance))
```

#### `tidb_kv_request_ops`

**说明**: kv request total by instance and command type

- **Labels**: `instance,type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(rate(tidb_tikvclient_request_seconds_count{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (instance, type)
```

### SQL 编译

#### `tidb_compile_duration`

**说明**: The quantile time cost of building the query plan(second)

- **Labels**: `instance,sql_type`
- **Quantile**: `0.95`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(tidb_session_compile_duration_seconds_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le, sql_type,instance))
```

#### `tidb_parse_duration`

**说明**: The quantile time cost of parsing SQL to AST(second)

- **Labels**: `instance,sql_type`
- **Quantile**: `0.95`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(tidb_session_parse_duration_seconds_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le,sql_type,instance))
```

### Schema 加载

#### `tidb_load_schema_duration`

**说明**: The quantile of TiDB loading schema time durations by instance

- **Labels**: `instance`
- **Quantile**: `0.99`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(tidb_domain_load_schema_duration_seconds_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le, instance))
```

#### `tidb_load_schema_ops`

**说明**: TiDB loading schema times including both failed and successful ones

- **Labels**: `instance,type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(rate(tidb_domain_load_schema_total{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (instance,type)
```

### 事务

#### `tidb_transaction_retry_error_ops`

**说明**: Error numbers of transaction retry

- **Labels**: `instance,type,sql_type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(rate(tidb_session_retry_error_total{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (type,sql_type,instance)
```

### 其他

#### `tidb_prepared_statement_count`

**说明**: TiDB prepare statements count

- **Labels**: `instance`
- **Quantile**: `-`

**PromQL**:
```promql
tidb_server_prepared_stmts{$LABEL_CONDITIONS}
```

### 分布式查询

#### `tidb_distsql_execution_duration`

**说明**: The quantile durations of distsql execution(second)

- **Labels**: `instance,type`
- **Quantile**: `0.95`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(tidb_distsql_handle_query_duration_seconds_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le, type, instance))
```

#### `tidb_distsql_scan_key_num`

**说明**: The quantile numebr of distsql scan numbers

- **Labels**: `instance`
- **Quantile**: `0.95`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(tidb_distsql_scan_keys_num_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le,instance))
```

### 慢查询分析

#### `tidb_slow_query_cop_process_duration`

**说明**: The quantile of TiDB slow query statistics with slow query total cop process time(second)

- **Labels**: `instance`
- **Quantile**: `0.9`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(tidb_server_slow_query_cop_duration_seconds_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le,instance))
```

#### `tidb_slow_query_cop_wait_duration`

**说明**: The quantile of TiDB slow query statistics with slow query total cop wait time(second)

- **Labels**: `instance`
- **Quantile**: `0.9`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(tidb_server_slow_query_wait_duration_seconds_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le,instance))
```

#### `tidb_slow_query_total_time`

**说明**: The total time of TiDB slow query statistics with slow query time(second)

- **Labels**: `instance`
- **Quantile**: `-`

**PromQL**:
```promql
sum(increase(tidb_server_slow_query_process_duration_seconds_sum{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (instance)
```

### 查询性能

#### `tidb_distsql_qps`

**说明**: distsql query handling durations per second

- **Labels**: `instance,type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(rate(tidb_distsql_handle_query_duration_seconds_count{$LABEL_CONDITIONS}[$RANGE_DURATION]))
```

#### `tidb_query_total_time`

**说明**: The total time of TiDB query durations(second)

- **Labels**: `instance,sql_type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(increase(tidb_server_handle_query_duration_seconds_sum{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (instance,sql_type)
```

### 统计信息

#### `tidb_statistics_auto_analyze_duration`

**说明**: The quantile of TiDB auto analyze time durations within 95 percent histogram buckets

- **Labels**: `instance`
- **Quantile**: `0.95`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(tidb_statistics_auto_analyze_duration_seconds_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le,instance))
```

## TiKV

### Region 管理

#### `tikv_region_count`

**说明**: The number of regions on each TiKV instance

- **Labels**: `instance,type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(tikv_raftstore_region_count{$LABEL_CONDITIONS}) by (instance,type)
```

### Store

#### `tikv_store_size`

**说明**: The available or capacity size of each TiKV instance

- **Labels**: `instance,type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(tikv_store_size_bytes{$LABEL_CONDITIONS}) by (instance,type)
```

### 存储引擎

#### `tikv_compaction_duration`

**说明**: The time consumed when executing the compaction and flush operations

- **Labels**: `instance,type,db`
- **Quantile**: `-`

**PromQL**:
```promql
avg(tikv_engine_compaction_time{$LABEL_CONDITIONS}) by (type,instance,db)
```

#### `tikv_compaction_pending_bytes`

**说明**: The pending bytes to be compacted

- **Labels**: `instance,cf,db`
- **Quantile**: `-`

**PromQL**:
```promql
tikv_engine_pending_compaction_bytes{$LABEL_CONDITIONS}
```

### 快照

#### `tikv_snapshot_size`

**说明**: The quantile of snapshot size

- **Labels**: `instance`
- **Quantile**: `0.9999`

**PromQL**:
```promql
histogram_quantile($QUANTILE, sum(rate(tikv_snapshot_size_bucket{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (le,instance))
```

### 线程池

#### `tikv_thread_cpu`

**说明**: The CPU usage of each TiKV instance

- **Labels**: `instance,name`
- **Quantile**: `-`

**PromQL**:
```promql
sum(rate(tikv_thread_cpu_seconds_total{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (instance,name)
```

### 调度器

#### `tikv_scheduler_command_total_count`

**说明**: The total count of tikv scheduler executing command

- **Labels**: `instance,type`
- **Quantile**: `-`

**PromQL**:
```promql
sum(increase(tikv_scheduler_command_duration_seconds_count{$LABEL_CONDITIONS}[$RANGE_DURATION])) by (instance,type)
```

