什么是从oracle表读取大量数据并获取到数据帧的最佳方法

问题描述 投票:1回答:1

我将从我的oracle数据库中的表中读取数据并在python中的数据框中获取它。该表有2200万条记录,使用fetchall()需要很长时间而没有任何结果。 (查询在1秒内​​在oracle中运行)

我尝试使用下面的代码切片数据,但仍然没有效率。

import cx_Oracle
import pandas as pd
from pandas import DataFrame
connect_serv = cx_Oracle.connect(user='', password='', dsn='')
cur = connect_serv.cursor()  

table_row_count=22242387;
batch_size=100000;

sql="""select t.* from (select a.*,ROW_NUMBER() OVER (ORDER BY column1 ) as row_num  from  table1 a) T where t.row_num between :LOWER_BOUND and :UPPER_BOUND"""

data=[]
for lower_bound in range (0,table_row_count,batch_size):
    cur.execute(sql,{'LOWER_BOUND':lower_bound,
                     'UPPER_BOUND':lower_bound + batch_size - 1})
    for row in cur.fetchall():
        data.append(row)

我想知道在合理的时间内在python中获取此数据量的正确解决方案是什么。

python cx-oracle fetchall
1个回答
0
投票

这不是查询慢,而是使用data.append(row)堆叠数据。

尝试使用

data.extend(cur.fetchall())

对于初学者。它将避免重复的单行追加,而是立即附加来自fetchall的整个行集。

© www.soinside.com 2019 - 2024. All rights reserved.