python - 从oracle表中读取大量数据并提取到数据框中的最佳方法是什么
问题描述
我将从我的 oracle 数据库中的表中读取数据并在 python 的数据框中获取它。该表有 2200 万条记录,使用 fetchall() 需要很长时间而没有任何结果。(查询在 oracle 中运行 1 秒)
我曾尝试使用以下代码对数据进行切片,但仍然效率不高。
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 中获取这么多数据的正确解决方案是什么。
解决方案
您将不得不调整 arraysize 和 prefetchrow 参数。我遇到了同样的问题。增加数组大小解决了这个问题。根据您拥有的内存选择值。
推荐阅读
- pg-promise - pg-promise : 取消使用 pg-query-stream 发起的查询
- r - 如何将数据从 Google Cloud Platform (BigQuery/Cloud SQL) 导入 R?
- angular - 如何停止执行数据检索,直到其他数组填满数据
- replace - Notepad ++中有没有办法使用查找和替换来查找文本,然后替换后面几行的文本?
- gremlin - 如何使用远程遍历生成用于导出的 GraphSON 数据?
- sql-server - “无法绑定多部分标识符”错误,但服务器已链接
- c++ - C++ 程序中的分段错误,查找重复字符串中出现的字符
- javascript - Javascript 中 BigInt 类型的 Math.max 和 Math.min 的替代方案
- reactjs - 在反应道具中看不到子组件
- java - 有没有办法改变 TableView 焦点可遍历的行为?