MySQL - -【mysql 使用insert-select快速造出大量测试数据】【查看mysql数据库容量】

目录

  • mysql 使用insert-select快速造出大量测试数据
  • 查看mysql数据库容量

1 mysql 使用insert-select快速造出大量测试数据

insert-select的原理是将表中数据查出后,再次插入数据库,这样数据就会成倍增长。
  • use 库建表
CREATE TABLE `review` (
`review_id` INT(11) NOT NULL AUTO_INCREMENT,
`movie_id` INT(11) NOT NULL,
`content` VARCHAR(1024) NOT NULL,
PRIMARY KEY (`review_id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
  • 首先使用如下语句插入10条数据,主键任其自增
insert into review(movie_id,content) values(1,now());
  • 然后使用insert-select方法成倍插入数据
insert into review(movie_id,content) select movie_id,content from review;
  • 查看review表数据行数
select count(*) from review;

2 查看mysql数据库容量

  • 查询所有数据库的总大小
use information_schema;
select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES;
  • 统计一下所有库数据量
# 每张表数据量=AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH
SELECT
SUM(AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024/1024 AS total_mb
FROM information_schema.TABLES;
  • 统计每个库大小:
SELECT
table_schema,SUM(AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024/1024 AS total_mb
FROM information_schema.TABLES group by table_schema;
  • 查看指定数据库的大小,比如说:数据库test
use information_schema;
select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES where table_schema='test';
  • 查看所有数据库各容量大小
select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
group by table_schema
order by sum(data_length) desc, sum(index_length) desc;
  • 查看所有数据库各表容量大小
select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
order by data_length desc, index_length desc;
  • 查看指定数据库容量大小
# 例:查看mysql库容量大小
select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
where table_schema='mysql'; 
  • 查看指定数据库各表容量大小
例:查看mysql库各表容量大小
select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
where table_schema='mysql'
order by data_length desc, index_length desc;

参考文档:

posted @ 2021-02-04 10:04  小Q渺晓  阅读(865)  评论(0编辑  收藏  举报