簡體   English   中英

從 oracle 表中讀取大量數據並提取到數據框中的最佳方法是什么

[英]What is an optimum way to read huge data from oracle table and fetch into a data frame

我將從我的 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 中獲取這么多數據的正確解決方案是什么。

慢的不是查詢,而是數據與data.append(row)的堆疊。

嘗試使用

data.extend(cur.fetchall())

對於初學者。 它將避免重復的單行追加,而是一次追加來自fetchall的整組行。

您將必須調整 arraysize 和 prefetchrow 參數。 我有同樣的問題。 增加 arraysize 解決了這個問題。 根據您的記憶選擇值。

鏈接: https ://cx-oracle.readthedocs.io/en/latest/user_guide/tuning.html?highlight=arraysize#choosing-values-for-arraysize-and-prefetchrows

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM