通过Python分批从MySQL中检索数据

2024-05-16 10:12:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我想分批做这个过程,因为体积太大了。

这是我的代码:

 getconn = conexiones()
 con = getconn.mysqlDWconnect()
 with con:
     cur = con.cursor(mdb.cursors.DictCursor)
     cur.execute("SELECT id, date, product_id, sales FROM sales")
     rows = cur.fetchall()

如何实现索引以批量获取数据?


Tags: 代码id过程with体积concursorsales
3条回答

你可以用

SELECT id, date, product_id, sales FROM sales LIMIT X OFFSET Y;

其中X是所需批的大小,Y是当前偏移量(例如X乘以当前迭代次数)

第一点:pythondb-api.cursor是一个迭代器,因此除非您真的需要一次将整个批加载到内存中,否则您可以从使用此功能开始,即不要:

cursor.execute("SELECT * FROM mytable")
rows = cursor.fetchall()
for row in rows:
   do_something_with(row)

你可以:

cursor.execute("SELECT * FROM mytable")
for row in cursor:
   do_something_with(row)

如果您的db connector的实现仍然不能正确地使用这个特性,那么现在就应该为这个组合添加LIMIT和OFFSET:

# py2 / py3 compat
try:
    # xrange is defined in py2 only
    xrange
except NameError:
    # py3 range is actually p2 xrange
    xrange = range

cursor.execute("SELECT count(*) FROM mytable")
count = cursor.fetchone()[0]
batch_size = 42 # whatever

for offset in xrange(0, count, batch_size):
    cursor.execute(
        "SELECT * FROM mytable LIMIT %s OFFSET %s", 
        (batch_size, offset))
   for row in cursor:
       do_something_with(row)

要扩展akalikin的答案,可以使用分步迭代将查询分割成块,然后使用LIMIT和OFFSET执行查询。

cur = con.cursor(mdb.cursors.DictCursor)
cur.execute("SELECT COUNT(*) FROM sales")

for i in range(0,cur.fetchall(),5):
    cur2 = con.cursor(mdb.cursors.DictCursor)
    cur2.execute("SELECT id, date, product_id, sales FROM sales LIMIT %s OFFSET %s" %(5,i))
    rows = cur2.fetchall()
    print rows

相关问题 更多 >