元数据的获取和应用

第1章 元数据

1.1 什么是元数据

1. 元数据是存储在"基表"中。
2. 通过专用的DDL语句,DCL语句进行修改
3. 通过专用视图和命令进行元数据的查询
4. information_schema中保存了大量元数据查询的视图
5. show 命令是封装好功能,提供元数据查询基础功能

1.2 information_schema的基本应用

1.2.1 查看视图

--查看视图
use information_schema;
show tables;
--注意:information_schema 是一个虚拟库 不占内存

1.2.2 常用的视图

1. table_schema              表所在的库名
2. table_name			    表名
3. engine			        存储引擎
4. table_rows			    数据行
5. AVG_ROW_LENGTH		    平均行长度
6. INDEX_LENGTH              索引长度

1.3 示例

例子1: 查看整个数据库所有的库所有的表

select table_schema,table_name from information_schema.tables;

例子2 :显示world库下所有的表

select table_schema,table_name from information_schema.tables where table_schema='world';

例子3:显示所有innodb引擎的表

select table_schema,table_name from information_schema.tables where engine='innodb';

例子4 :显示world库下city表有多少行

select table_schema,table_name,table_rows from information_schema.tables where table_name='city';

例子5:统计world库下city的占用空间

--说明单表的数据量真实大小=平均行长度*行数+索引长度
select table_name,(avg_row_length*table_rows+index_length)/1024
from information_schema.tables
where table_schema='world' and table_name='city';

例子5:统计world库数据量总大小

select table_schema,sum((avg_row_length*table_rows+index_length)/1024/1024)
from information_schema.tables
where table_schema='world';

例子6:统计每个库的数据量大小,并按数据量从大到小排序

select table_schema,sum((avg_row_length*table_rows+index_length)/1024/1024)
from information_schema.tables
group by table_schema
order by sum((avg_row_length*table_rows+index_length)/1024/1024)
desc;

第2章 配合concat( )函数拼接语句或命令

1.1 模仿以下语句,进行数据库的分库分表备份(现在流行的备份方式)

mysqldump -uroot -p123 world city >/backup/world_city.sql
===================================================================
SELECT CONCAT("mysqldump –uroot –p123 ",table_schema," ",table_name," >/backup/",table_schema,"_",table_name,".sql")
 FROM information_schema.tables into outfile '/backup/bak.sh'; 
--说明:此处的备份思路就是把每个表都进行备份,到时候哪个表有问题,直接执行就可以恢复任意一张表了

1.2 模仿以下语句,进行批量生成对world库下所有表进行操作

alter table world.city discard tablespace;
--说明:把新建表空间删除掉(数据库专用命令)
SELECT CONCAT("alter table",TABLE_SCHEMA,".",TABLE-NAME"discard tablespace")
FROM information_schema.tables

1.3 columns (数据字典统计)就是统计表的列的信息

--例子统计school数据库下的表的数据字典信息
SELECT table_name,column_name ,data_type,column_comment
FROM information_schema.columns
WHERE table_schema='school';

猜你喜欢

转载自www.cnblogs.com/yufenchi/p/12961482.html