Tiup相关

1
2
3
tiup cluster list 
tiup cluster display <cluster_name>
tiup cluster reload <cluster_name>

创建数据库

1
2
3
CREATE DATABASE IF NOT EXISTS `testdb`
DEFAULT CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;

创建用户

1
CREATE USER 'test_user'@'%' IDENTIFIED BY 'Test@123';

查看tidb版本号

1
select tidb_version();

用户授权

1
2
3
4
5
6
7
给部分权限
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER
ON `longrise_oa`.* TO 'appuser'@'%';
给所有权限
GRANT ALL PRIVILEGES
ON bikeshare.*
TO 'appuser'@'%';

查看某张表的列信息

1
show columns from table_name

查看索引

1
show index from table_name;

查看库或表的基本情况

1
show stats_meta where db_name ='' and table_name='';

查看当前库执行的ddl sql

1
ADMIN SHOW DDL JOBS

取消某个执行的DDL

1
ADMIN CANCEL DDL JOBS job_id;

查看DML 阻塞 DDL:

1
2
3
4
5
6
7
8
9
10
11
-- 1. 看哪个事务/SQL 阻塞了 DDL
TABLE mysql.tidb_mdl_view\G

-- 2. 看 DDL job 状态
ADMIN SHOW DDL JOBS;

-- 3. 根据 session_id 反查会话
SELECT * FROM information_schema.cluster_processlist WHERE id = xxx\G;

-- 4. 根据 session_id 反查事务
SELECT * FROM information_schema.cluster_tidb_trx WHERE session_id = xxx\G;

对表执行 一致性检查

1
ADMIN CHECK TABLE trips;

查看整个表的统计信息状态(包含更新时间)

1
SHOW STATS_HEALTHY WHERE table_name = 'trips';
  • healthy:统计信息健康度(0~100)
  • Db_name:库名
  • table_name:表名
  • Partition_name:分区名

查看当前连接客户端

1
show processlist;

查看统计信息更新时间

1
2
3
4
5
6
7
SELECT *
FROM mysql.stats_meta
WHERE table_id = (
SELECT tidb_table_id
FROM information_schema.tables
WHERE table_schema = 'bikeshare' AND table_name = 'trips'
);
1
2
3
4
5
6
version            : 462805263081013249
table_id : 176
modify_count : 0
count : 3537215
snapshot : 462805262058913793
last_stats_histograms_version : 462805263081013249
1
2
3
4
5
6
7
8
9
10
11
12
TiDB 每次更新统计信息时都会生成一个全局递增 TSO(timestamp),这个版本号实际上是:
统计信息的更新时间(逻辑时间戳)
来自 PD 的 TSO
虽然它不是直接的 datetime,但是:
⚠ TiDB 的 TSO = 物理时间 + 逻辑序列号
因此更大的 version 表示更“新”的统计信息。

TiDB TSO 的结构
TiDB(准确说 TiKV/PD)的 TSO 是一个 64 位整数,结构如下:
[ 高 48 bits = 物理时间戳 (ms) ] [ 低 16 bits = 逻辑序号 ]
因此:
物理时间戳(ms) = TSO >> 18 相当于 floor(TSO / 2^18)

转化TSO为实际时间

1
SELECT TIDB_PARSE_TSO(462805263081013249);

查看某个库下的表及大小

1
2
use [database]
show table status;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1)按库汇总大小(推荐)
SELECT
table_schema AS db,
FORMAT_BYTES(SUM(data_length + index_length)) AS total_size,
FORMAT_BYTES(SUM(data_length)) AS data_size,
FORMAT_BYTES(SUM(index_length)) AS index_size,
COUNT(*) AS table_count
FROM information_schema.tables
WHERE table_schema NOT IN ('mysql','information_schema','performance_schema','metrics')
GROUP BY table_schema
ORDER BY SUM(data_length + index_length) DESC;

2)整个集群“所有库总大小”
SELECT
FORMAT_BYTES(SUM(data_length + index_length)) AS cluster_total_logical_size
FROM information_schema.tables
WHERE table_schema NOT IN ('mysql','information_schema','performance_schema','metrics');

3)只看某个库
SELECT
FORMAT_BYTES(SUM(data_length + index_length)) AS db_total_size
FROM information_schema.tables
WHERE table_schema = 'your_db';

查看表的Region分布

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- 查看表数据在Region中的分布

SHOW TABLE [table_name] REGIONS [WhereClauseOptional];
SHOW TABLE [table_name] INDEX [index_name] REGIONS [WhereClauseOptional];
或者
SELECT
region_id,
start_key,
end_key,
written_bytes,
read_bytes
FROM information_schema.tikv_region_status
WHERE table_name = 'nps_pdar_igdr'
ORDER BY written_bytes DESC;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
SHOW TABLE REGIONS 会返回如下列:
REGION_ID:Region 的 ID。
START_KEY:Region 的 Start key。
END_KEY:Region 的 End key。
LEADER_ID:Region 的 Leader ID。
LEADER_STORE_ID:Region leader 所在的 store (TiKV) ID。
PEERS:Region 所有副本的 ID。
SCATTERING:Region 是否正在调度中。1 表示正在调度。
WRITTEN_BYTES:估算的 Region 在 1 个心跳周期内的写入数据量大小,单位是 byte。
READ_BYTES:估算的 Region 在 1 个心跳周期内的读数据量大小,单位是 byte。
APPROXIMATE_SIZE(MB):估算的 Region 的数据量大小,单位是 MB。
APPROXIMATE_KEYS:估算的 Region 内 Key 的个数。
SCHEDULING_CONSTRAINTS:Region 所在的表或者分区所关联的放置策略的规则。
SCHEDULING_STATE:关联了放置策略的 Region 的当前调度状态。
1
2
3
4
5
6
7
8
9
10
11
12
13
SELECT
p.STORE_ID,
COUNT(s.REGION_ID) PEER_COUNT
FROM
INFORMATION_SCHEMA.TIKV_REGION_STATUS s
JOIN INFORMATION_SCHEMA.TIKV_REGION_PEERS p ON s.REGION_ID = p.REGION_ID
WHERE
TABLE_NAME = 'table_name'
AND p.is_leader = 1
GROUP BY
p.STORE_ID
ORDER BY
PEER_COUNT DESC;

停止某个tidb节点

1
tiup cluster stop tidb-test -R tidb --node ip:port

concat拼接语句

1
select concat('analyze table  ',table_name,';') from information_schema.tables where table_schema='sysbench';

查事务

1
SELECT * FROM information_schema.cluster_tidb_trx WHERE id = 'TSO号';

查会话

1
SELECT * FROM information_schema.cluster_processlist WHERE id = session_id;

根据指纹查sql

1
2
3
4
5
6
SELECT
digest, digest_text, query_sample_text, last_seen
FROM information_schema.statements_summary_history
WHERE digest = 'becea4e92ae148b6be33557029855c7bb05629a448cc7514003dfb5f2463b84c'
ORDER BY last_seen DESC
LIMIT 5;

查询DML历史操作

1
2
3
4
5
6
7
8
SELECT
SAMPLE_USER,
SUMMARY_BEGIN_TIME,
SUMMARY_END_TIME,
DIGEST_TEXT
FROM information_schema.CLUSTER_STATEMENTS_SUMMARY_HISTORY
WHERE DIGEST_TEXT LIKE 'delete%%n_bc_cert_ukey%'
ORDER BY SUMMARY_END_TIME DESC;

收集统计信息

1
mysql -h ip -P port -uroot -pxxx -e "select concat('analyze table  ',table_name,';') as sql_text from information_schema.tables where table_schema='sysbench' and table_type= 'BASE TABLE';" | mysql -h ip -P port -uroot -pxxx

递归生成数据插入测试表

1
2
3
4
5
6
7
8
9
10
11
12
INSERT INTO 表名 (数字列, 字符列)
WITH RECURSIVE seq AS (
SELECT 1 AS n
UNION ALL
SELECT n + 1
FROM seq
WHERE n < 目标值
)
SELECT
n,
SUBSTRING(MD5(RAND()), 1, 5)
FROM seq;

收集集群clinic

1
2
3
4
5
6
7
##安装clinic
tiup install diag
tiup diag collect ${cluster-name} -f="-4h" -t="-2h" --include="monitor"
##收集指定时间
tiup diag collect tidb-ts-tp -f="2025-07-25 02:00:00" -t="2025-07-25 06:30:00" --include="monitor"
##打包clinic文件
tiup diag package ${filepath}