df_name.to_sql('table_name',
schema = 'public',
con = engine,
index = False,
if_exists = 'replace')
λλ 500,000 ν λ°μ΄ν° νλ μμ postgres AWS λ°μ΄ν°λ² μ΄μ€μ μ°κ³ μλλ° λ°μ΄ν°λ₯Ό νΈμνλ λ° λ§€μ° μ€λ μκ°μ΄ 걸립λλ€.
μλΉν ν° SQL μλ²μ΄κ³ λ΄ μΈν°λ· μ°κ²°μ΄ μ°μνλ―λ‘ λ¬Έμ μ κΈ°μ¬νλ κ²μΌλ‘ λ°°μ νμ΅λλ€.
μ΄μ λΉν΄ csv2sqlμ μ¬μ©νκ±°λ λͺ λ Ή μ€μμ cat λ° pipingμ psqlμ μ¬μ©νλ κ²μ΄ ν¨μ¬ λΉ λ¦ λλ€.
μ¬κΈ°λ₯Ό μ°Έμ‘°νμμμ€ : http://stackoverflow.com/questions/33816918/write-large-pandas-dataframes-to-sql-server-database
SQLServerλ₯Ό μ¬μ©νλ©΄ ν¨μ¨μ±μ μν΄ λλ μ λ‘λμ ν¨κ» csvλ₯Ό ν΅ν΄ κ°μ ΈμμΌν©λλ€.
μ μ© ν κ²μ λλ€ : http://odo.pydata.org/en/latest/perf.html
ODOλ λλ₯Ό μν΄ μλνμ§ μκ³ μμ ν μ μμλ μ€λ₯λ₯Ό μμ±νμ§λ§ d6tstackμ https://github.com/d6t/d6tstack/blob/master/examples-sql.ipynb μμ μ μλνμ΅λλ€
engine = create_engine(connection_string)
μλμμ΄ μ½λλ₯Ό μΆκ°νμμμ€.
from sqlalchemy import event
@event.listens_for(e, 'before_cursor_execute')
def receive_before_cursor_execute(conn, cursor, statement, params, context, executemany):
if executemany:
cursor.fast_executemany = True
cursor.commit()
λ΄ μ½λμμ to_sql
ν¨μλ₯Ό μ€ννλ λ° 7 λΆμ΄ κ±Έλ¦¬κ³ μ΄μ λ 5 μ΄ λ°μ κ±Έλ¦¬μ§ μμ΅λλ€.)
κ°μ¬ν©λλ€ @llautert!
λ§μ λμμ΄λμμ΅λλ€!
# dont forget to import event
from sqlalchemy import event, create_engine
engine = create_engine(connection_string)
@event.listens_for(engine, 'before_cursor_execute')
def receive_before_cursor_execute(conn, cursor, statement, params, context, executemany):
if executemany:
cursor.fast_executemany = True
cursor.commit()
μ΄ μμ νλ‘κ·Έλ¨μ μ€ννλ €κ³ νμ§λ§ μ€λ₯ λ©μμ§κ° νμλ©λλ€.
AttributeError: 'psycopg2.extensions.cursor' object has no attribute 'fast_executemany'
λ¬΄μ¨ μΌμΈμ§ μλ μ¬λ μλμ?
μλ νμΈμ @ tim-sauchuk, λμΌν μ€λ₯κ° λ°μνμ΅λλ€. pandas.io.sql.py νμΌμ μ½κ° νΈμ§νλ λ± νλ₯νκ² μλνλ μ루μ μ μ°Ύμμ§λ§ λ€μ κ°μ Έ μ€κΈ° μ μ __pycache__μμ .pyc νμΌμ μμ νμμμ€. μμΆ νμΌμ μ λ²μ μ κΈ°λ‘νλμ§ νμΈ)
μλ νμΈμ @ tim-sauchuk, λμΌν μ€λ₯κ° λ°μνμ΅λλ€. pandas.io.sql.py νμΌμ μ½κ° νΈμ§νλ λ± νλ₯νκ² μλνλ μ루μ μ μ°Ύμμ§λ§ λ€μ κ°μ Έ μ€κΈ° μ μ pycache μμ .pyc νμΌμ μμ νμμμ€. μμΆ νμΌμ μ λ²μ μ κΈ°λ‘νλμ§ νμΈ)
8953
@ bsaunders23μ΄ μΈκΈ ν μ΄μ # 8953μ "μμμ΄ ν¨μΉ"(λ°νμμ μμ ) λ°©λ²μ 보μ¬μ€λλ€. λλ κ·Έκ²μ μλνκ³ , μ λ‘λνλ λ° 10 λΆ μ΄μ 걸리λ 20k λ°μ΄ν° μΈνΈλ 4 μ΄ λ°μ κ±Έλ¦¬μ§ μμμ΅λλ€.
κ°μ¬ν©λλ€ @llautert!
λ§μ λμμ΄λμμ΅λλ€!# dont forget to import event from sqlalchemy import event, create_engine engine = create_engine(connection_string) @event.listens_for(engine, 'before_cursor_execute') def receive_before_cursor_execute(conn, cursor, statement, params, context, executemany): if executemany: cursor.fast_executemany = True cursor.commit()
self.engine μΈμ€ν΄μ€κ°μλ ν΄λμ€ λ΄μμμ΄ μ루μ μ ꡬννλ λ°©λ²μ μλ μ¬λμ΄ μμ΅λκΉ?
self.engine μΈμ€ν΄μ€κ°μλ ν΄λμ€ λ΄μμμ΄ μ루μ μ ꡬννλ λ°©λ²μ μλ μ¬λμ΄ μμ΅λκΉ?
self.engine
λ₯Ό μ°Έμ‘°νμ¬ λλ₯Ό μν΄ μλν©λλ€.
μ:
self.engine = sqlalchemy.create_engine(connectionString, echo=echo)
self.connection = self.engine.connect()
@event.listens_for(self.engine, 'before_cursor_execute')
def receive_before_cursor_execute(conn, cursor, statement, params, context, executemany):
print("Listen before_cursor_execute - executemany: %s" % str(executemany))
if executemany:
cursor.fast_executemany = True
cursor.commit()
λλ₯Ό μν΄ μλνμ§ μμ΅λλ€. μ΄λ€ pandasμ sqlalchemy λ²μ μ μ¬μ©νκ³ μμ΅λκΉ?
sqlalchemy : 1.2.4-py35h14c3975_0 λ° 1.2.11-py35h7b6447c_0 μ€ν ν΄ λ³΄μμ΅λλ€.
νμ§λ§ λλ μ μ
AttributeError : 'psycopg2.extensions.cursor'κ°μ²΄μ 'fast_executemany'μμ±μ΄ μμ΅λλ€.
νΈμ λ΄μ κ°κΈ°
μ΄ μ»¨ν μ€νΈμμ ν¨μ νΈμΆμ μ΄λ»κ² μκ²Όμ΅λκΉ? μ¦, ν μ΄λΈμ μ±κ³΅μ μΌλ‘ μ λ‘λνκΈ° μν΄ μΈμλ‘ λ¬΄μμ μ¬μ©νκ³ μμ΅λκΉ?
<# dont forget to import event
from sqlalchemy import event, create_engine
engine = create_engine(connection_string)
@event.listens_for(engine, 'before_cursor_execute')
def receive_before_cursor_execute(conn, cursor, statement, params, context, executemany):
if executemany:
cursor.fast_executemany = True
cursor.commit()>``
sqlalchemy : 1.2.4-py35h14c3975_0 λ° 1.2.11-py35h7b6447c_0 μ€ν ν΄ λ³΄μμ΅λλ€.
νμ§λ§ λλ μ μ
AttributeError : 'psycopg2.extensions.cursor'κ°μ²΄μ 'fast_executemany'μμ±μ΄ μμ΅λλ€.
postgresql λλΌμ΄λ² μΈ psycopg2λ₯Ό μ¬μ©νκ³ μμ΅λλ€. μ΄ λ¬Έμ λ° μμ μ pyodbc λλΌμ΄λ²λ₯Ό μ¬μ©νλ Microsoft SQL Serverμ κ΄λ ¨μ΄ μμ΅λλ€.
'dtype'λ§€κ° λ³μ μΆκ°λ μ΄λ»μ΅λκΉ?
self.engine μΈμ€ν΄μ€κ°μλ ν΄λμ€ λ΄μμμ΄ μ루μ μ ꡬννλ λ°©λ²μ μλ μ¬λμ΄ μμ΅λκΉ?
self.engine
λ₯Ό μ°Έμ‘°νμ¬ λλ₯Ό μν΄ μλν©λλ€.μ:
self.engine = sqlalchemy.create_engine(connectionString, echo=echo) self.connection = self.engine.connect() @event.listens_for(self.engine, 'before_cursor_execute') def receive_before_cursor_execute(conn, cursor, statement, params, context, executemany): print("Listen before_cursor_execute - executemany: %s" % str(executemany)) if executemany: cursor.fast_executemany = True cursor.commit()
λ°©λ²μ μμ λμ΅λκΉ?
pandas dataframe
λ₯Ό μ μ₯νλ €λ κ²½μ° μ λ΅μ https://docs.sqlalchemy.org/en/13/dialects/postgresql.html#psycopg2 -batch-mode-fast-executionμ μ¬μ©νλ κ²μ
λλ€. pandas dataframe
~ postgres
μ λ²μ μ Pandasμλ 'λ©ν°'λ‘ μ νν μμλ method
λ§€κ° λ³μκ° ν¬ν¨λμ΄ μμ΅λλ€. μ΄λ κ²νλ©΄ μ½λκ° ν¨μ¬ λΉ λ₯΄κ² μ€νλ©λλ€.
fast_executemany
λ μ΄μ λ¨μΌ λ¨κ³λ‘ μν ν μ μμ΅λλ€ (sqlalchemy> = 1.3.0) :
engine = sqlalchemy.create_engine(connection_string, fast_executemany=True)
λ¬Έμμ μ΄λκ°μμ λλ μμ μ ν¨κ» μΈκΈ ν κ°μΉκ° μμ΅λκΉ? νλ€μ κ΄λ ¨μ΄μλ νΉλ³ν κ²½μ°μ΄μ§λ§ λ§μ κ²½μ° μ±λ₯μ ν¬κ² ν₯μμν¬ μμλ μμ μΆκ°μ λλ€.
μ λ²μ μ Pandasμλ 'λ©ν°'λ‘ μ νν μμλ
method
λ§€κ° λ³μκ° ν¬ν¨λμ΄ μμ΅λλ€. μ΄λ κ²νλ©΄ μ½λκ° ν¨μ¬ λΉ λ₯΄κ² μ€νλ©λλ€.
chunksize
λ§€κ° λ³μλ₯Ό μ€μ νλ©΄ to_sql
μΌκ΄ μ½μ
μ μννλ λ° μΆ©λΆνλ€κ³ μκ°ν μ μμ΅λλ€.
MS SQL μ¬μ©μλ₯Όμν λμμ turbodbc.Cursor.insertmanycolumns
λ μ¬μ©νλ κ²μ
λλ€. λ§ν¬ λ StackOverflow κ²μλ¬Όμμ μ€λͺ
νμ΅λλ€. https://stackoverflow.com/a/62671681/1689261
μ΄μ λν΄ ν₯ν λ μλ₯Ό μν΄ to_sqlμ 'batch_mode'λ₯Ό μ¬μ©νλ λ κ°μ§ μ΅μ μ΄ μμ΅λλ€. λ€μμ λ κ°μ§ μ‘°ν©μ λλ€.
create_engine(connection_string, executemany_mode='batch', executemany_batch_page_size=x)
λλ
create_engine(connection_string, executemany_mode='values', executemany_values_page_size=x)
μ΄λ¬ν μΈμμ λν μμΈν λ΄μ©μ https://docs.sqlalchemy.org/en/13/dialects/postgresql.html#psycopg2 -fast-execution-helpersμμ μ°Ύμ μ μμ΅λλ€.
postgres μ¬μ©μμ κ²½μ° method
λ₯Ό μ½λ¬ λΈλ‘ μ€μ νλ κ²μ΄ μ’μ΅λλ€.
μλͺ μΌλ‘ νΈμΆ κ°λ₯ (pd_table, conn, keys, data_iter) : νΉμ λ°±μλ μΈμ΄ κΈ°λ₯μ κΈ°λ°μΌλ‘λ³΄λ€ μ±λ₯μ΄ λ°μ΄λ μ½μ λ°©λ²μ ꡬννλ λ° μ¬μ©ν μ μμ΅λλ€.
μ¬κΈ° https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html#insertion -method λ° μμ μ½λμμ ν¨μλ₯Ό νΈμΆν©λλ€.
COPY FROM
μ΄ μ λ§ ν¨μ¬ λΉ λ¦
λλ€ π
κ°μ₯ μ μ©ν λκΈ
engine = create_engine(connection_string)
μλμμ΄ μ½λλ₯Ό μΆκ°νμμμ€.λ΄ μ½λμμ
to_sql
ν¨μλ₯Ό μ€ννλ λ° 7 λΆμ΄ κ±Έλ¦¬κ³ μ΄μ λ 5 μ΄ λ°μ κ±Έλ¦¬μ§ μμ΅λλ€.)