python 读取mysql大量数据处理,在python中读取巨大的MySQL表的最快方法

本文探讨了如何使用Pandas和multiprocessing模块处理大量MySQL数据,通过将查询分割并分配到多个并行进程中,提高了读取效率。分别展示了基于分类索引和时间序列的两种查询分割示例,以适应不同类型的数据库表。
摘要由CSDN通过智能技术生成

I was trying to read a very huge MySQL table made of several millions of rows. I have used Pandas library and chunks. See the code below:

import pandas as pd

import numpy as np

import pymysql.cursors

connection = pymysql.connect(user='xxx', password='xxx', database='xxx', host='xxx')

try:

with connection.cursor() as cursor:

query = "SELECT * FROM example_table;"

chunks=[]

for chunk in pd.read_sql(query, connection, chunksize = 1000):

chunks.append(chunk)

#print(len(chunks))

result = pd.concat(chunks, ignore_index=True)

#print(type(result))

#print(result)

finally:

print("Done!")

connection.close()

Actually the execution time is acceptable if I limit the number o

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值