簡體   English   中英

有沒有更快的方法來獲得從 python 到 sql 服務器表的大 dataframe?

[英]Is there a faster way to get big dataframe from python to sql server table?

我正在嘗試向 sql 服務器獲取近 13000000 行,但它給出了資源池錯誤。 這是代碼:

def execute_sql_file(pconnection, pfilepath, **psqlparams):
    cursor = pconnection.cursor()
    f = open(pfilepath)
    full_sql = f.read()
    for key, value in psqlparams.items():
        full_sql = full_sql.replace('#' + str(key) + '#', str(value))
    f.close()
    return cursor.execute(full_sql)


def write_result_to_db(dfsave, pServer='oltp', pDatabase='warehouse',pSchema = 'dbo',pTable = 
'tb_table'):
    params = urllib.parse.quote_plus(
    "DRIVER={SQL Server Native Client 11.0};SERVER=" + pServer + ";DATABASE=" + pDatabase + ";   
             Trusted_Connection=yes;MARS_Connection=Yes")
    engine = sqlalchemy.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
    to_db_connection = engine.connect()

    @sqlalchemy.event.listens_for(engine, "before_cursor_execute")
    def receive_before_cursor_execute(conn, cursor, statement, params, context, executemany):
        if executemany:
            cursor.fast_executemany = True

    chunknum = math.floor(2100 / dfsave.shape[1]) - 1
    if dfsave.shape[0] > 0:
        dfsave.to_sql(pTable, con=engine, schema=pSchema, if_exists='append', index=False, 
                       method='multi',chunksize=chunknum)
    
    return to_db_connection

這是 function。 在進行數學運算之后,在計算結束時,我得到了 df. 當我嘗試將其發送到 sql 服務器時,我使用以下代碼:

write_result_to_db(df,pTable='tb_table',pServer='testoltp', pSchema = 'dbo', 
                   pDatabase = 'warehouse')

但它給出了錯誤。 然后我嘗試划分 df 並寫入 sql 表,如下所示:

bol = int(tb_result.shape[0] / 50)
for start in range(0, tb_result.shape[0], bol):
    write_result_to_db(tb_result.iloc[start:start + 
                       bol],pTable='tb_table',pServer='testoltp', pSchema = 
                       'dbo', pDatabase = 'testwarehouse')
    time.sleep(15)

執行此循環后,它會給出資源池錯誤。 我想我不能這樣做。 那么如何從 python 獲取 dataframe 到 sql 服務器表?

嘗試使用 modin 庫。 您也許可以解決它https://github.com/modin-project/modin

暫無
暫無

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

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