Python程序文件如下:

# -*- coding: utf-8
# File : start.py
# Author : baoshan
import json
import pymysql
import cx_Oracle
import pandas as pd def main():
dataSum = []
connInfo = "connInfo.json" # 配置文件名称 connFile = open(connInfo, 'r', encoding='utf8')
connRecords = connFile.read(102400) #一次读取多个字节
connRecordsjs = json.loads(connRecords)
for single in connRecordsjs:
if "mysql" == single.get("dbtype"):
conn = pymysql.connect(host=single.get("host"), port=single.get("port"), user=single.get("user"),
passwd=single.get("passwd"), charset=single.get("charset"))
if "gongxiangwangzhan" == single.get("source", ""): # 共享网站 公安局、民政局、聊城市发展和改革委员会 定制
sql = "select table_schema as '数据库', " \
"table_name as '数据表', " \
"TABLE_COMMENT as '表注释', " \
"round(data_length/1024/1024,2) as '数据大小(M)', " \
"round(index_length/1024/1024,2) as '索引大小(M)', " \
"TABLE_ROWS as '行数' " \
"from information_schema.tables " \
"where TABLE_SCHEMA in ('"+single.get("dbschema")+"') " \
"AND TABLE_ROWS > 0 " \
"and table_name in "+single.get("selectkeystr")+""
else:
sql = "select " \
"table_schema as '数据库'," \
"table_name as '数据表', " \
"TABLE_COMMENT as '表注释', " \
"round(data_length/1024/1024,2) as '数据大小(M)', " \
"round(index_length/1024/1024,2) as '索引大小(M)', " \
"TABLE_ROWS as '行数'" \
"from information_schema.tables " \
"where TABLE_SCHEMA in ('"+single.get("dbschema")+"') " \
"and (table_name "+single.get("selectstr")+" '"+single.get("selectkeystr")+"') " \
"and TABLE_ROWS > 0"
df = pd.read_sql(sql, conn)
print(single.get("key"), str(df['行数'].sum()))
dataSum.append(df['行数'].sum())
conn.close()
elif "oracle" == single.get("dbtype"):
if "table" == single.get("selecttype"):
sql = "select owner as owner," \
"table_name as table_name," \
"tablespace_name as tablespace_name, " \
"num_rows as num_rows " \
"from all_tables " \
"where num_rows > 0 " \
"and table_name like '"+single.get("selectkeystr")+"' " \
"order by num_rows desc "
elif "database" == single.get("selecttype"): # 共享网站-oracle-工商局 定制
sql = "select owner as owner, " \
"table_name as table_name, " \
"tablespace_name as tablespace_name, " \
"num_rows as num_rows " \
"from all_tables " \
"where num_rows > 0 " \
"and tablespace_name in('"+single.get("dbschema")+"') " \
"order by num_rows desc"
db = cx_Oracle.connect(single.get("connstr"), encoding='utf-8')
cursor = db.cursor()
cursor.execute(sql)
rs = cursor.fetchall()
df = pd.DataFrame(rs)
print(single.get("key"), str(df[3].sum()))
dataSum.append(df[3].sum())
cursor.close()
db.close()
elif "sqlserver" == single.get("dbtype"):
print(single.get("key"), '')
dataSum.append(55568045)
# "SELECT A.NAME ,B.ROWS FROM sysobjects A JOIN sysindexes B ON A.id = B.id WHERE A.xtype = 'U' AND B.indid IN(0,1) and b.rows >0 ORDER BY B.ROWS DESC"
else:
print("please give right database type.")
connFile.close()
print('-'*30)
print("数据量总计:", str(sum(dataSum))) if __name__ == '__main__':
print("***一次性统计所有对接数据的委办局,和其对应的数据(条数)***")
main()

所需要的配置文件格式如下:

[
{
"key": "智慧公交",
"dbtype": "oracle",
"connstr": "nicai/123456@10.10.10.10:1521/ORCL",
"selecttype": "table",
"selectstr": "like",
"selectkeystr": "BUS%"
},
{
"key": "公共自行车",
"dbtype": "oracle",
"connstr": "nicai/123456@10.10.10.10:1521/ORCL",
"selecttype": "table",
"selectstr": "like",
"selectkeystr": "BICYCLE%"
},
{
"key": "安监局",
"dbtype": "mysql",
"host": "10.10.10.10",
"port": 3306,
"user": "nicai",
"passwd": "",
"charset": "utf8",
"selecttype": "table",
"selectstr": "like",
"dbschema": "statistics_data",
"selectkeystr": "ajj%"
},
{
"key": "百度交通",
"dbtype": "mysql",
"host": "10.10.10.2",
"port": 3306,
"user": "nicai",
"passwd": "",
"charset": "utf8",
"selecttype": "table",
"selectstr": "like",
"dbschema": "statistics_data",
"selectkeystr": "bdu%"
}
]

关于SqlServer的数据量查询,由于当时连不上,就没有嵌入到这个程序中。

不过查询的方法已经列出。

精进自己,分享他人!

谢谢

Python统计数据库中的数据量【含MySQL、Oracle】的更多相关文章

  1. 使用Python将Excel中的数据导入到MySQL

    使用Python将Excel中的数据导入到MySQL 工具 Python 2.7 xlrd MySQLdb 安装 Python 对于不同的系统安装方式不同,Windows平台有exe安装包,Ubunt ...

  2. spark查看DF的partition数目及每个partition中的数据量【集群模式】

    println("--------------------"+data.rdd.getNumPartitions) // 获取DF中partition的数目 val partiti ...

  3. Sql Server中的数据类型和Mysql中的数据类型的对应关系(转)

    Sql Server中的数据类型和Mysql中的数据类型的对应关系(转):https://blog.csdn.net/lilong329329/article/details/78899477 一.S ...

  4. 大数据量时Mysql的优化

    (转自网络) 如今随着互联网的发展,数据的量级也是撑指数的增长,从GB到TB到PB.对数据的各种操作也是愈加的困难,传统的关系性数据库已经无法满足快速查询与插入数据的需求.这个时候NoSQL的出现暂时 ...

  5. requests从api中获取数据并存放到mysql中

    python的requests库是一个非常强大的库,requests的安装方法十分简单,用: pip install requests 即可安装requests,安装成功后: import reque ...

  6. 用JDBC把Excel中的数据导入到Mysql数据库中

    步骤:0.在Mysql数据库中先建好table 1.从Excel表格读数据 2.用JDBC连接Mysql数据库 3.把读出的数据导入到Mysql数据库的相应表中 其中,步骤0的table我是先在Mys ...

  7. Scrapy基础(十)———同步机制将Item中的数据写在Mysql

      前面讲解到将Item中的所有字段都已经填写完成,那么接下来就是将他们存储到mysql数据库中,那就用到了pipeline项目管道了:  对项目管道的理解:做一个比喻,爬取好比是开采石油,Item装 ...

  8. 表数据量影响MySQL索引选择

    现象 新建了一张员工表,插入了少量数据,索引中所有的字段均在where条件出现时,正确走到了idx_nap索引,但是where出现部分自左开始的索引时,却进行全表扫描,与MySQL官方所说的最左匹配原 ...

  9. 大数据量时 Mysql LIMIT如何正确对其进行优化(转载)

    以下的文章主要是对Mysql LIMIT简单介绍,我们大家都知道LIMIT子句一般是用来限制SELECT语句返回的实际行数.LIMIT取1个或是2个数字参数,如果给定的是2个参数,第一个指定要返回的第 ...

随机推荐

  1. 【转】学习ARM为什么首选IMAX6??

    ARM作为目前嵌入式行业主流的架构,已经让越来越多从事电子行业的朋友了解,并且高校对于嵌入式的学习,很多直接从ARM开始,目前ARM的嵌入式培训也越来越多,足以说明现在嵌入式行业有多火.目前主流的AR ...

  2. 解决Mac OS X 系统在home文件夹下面操作不支持的方法

    解决Mac OS X 系统在home文件夹下面操作不支持的方法   最近需要使用Mac OS X 系统尝试安装使用appium程序,安装过程中发现,Mac OS X 系统在home文件夹下面操作不支持 ...

  3. ansible之REPLACE模块

    > REPLACE (/usr/lib/python2.7/site-packages/ansible/modules/files/replace.py) This module will re ...

  4. 漏洞利用 Exploit---利用默认口令、IP假冒、应用漏洞

    漏洞利用 编辑 讨论 本词条由“科普中国”科学百科词条编写与应用工作项目 审核 . 漏洞利用(英语:Exploit,本意为“利用”)是计算机安全术语,指的是利用程序中的某些漏洞,来得到计算机的控制权( ...

  5. reactNative 获取组件高、宽、位置等信息

    import {findNodeHandle, UIManager} from 'react-native' layout(ref) { const handle = findNodeHandle(r ...

  6. accept返回的socket的端口号和连接socket一样的!!! socket绑定信息结构

    今天与同学争执一个话题:由于socket的accept函数在有客户端连接的时候产生了新的socket用于服务该客户端,那么,这个新的socket到底有没有占用一个新的端口? 讨论完后,才发现,自己虽然 ...

  7. POJ1321-棋盘问题-(dfs)

    http://poj.org/problem?id=1321 解题: dfs中,两种情况,某一行摆不摆?某一列摆不摆? #include<stdio.h> #include<iost ...

  8. [ARC064F] Rotated Palindromes

    题意 给定一个整数N,请你求出有多少字符集为1到K之间整数的字符串,使得该字符串可以由一个长度为N的回文串循环移位后得到.所谓循环移位,就是把字符串的某个前缀(可以为空)移到字符串末尾,如" ...

  9. Mysql8.0 创建远程登陆账户

    mysql8和原来的版本有点不一样,8的安全级别更高,所以在创建远程连接用户的时候, 不能用原来的命令(同时创建用户和赋权): mysql>grant all PRIVILEGES on *.* ...

  10. ES5新增的数组方法

    ES5新增:(IE9级以上支持)1.forEach():遍历数组,无返回值,不改变原数组.2.map():遍历数组,返回一个新数组,不改变原数组.3.filter():过滤掉数组中不满足条件的值,返回 ...