#创建基表
CREATE TABLE `table_sum` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`table_name` varchar(50) DEFAULT NULL,
`table_rows` int(11) DEFAULT NULL,
`total_size` int(11) DEFAULT NULL,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#创建统计表
CREATE TABLE `table_day_growth` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`table_name` varchar(50) DEFAULT NULL,
`table_rows` int(11) DEFAULT NULL,
`total_size` int(11) DEFAULT NULL,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#每天向基表里插入数据
cat insert_table_bak.sh
#!/bin/bash
source /etc/profile
mysql_login='/usr/local/mysql/bin/mysql'
$mysql_login -uroot -pxxx -h10.10.88.168 -P3306 -BNe "SELECT CONCAT(table_schema, '.', table_name) table_name, table_rows,CONCAT(ROUND(ROUND(data_length + index_length) / (1024 * 1024))) total_size FROM information_schema.tables WHERE table_schema NOT IN ('information_schema' , 'performance_schema', 'sys', 'mysql','zabbix','test') and table_rows >=1000;" > /tmp/table_sum.txt
$mysql_login -e "load data infile '/tmp/table_sum.txt' ignore into table test.table_sum character set utf8 lines terminated by '\n' (table_name,table_rows,total_size);"
/bin/rm -rf /tmp/table_sum.txt
#统计基表里当天的数据
#!/bin/bash
mysql_login='/usr/local/mysql/bin/mysql'
$mysql_login -NBe "select table_rows from test.table_sum where create_time>=date_format(date_sub(now(),interval 1 day),'%Y-%m-%d %H:%i:00');" > s1
$mysql_login -NBe "select table_rows from test.table_sum where create_time<=date_format(date_sub(now(),interval 1 day),'%Y-%m-%d %H:%i:00') and create_time>=date_format(date_sub(now(),interval 2 day),'%Y-%m-%d %H:%i:00');" > s2
$mysql_login -NBe "select total_size from test.table_sum where create_time>=date_format(date_sub(now(),interval 1 day),'%Y-%m-%d %H:%i:00');" > s3
$mysql_login -NBe "select total_size from test.table_sum where create_time<=date_format(date_sub(now(),interval 1 day),'%Y-%m-%d %H:%i:00') and create_time>=date_format(date_sub(now(),interval 2 day),'%Y-%m-%d %H:%i:00');" > s4
$mysql_login -NBe "select table_name from test.table_sum where create_time>=date_format(date_sub(now(),interval 1 day),'%Y-%m-%d %H:%i:00') ;" > s10
n=1
for i in `cat s1`;do # 循环s1
num=`sed -n "${n}p" s2` # 获取s2对行的数字
minus=$(($i - $num)) # 两数相减
echo $minus >> s5 # 打印结果
n=$(($n + 1))
done
n=1
for i in `cat s3`;do # 循环s3
num=`sed -n "${n}p" s4` # 获取s4对行的数字
minus=$(($i - $num)) # 两数相减
echo $minus >> s6 # 打印结果
n=$(($n + 1))
done
/root/dba/calculate_table.py
/bin/rm -rf s1 s2 s3 s4 s5 s6 s10 table_info.txt
数据合并脚本
cat calculate_table.py
#!/usr/bin/env python
# coding:utf-8
import sys,os,MySQLdb
file1 = open("s5", "rb")
file2 = open("s6", "rb")
file3 = open("s10", "rb")
file_list1 = file1.readlines() # 将所有变量读入列表file_list1
file_list2 = file2.readlines() # 将所有变量读入列表file_list2
file_list3 = file3.readlines() # 将所有变量读入列表file_list2
# print(type(file1))
# 定义各属性数据存储列表
file_list1_name = []
file_list2_name = []
file_list3_name = []
# 遍历file_list1 列表 将得到的信息进行下列操作
for message in file_list1:
temp_list = message.split()
# 将txt文件中的第一行 也就是file_list1 列表的第一项 用split方法操作 以空格为分隔符 分成两部分继续放到temp_list列表里
file_list1_name.append(str(temp_list[0]))
# 操作与file_list1列表完全相同
for message in file_list2:
temp_list = message.split()
file_list2_name.append(str(temp_list[0]))
# 操作与file_list1列表完全相同
for message in file_list3:
temp_list = message.split()
file_list3_name.append(str(temp_list[0]))
file_list4 = []
for i in range(len(file_list3_name)):
s = ''
s = '\t'.join([file_list3_name[i], file_list1_name[i], file_list2_name[i]])
s += '\n'
file_list4.append(s)
# 将数据写入file4
file4 = open("table_info.txt", "w")
file4.writelines(file_list4)
# 关闭文件
file1.close()
file2.close()
file3.close()
file4.close()
#table_info = open("/root/dba/table_info.txt", mode="r")
table_info = open("table_info.txt", mode="r")
for info in table_info.readlines():
info_array = info.split()
arg1 = info_array[0]
arg2 = info_array[1]
arg3 = info_array[2]
# 打开数据库连接
conn = MySQLdb.connect(host='10.10.88.18', port=3306, user='root', passwd='123456', db='test')
# 使用cursor()方法获取操作游标
cursor = conn.cursor()
# SQL 插入语句
sql = "INSERT INTO table_day_growth(`table_name`,`table_rows`,`total_size`) values('%s','%s','%s')" % (arg1, arg2, arg3)
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
except Exception as error:
print(error)
# 发生错误时回滚
conn.rollback()
# 关闭数据库连接
conn.close()
table_info.close()
- mysql 统计按天、星期、按月数据的各种 sql 语句 (转录)
文章主要是作为知识整理,内容略有修改,方便以后查阅,内容转摘至 陈宇衡的个人博客,欢迎前去围观. 作为演示效果,先创建一个测试表,在插入两条数据(注:时间为 datetime 类型,unix 时间戳需 ...
- python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图
python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图 # coding=utf-8 from openpyxl import load_workbook ...
- Mysql统计每年每个月的数据——详细教程
Mysql统计每年每个月的数据(前端页面统计图实现) 最终想实现的效果图,在这里就不多废话了,直接上效果图,由于测试数据有几个月是为0的,所以数据图看着会有点怪怪. 接下来是数据库的两个表,这里直接给 ...
- MySQL 分区表原理及数据备份转移实战
MySQL 分区表原理及数据备份转移实战 1.分区表含义 分区表定义指根据可以设置为任意大小的规则,跨文件系统分配单个表的多个部分.实际上,表的不同部分在不同的位置被存储为单独的表.用户所选择的.实现 ...
- 网站统计中的数据收集原理及实现(share)
转载自:http://blog.codinglabs.org/articles/how-web-analytics-data-collection-system-work.html 网站数据统计分析工 ...
- 使用nginx lua实现网站统计中的数据收集
导读网站数据统计分析工具是各网站站长和运营人员经常使用的一种工具,常用的有 谷歌分析.百度统计和腾讯分析等等.所有这些统计分析工具的第一步都是网站访问数据的收集.目前主流的数据收集方式基本都是基于ja ...
- Excel连接到MySQL,将Excel数据导入MySql,MySQL for Excel,,
Excel连接到MySQL 即使当今时代我们拥有了类似微软水晶报表之类的强大报表工具和其他一些灵活的客户管 理应用工具,众多企业在分析诸如销售统计和收入信息的时候,微软的Excel依然是最常用的工具. ...
- TiDB 作为 MySQL Slave 实现实时数据同步
由于 TiDB 本身兼容绝大多数的 MySQL 语法,所以对于绝大多数业务来说,最安全的切换数据库方式就是将 TiDB 作为现有数据库的从库接在主 MySQL 库的后方,这样对业务方实现完全没有侵入性 ...
- 【mysql】mysql统计查询count的效率优化问题
mysql统计查询count的效率优化问题 涉及到一个问题 就是 mysql的二级索引的问题,聚簇索引和非聚簇索引 引申地址:https://www.cnblogs.com/sxdcgaq8080/p ...
随机推荐
- python yield && scrapy yield
title: python yield && scrapy yield date: 2020-03-17 16:00:00 categories: python tags: 语法 yi ...
- leetcode 122 123 309 188 714 股票买卖 动态规划
这类问题有一个通法 https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/solution/yi-ge-tong-y ...
- 开放式 Web 应用程序安全性项目 OWASP
开放式 Web 应用程序安全性项目 OWASP Open Web Application Security Project (OWASP) OWASP 基金会是谁? Open Web Applicat ...
- Flutter 1.5
Flutter 1.5 Flutter SDK https://flutter.dev/docs/get-started/install/windows Android SDK This instal ...
- Swift in Action
Swift in Action Swift Playgrounds https://apps.apple.com/us/app/swift-playgrounds/id1496833156?mt=12 ...
- idle & js
idle & js idle meaning in js https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensi ...
- 关于TCP的Total Length
TCP/IP传输层 文档 随便找了个发送的TCP: 70 89 cc ee 84 2c 3c 2c 30 a6 a2 d0 08 00 45 00 00 4c c7 a8 40 00 80 06 00 ...
- Dart: puppeteer库
和node的差不多,只有写API不一样 puppeteer 地址 安装依赖 dependencies: puppeteer: ^1.7.1 下载 chrome-win 到 <project_ro ...
- NGK公链如何构建区块链数字经济商业帝国?
2020年对于区块链市场来说,重大的利好消息莫过于NGK公链的上线了.NGK公链其广泛的市场前景.顶尖的技术,一直备受众多大型机构以及投资者所看好.同时,NGK公链也不负众望,在上线以后,就开始落地到 ...
- NGK全球巡回路演莫斯科站,共探BGV能否超越YFI?
近日,NGK全球巡回路演在俄罗斯首都莫斯科落下帷幕.此次路演取得了空前的成功.路演伊始俄罗斯路演讲师Andrew致开幕辞,安德鲁称,俄罗斯作为未一个幅员辽阔的大国,区块链技术有着非常大的应用场景. 俄 ...