如何使用python对比两个表的数据量?对于数据迁移后的数据检验非常重要!免费分享!拿去不谢

以下是测试通过的版本。可以换成自己的库,表,实际对比应该不止对比一个表,到时候配置一个列表,逐个轮询。

import concurrent.futures
import pymysql

# 连接数据库的配置
db_config1 = {
    'host': '127.0.0.1',
    'user': 'kkk',
    'password': '',
    'db': 'world',
    'charset': 'utf8mb4'
}

db_config2 = {
     'host': '127.0.0.1',
    'user': 'kkk',
    'password': 'kkk',
    'db': 'mysql',
    'charset': 'utf8mb4'
}


# 查询表的数据量
def count_records(db_config, table):
    connection = pymysql.connect(**db_config)
    with connection.cursor() as cursor:
        cursor.execute(f"SELECT COUNT(1) FROM {table}")
        count = cursor.fetchone()[0]
    connection.close()
    return count


# 表1和表2
table1 = 'countrylanguage'
table2 = 'user'

# 使用并行处理进行查询
with concurrent.futures.ThreadPoolExecutor() as executor:
    future1 = executor.submit(count_records, db_config1, table1)
    future2 = executor.submit(count_records, db_config2, table2)

    count1 = future1.result()
    count2 = future2.result()

print(f"Table1 {table1} count: {count1}")
print(f"Table2 {table2} count: {count2}")

执行结果如下:

Table1 countrylanguage count: 984
Table2 user count: 5

Process finished with exit code 0
 

公众号小分享~~~

  • 14
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_38111284

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值