Mysql分表数据通过Python进行汇总统计

85 篇文章 1 订阅
43 篇文章 0 订阅


接上文:【Mysql分表数据通过Shell进行导出与统计】---> http://blog.csdn.net/babyfish13/article/details/52777780
1、每个分表数据量及总数据量统计
mysql_much_tab_data_static1.py

# -*- coding=utf-8 -*-
import smtplib
import MySQLdb
import warnings
import datetime
import os
import os.path
import shutil

warnings.filterwarnings("ignore")

db_config = {
    'host': 'mysql-server-ip',
    'user': 'mysql-server-user',
    'passwd': 'mysql-server-passwd',
    'port': 3306,
    'db': 'jellyfish_server'
}

def getDB():
    try:
        conn = MySQLdb.connect(host=db_config['host'],user=db_config['user'],passwd=db_config['passwd'],port=db_config['port'])
        conn.autocommit(True)
        curr = conn.cursor()
        curr.execute("SET NAMES utf8");
        curr.execute("USE %s" % db_config['db']);
    
        return conn, curr
    except MySQLdb.Error,e:
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])
        return None, None

conn,curr = getDB()

today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
tomorrow = today + datetime.timedelta(days=1)

print today
print yesterday 

sql_text = "select table_name from information_schema.tables where table_schema='jellyfish_server' and table_name like 'room_subscriber_roomid_%';"
curr.execute(sql_text)
tab_list = curr.fetchall()

cnt_sum = 0
for tab_name in tab_list:
    tab_name = tab_name[0]

    sql_text = "select count(*) from jellyfish_server.%s where substr(updated_time,1,10) ='%s' and state='0';" % (tab_name,yesterday)
    curr.execute(sql_text)
    cnts = curr.fetchall()
    for cnt in cnts:
        cnt = cnt[0]
        print  (tab_name + '\'s data cnt is : ' + str(cnt) )
        cnt_sum = cnt_sum + cnt
        print cnt_sum

curr.close()
conn.close()


2、各分表按关键字段分别汇总(相当于sql:group by sum count)
mysql_much_tab_data_static2.py

# -*- coding=utf-8 -*-
import smtplib
import MySQLdb
import warnings
import datetime
import os
import os.path
import shutil
from itertools import groupby
from operator import itemgetter

warnings.filterwarnings("ignore")

db_config = {
    'host': 'mysql-server-ip',
    'user': 'mysql-server-user',
    'passwd': 'mysql-server-passwd',
    'port': 3306,
    'db': 'jellyfish_server'
}

def getDB():
    try:
        conn = MySQLdb.connect(host=db_config['host'],user=db_config['user'],passwd=db_config['passwd'],port=db_config['port'])
        conn.autocommit(True)
        curr = conn.cursor()
        curr.execute("SET NAMES utf8");
        curr.execute("USE %s" % db_config['db']);
    
        return conn, curr
    except MySQLdb.Error,e:
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])
        return None, None

conn,curr = getDB()

today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
tomorrow = today + datetime.timedelta(days=1)

print today
print yesterday 

sql_text = "select table_name from information_schema.tables where table_schema='jellyfish_server' and table_name like 'room_subscriber_roomid_%';"
curr.execute(sql_text)
tab_list = curr.fetchall()

col_data_sum = 0
for tab_name in tab_list:
    tab_name = tab_name[0]

    sql_text = "select room_id,uid,state,created_time,updated_time from jellyfish_server.%s where substr(updated_time,1,10) ='%s' and state='0';" % (tab_name,yesterday)
    curr.execute(sql_text)
    col_datas = curr.fetchall()

    #col_datas.sort(key = itemgetter(0,2))
    #sorted(col_datas,key = itemgetter(0,2))
#    group_col_datas = groupby(col_datas,itemgetter(0,2))
    group_col_datas = groupby(sorted(col_datas,key = itemgetter(0,2)),itemgetter(0,2))   

    for key,item in group_col_datas:
        i=0
        j=0
        for jtem in item:
            i+=float(jtem[1])
            j+=1
        print key,i,j

'''
    for col_data in col_datas:
        room_id            = col_data[0] 
        uid                = col_data[1]
        state              = col_data[2]
        created_time       = col_data[3]
        updated_time       = col_data[4]
        print  (room_id + uid )
'''
curr.close()
conn.close()


3、各分表按关键字段总体汇总(相当于sql:group by sum count) 
mysql_much_tab_data_static3.py

# -*- coding=utf-8 -*-
import smtplib
import MySQLdb
import warnings
import datetime
import os
import os.path
import shutil
from itertools import groupby
from operator import itemgetter

warnings.filterwarnings("ignore")

db_config = {
    'host': 'mysql-server-ip',
    'user': 'mysql-server-user',
    'passwd': 'mysql-server-passwd',
    'port': 3306,
    'db': 'jellyfish_server'
}

def getDB():
    try:
        conn = MySQLdb.connect(host=db_config['host'],user=db_config['user'],passwd=db_config['passwd'],port=db_config['port'])
        conn.autocommit(True)
        curr = conn.cursor()
        curr.execute("SET NAMES utf8");
        curr.execute("USE %s" % db_config['db']);
    
        return conn, curr
    except MySQLdb.Error,e:
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])
        return None, None

conn,curr = getDB()

today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
tomorrow = today + datetime.timedelta(days=1)

print today
print yesterday 

sql_text = "select table_name from information_schema.tables where table_schema='jellyfish_server' and table_name like 'room_subscriber_roomid_9%';"
curr.execute(sql_text)
tab_list = curr.fetchall()

col_datas_union = ''
for tab_name in tab_list:
    tab_name = tab_name[0]

    sql_text = "select room_id,uid,state,created_time,updated_time from jellyfish_server.%s where substr(updated_time,1,10) ='%s' and state='0';" % (tab_name,yesterday)
    curr.execute(sql_text)
    col_datas = curr.fetchall()
    col_datas_union = list(set(col_datas_union).union(set(col_datas)))
#print col_datas_union

    #col_datas.sort(key = itemgetter(0,2))
    #sorted(col_datas,key = itemgetter(0,2))
#    group_col_datas = groupby(col_datas,itemgetter(0,2))
group_col_datas = groupby(sorted(col_datas_union,key = itemgetter(1,2)),itemgetter(1,2))   
#col_data_list = [item for item in group_col_datas]
    #print col_data_list
    #print dir(col_data_list)
    #print type(col_data_list)
    

for key,item in group_col_datas:
#for key,item in col_data_list:
    i=0
    j=0
    for jtem in item:
        i+=float(jtem[0])
        j+=1
    print key,i,j

'''
    for col_data in col_datas:
        room_id            = col_data[0] 
        uid                = col_data[1]
        state              = col_data[2]
        created_time       = col_data[3]
        updated_time       = col_data[4]
        print  (room_id + uid )
'''
curr.close()
conn.close()


附:数据验证sql
r3.dataver

select 'room_subscriber_roomid_90' tab_name, a.* from room_subscriber_roomid_90 a where substr(updated_time,1,10) ='2016-11-06' and uid='1133980217' and state=0 union all
select 'room_subscriber_roomid_91' tab_name, a.* from room_subscriber_roomid_91 a where substr(updated_time,1,10) ='2016-11-06' and uid='1133980217' and state=0 union all
select 'room_subscriber_roomid_92' tab_name, a.* from room_subscriber_roomid_92 a where substr(updated_time,1,10) ='2016-11-06' and uid='1133980217' and state=0 union all
select 'room_subscriber_roomid_93' tab_name, a.* from room_subscriber_roomid_93 a where substr(updated_time,1,10) ='2016-11-06' and uid='1133980217' and state=0 union all
select 'room_subscriber_roomid_94' tab_name, a.* from room_subscriber_roomid_94 a where substr(updated_time,1,10) ='2016-11-06' and uid='1133980217' and state=0 union all
select 'room_subscriber_roomid_95' tab_name, a.* from room_subscriber_roomid_95 a where substr(updated_time,1,10) ='2016-11-06' and uid='1133980217' and state=0 union all
select 'room_subscriber_roomid_96' tab_name, a.* from room_subscriber_roomid_96 a where substr(updated_time,1,10) ='2016-11-06' and uid='1133980217' and state=0 union all
select 'room_subscriber_roomid_97' tab_name, a.* from room_subscriber_roomid_97 a where substr(updated_time,1,10) ='2016-11-06' and uid='1133980217' and state=0 union all
select 'room_subscriber_roomid_98' tab_name, a.* from room_subscriber_roomid_98 a where substr(updated_time,1,10) ='2016-11-06' and uid='1133980217' and state=0 union all
select 'room_subscriber_roomid_99' tab_name, a.* from room_subscriber_roomid_99 a where substr(updated_time,1,10) ='2016-11-06' and uid='1133980217' and state=0 union all
select 'room_subscriber_roomid_9 ' tab_name, a.* from room_subscriber_roomid_9  a where substr(updated_time,1,10) ='2016-11-06' and uid='1133980217' and state=0;



  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值