MySQL 元数据
查询库、表、列、状态的元信息(information_schema)。
Key Points元数据就是「描述数据的数据」;SHOW 命令与 information_schema 都能查。
SHOW 系列
SQL
SHOW DATABASES; -- 所有库
SHOW TABLES; -- 当前库的表
SHOW COLUMNS FROM users; -- 表字段
SHOW INDEX FROM users; -- 索引
SHOW TABLE STATUS; -- 表大小、行数、引擎
SHOW PROCESSLIST; -- 当前连接Output
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| shop |
+--------------------+information_schema
SQL
-- 查所有表及其行数估算
SELECT table_name, table_rows, engine
FROM information_schema.tables
WHERE table_schema = 'shop';Output
+------------+------------+--------+
| table_name | table_rows | engine |
+------------+------------+--------+
| users | 4 | InnoDB |
| orders | 3 | InnoDB |
+------------+------------+--------+table_rows for InnoDB is an estimate from statistics, not exact. For precise counts use SELECT COUNT(*), which is slow on big tables.SHOW 系列
SQL
SHOW DATABASES; -- 所有库
SHOW TABLES; -- 当前库的表
SHOW COLUMNS FROM users; -- 表字段
SHOW INDEX FROM users; -- 索引
SHOW TABLE STATUS; -- 表大小、行数、引擎
SHOW PROCESSLIST; -- 当前连接Output
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| shop |
+--------------------+information_schema
SQL
-- 查所有表及其行数估算
SELECT table_name, table_rows, engine
FROM information_schema.tables
WHERE table_schema = 'shop';Output
+------------+------------+--------+
| table_name | table_rows | engine |
+------------+------------+--------+
| users | 4 | InnoDB |
| orders | 3 | InnoDB |
+------------+------------+--------+table_rows 在 InnoDB 下是估算值(来自统计信息),不是精确行数;要精确用 SELECT COUNT(*),但大表会很慢。