在上一篇《初相识|全方位认识information_schema》中,我们针对 information_schema 系统库做了一个简单的认识,本期我们将为大家带来系列第二篇《Server层统计信息字典表 | 全方位认识 information_schema》,下面请跟随我们一起开始 sys 系统库的学习之旅吧~
该表提供查询表对象中的列(字段)信息
下面是该表中存储的信息内容
admin@localhost : information_schema 05:32:07> select * from COLUMNS where TABLE_NAME='sbtest1' limit 1\G;
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: sbtest
TABLE_NAME: sbtest1
COLUMN_NAME: id
ORDINAL_POSITION: 1
COLUMN_DEFAULT: NULL
IS_NULLABLE: NO
DATA_TYPE: int
CHARACTER_MAXIMUM_LENGTH: NULL
CHARACTER_OCTET_LENGTH: NULL
NUMERIC_PRECISION: 10
NUMERIC_SCALE: 0
DATETIME_PRECISION: NULL
CHARACTER_SET_NAME: NULL
COLLATION_NAME: NULL
COLUMN_TYPE: int(10) unsigned
COLUMN_KEY: PRI
EXTRA: auto_increment
PRIVILEGES: select,insert,update,references
COLUMN_COMMENT:
GENERATION_EXPRESSION:
1 row in set (0.00 sec)
字段含义如下:
COLUMNS表中记录的信息实际上还可以使用show columns语句进行查询,下面给出两者的一个对比查询结果供大家鉴赏。
# 使用show columns语句来查询表对象的列信息
## 语法如下
SHOW [full] COLUMNS
FROM tbl_name
[FROM db_name]
[LIKE 'wild']
## 查询结果如下,当然,从这里我们也可以看到,使用show columns语句查询的列信息明显比直接查询columns表中的信息要少得多
root@localhost : information_schema 12:42:34> show columns from sbtest.sbtest1;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| k | int(10) unsigned | NO | MUL | 0 | |
| c | char(120) | NO | MUL | | |
| pad | char(60) | NO | | | |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.04 sec)
## 还可以结合where语句来进行过滤
root@localhost : information_schema 12:44:46> show columns from sbtest.sbtest1 where Field like 'id%';
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
+-------+------------------+------+-----+---------+----------------+
1 row in set (0.00 sec)
# 使用select语句来查询,这里我们就直接指定与show columns语句输出信息相对应的列来做一个对比
## 语法
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
[AND table_schema = 'db_name']
[AND column_name LIKE 'wild']
## 查询结果如下
root@localhost : information_schema 12:42:20> SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS where table_schema='sbtest' and table_name='sbtest1';
+-------------+-----------+-------------+----------------+
| COLUMN_NAME | DATA_TYPE | IS_NULLABLE | COLUMN_DEFAULT |
+-------------+-----------+-------------+----------------+
| id | int | NO | NULL |
| k | int | NO | 0 |
| c | char | NO | |
| pad | char | NO | |
+-------------+-----------+-------------+----------------+
4 rows in set (0.00 sec)
# 另外,还可以使用desc语句和show create table tb_name;语句来查询列信息,这里不再赘述,请自行尝试
desc db_name.tb_name column_name;
desc db_name.tb_name;
show create table tb_name;
PS:查询某个表所有的字段的可授予权限,除了直接查询columns表之外,还可以使用show full columns语句,如下(Privileges列内容展示了对应的字段所有的可授予权限)
root@localhost : information_schema 09:29:07> show full columns from sbtest.sbtest1;
+-------+------------------+-----------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------+------------------+-----------+------+-----+---------+----------------+---------------------------------+---------+
| id | int(10) unsigned | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| k | int(10) unsigned | NULL | NO | MUL | 0 | | select,insert,update,references | |
| c | char(120) | utf8_bin | NO | MUL | | | select,insert,update,references | |
| pad | char(60) | utf8_bin | NO | | | | select,insert,update,references | |
+-------+------------------+-----------+------+-----+---------+----------------+---------------------------------+---------+
4 rows in set (0.00 sec)
该表提供查询哪些索引列存在约束条件
下面是该表中存储的信息内容
admin@localhost : information_schema 05:52:31> select * from KEY_COLUMN_USAGE where TABLE_NAME='sbtest1' limit 1\G;
*************************** 1. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: sbtest
CONSTRAINT_NAME: PRIMARY
TABLE_CATALOG: def
TABLE_SCHEMA: sbtest
TABLE_NAME: sbtest1
COLUMN_NAME: id
ORDINAL_POSITION: 1
POSITION_IN_UNIQUE_CONSTRAINT: NULL
REFERENCED_TABLE_SCHEMA: NULL
REFERENCED_TABLE_NAME: NULL
REFERENCED_COLUMN_NAME: NULL
1 row in set (0.00 sec)
字段含义如下:
如果约束是外键的,则该表中查询到的就是外键列(外键索引列),但没有记录引用关联关系。假设有两个表名为t1和t3,其定义如下:
# 父表(主表)
CREATE TABLE t1
(
s1 INT,
s2 INT,
s3 INT,
PRIMARY KEY(s3)
) ENGINE=InnoDB;
# 子表(从表)
CREATE TABLE t3
(
s1 INT,
s2 INT,
s3 INT,
KEY(s1),
CONSTRAINT CO FOREIGN KEY (s2) REFERENCES t1(s3)
) ENGINE=InnoDB;
# 对于这两个表,KEY_COLUMN_USAGE表中会记录如下两行约束信息,其中t1表的约束是主键,t3表的约束是外键:
CONSTRAINT_NAME ='PRIMARY',TABLE_NAME ='t1',COLUMN_NAME ='s3',ORDINAL_POSITION = 1,POSITION_IN_UNIQUE_CONSTRAINT = NULL
CONSTRAINT_NAME ='CO',TABLE_NAME ='t3',COLUMN_NAME ='s2',ORDINAL_POSITION = 1,POSITION_IN_UNIQUE_CONSTRAINT = 1
该表提供查询关于外键约束的一些信息
下面是该表中存储的信息内容
# 使用MySQL样例数据库
root@localhost : information_schema 05:31:15> select * from REFERENTIAL_CONSTRAINTS where CONSTRAINT_SCHEMA='employees' limit 1\G;
*************************** 1. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: employees
CONSTRAINT_NAME: dept_emp_ibfk_1
UNIQUE_CONSTRAINT_CATALOG: def
UNIQUE_CONSTRAINT_SCHEMA: employees
UNIQUE_CONSTRAINT_NAME: PRIMARY
MATCH_OPTION: NONE
UPDATE_RULE: RESTRICT
DELETE_RULE: CASCADE
TABLE_NAME: dept_emp
REFERENCED_TABLE_NAME: employees
1 row in set (0.00 sec)
字段含义如下:
该表提供查询关于索引的一些统计信息,一个索引列对应一行记录
下面是该表中存储的信息内容
admin@localhost : information_schema 06:15:14> select * from STATISTICS where TABLE_NAME='sbtest1' limit 1\G;
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: sbtest
TABLE_NAME: sbtest1
NON_UNIQUE: 0
INDEX_SCHEMA: sbtest
INDEX_NAME: PRIMARY
SEQ_IN_INDEX: 1
COLUMN_NAME: id
COLLATION: A
CARDINALITY: 4392231
SUB_PART: NULL
PACKED: NULL
NULLABLE:
INDEX_TYPE: BTREE
COMMENT:
INDEX_COMMENT:
1 row in set (0.00 sec)
字段含义如下(部分字段)
PS:该表中的信息还可以使用show语句查询
# show index有两种方法,两种方法差别不大,如下
root@localhost : information_schema 07:04:47> show index from sbtest1 from sbtest;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| sbtest1 | 0 | PRIMARY | 1 | id | A | 7522488 | NULL | NULL | | BTREE | | |
| sbtest1 | 1 | k_1 | 1 | k | A | 713252 | NULL | NULL | | BTREE | | |
| sbtest1 | 1 | i_c | 1 | c | A | 4176349 | NULL | NULL | | BTREE | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
root@localhost : information_schema 07:04:29> show index from sbtest.sbtest1;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| sbtest1 | 0 | PRIMARY | 1 | id | A | 7522488 | NULL | NULL | | BTREE | | |
| sbtest1 | 1 | k_1 | 1 | k | A | 713252 | NULL | NULL | | BTREE | | |
| sbtest1 | 1 | i_c | 1 | c | A | 4176349 | NULL | NULL | | BTREE | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
该表提供查询表相关的约束信息
下面是该表中存储的信息内容
admin@localhost : information_schema 06:19:37> select * from TABLE_CONSTRAINTS where TABLE_NAME='sbtest1';
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA | TABLE_NAME | CONSTRAINT_TYPE |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
| def | sbtest | PRIMARY | sbtest | sbtest1 | PRIMARY KEY |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
1 row in set (0.00 sec)
字段含义如下:
该表提供查询MySQL的数据表空间文件相关的信息,包含InnoDB存储引擎和NDB存储引擎相关的数据文件信息,由于NDB存储引擎在国内较少使用,我们大多数场景(95%以上场景InnoDB存储引擎都可以使用)都是使用InnoDB存储引擎,以下我们只针对InnoDB存储引擎做介绍
下面是该表中存储的信息内容
# 表完整行记录数据
admin@localhost : information_schema 05:44:39> select * from FILES limit 1\G;
*************************** 1. row ***************************
FILE_ID: 0
FILE_NAME: /home/mysql/data/mysqldata1/innodb_ts/ibdata1
FILE_TYPE: TABLESPACE
TABLESPACE_NAME: innodb_system
TABLE_CATALOG:
TABLE_SCHEMA: NULL
TABLE_NAME: NULL
LOGFILE_GROUP_NAME: NULL
LOGFILE_GROUP_NUMBER: NULL
ENGINE: InnoDB
FULLTEXT_KEYS: NULL
DELETED_ROWS: NULL
UPDATE_COUNT: NULL
FREE_EXTENTS: 3
TOTAL_EXTENTS: 2048
EXTENT_SIZE: 1048576
INITIAL_SIZE: 2147483648
MAXIMUM_SIZE: NULL
AUTOEXTEND_SIZE: 67108864
CREATION_TIME: NULL
LAST_UPDATE_TIME: NULL
LAST_ACCESS_TIME: NULL
RECOVER_TIME: NULL
TRANSACTION_COUNTER: NULL
VERSION: NULL
ROW_FORMAT: NULL
TABLE_ROWS: NULL
AVG_ROW_LENGTH: NULL
DATA_LENGTH: NULL
MAX_DATA_LENGTH: NULL
INDEX_LENGTH: NULL
DATA_FREE: 2105540608
CREATE_TIME: NULL
UPDATE_TIME: NULL
CHECK_TIME: NULL
CHECKSUM: NULL
STATUS: NORMAL
EXTRA: NULL
1 row in set (0.00 sec)
# 该表字段较多,通常,我们可能只需要查询一部分字段即可,类似如下:
root@localhost : information_schema 12:22:27> SELECT FILE_ID, FILE_NAME, FILE_TYPE, TABLESPACE_NAME, FREE_EXTENTS, TOTAL_EXTENTS, EXTENT_SIZE, INITIAL_SIZE, MAXIMUM_SIZE, AUTOEXTEND_SIZE, DATA_FREE, STATUS ENGINE FROM INFORMATION_SCHEMA.FILES \G
字段含义如下(该表中所有字段都为 "MySQL extension" 列)
PS:
注意:该表中的数据没有对应的show语句查询方式
该表提供查询MySQL Server支持的引擎相关的信息
下面是该表中存储的信息内容
admin@localhost : information_schema 05:43:13> select * from ENGINES;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ENGINE | SUPPORT | COMMENT | TRANSACTIONS | XA | SAVEPOINTS |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| CSV | YES | CSV storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.01 sec)
字段含义如下(该表中所有字段都为 "MySQL extension" 列)
PS:该表中的内容还可以通过show语句查询
# 语法
SHOW [STORAGE] ENGINES
# 示例
root@localhost : information_schema 10:21:05> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
该表提供查询关于活跃表空间的相关信息(主要记录的是NDB存储引擎表空间信息)
下面是该表中存储的信息内容
# 暂无,需要创建NDB存储引擎表
字段含义如下(该表中的所有字段为 "MySQL extension" 列)
该表提供查询MySQL Server中的数据库列表信息,一个schema就代表一个database
下面是该表中存储的信息内容
root@localhost : information_schema 06:35:32> select * from schemata;
+--------------+--------------------+----------------------------+------------------------+----------+
| CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |
+--------------+--------------------+----------------------------+------------------------+----------+
| def | information_schema | utf8 | utf8_general_ci | NULL |
| def | employees | utf8 | utf8_bin | NULL |
| def | luoxiaobo | utf8 | utf8_bin | NULL |
| def | mysql | utf8 | utf8_bin | NULL |
| def | performance_schema | utf8 | utf8_general_ci | NULL |
| def | qfsys | utf8 | utf8_bin | NULL |
| def | sbtest | utf8 | utf8_bin | NULL |
| def | sys | utf8 | utf8_general_ci | NULL |
| def | test | utf8 | utf8_bin | NULL |
+--------------+--------------------+----------------------------+------------------------+----------+
9 rows in set (0.00 sec)
字段含义如下:
PS:该表中的信息还可以使用show语句查询
root@localhost : information_schema 06:41:30> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| employees |
| luoxiaobo |
| mysql |
| performance_schema |
| qfsys |
| sbtest |
| sys |
| test |
+--------------------+
9 rows in set (0.00 sec)
本期内容就介绍到这里,本期内容参考链接如下:
| 作者简介
《千金良方——MySQL性能优化金字塔法则》、《数据生态:MySQL复制技术与生产实践》作者之一。熟悉MySQL体系结构,擅长数据库的整体调优,喜好专研开源技术,并热衷于开源技术的推广,在线上线下做过多次公开的数据库专题分享,发表过近100篇数据库相关的研究文章。
全文完。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有