51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

mysql 统计所有表的数据量

需求:

数据库迁移后, 需要进行数据校验;

专业的工具有: mysqldbcompare pt-table-checksum

但是我们环境是RDS 生产环境,不允许外网直接访问,内网也没有权限去访问,就是这么变态,

无奈只好进行粗粒度的检测, 统计所有表的行数。

在mysql里是可以查询information_schema.tables这张表的,然后获取我们想要的信息:

SELECT table_rows,table_name FROM information_schema.tables  
WHERE TABLE_SCHEMA = 'mysql'
and table_name not in ('db','func')
ORDER BY table_rows DESC;

SELECT table_rows,table_name FROM information_schema.tables  

 WHERE TABLE_SCHEMA = 'kg_audio' 

 and table_name not in ('db','func') 

 ORDER BY table_rows DESC;

结果如下:

wps22


如果想要总得数据量:

要统计的,加上sum函数就可以

SELECT SUM(table_rows)FROM information_schema.tables

WHERE TABLE_SCHEMA = 'mysql'


and table_name not in ('db','func')

`ORDER BY table_rows DESC;`

结果如下图:

wps23


附件:

如果想吧表的信息 导出到EXCEL 中怎办?

上程序:

import pymysql
import xlwt
#python连接mysql获取表信息(表名、字段数)
数据库信息
config = {
'host': '192.168.88.100',
'port': 3304,
'user': 'root',
'password': '123456',
'database': 'demo',
'charset': 'utf8mb4',
'cursorclass': pymysql.cursors.Cursor,
}
db= 'label'
excel的存放目录
dir = "data"
写入EXCEL
def write_to_excel(res, sheet_name, excel_name):
'''
:param res: 要写入excel的数据
:param sheet_name: sheet页名称
:param excel_name: excel名称
'''
workbook = xlwt.Workbook()
sheet = workbook.add_sheet(sheet_name, cell_overwrite_ok=True)
row = 0
for i in res:
for j in range(0, len(i)):
sheet.write(row, j, i[j])
row += 1
workbook.save(excel_name)
当前库下所有表的名称
def tables_name(db):
res = mysql_info("select table_name from information_schema.tables where table_schema='%s'" % (db))
print('当前库下所有表的名称')
for i in res:
print(i[0])
return res
每个表的记录行数
def count_rows(db):
res = mysql_info("select table_name,table_rows from information_schema.tables where TABLE_SCHEMA = '%s'" % (db))
print('每个表的记录行数')
print(res)
return res
def mysql_info(sql):
"""
:param sql: 执行的查询sql语句
:return: 返回查询结果
如果程中发生错误直接报错退出
"""
try:
# conn1 = pymysql.connect(host, user, passwd, db)
conn1 = pymysql.connect(**config)
cursor1 = conn1.cursor()
cursor1.execute(sql)
res = cursor1.fetchall()
conn1.close()
return res
except Exception as e:
print(e)
print("!!!!!!!!!!!!!!请检查数据库连接信息!!!!!!!!!!!!!!")
exit(-1)
if name == "main":
result1 = tables_name(db)
write_to_excel(result1, 'tables_name', dir + '/%s库中每个表的名字.xlsx' % db)
result2 = count_rows(db)
write_to_excel(result2, 'count_rows', dir + '/%s库中每个表的记录行数.xlsx' % db)


赞(1)
未经允许不得转载:工具盒子 » mysql 统计所有表的数据量