I'm looking for some help on how to bulk update an Oralce SQL table with records up to 250,000.
Basically I have a list of keys that is passed in to a function that then needs to update an Oracle table. The list can up to 250,000 rows, I can do this using a normal update statement or using an 'executemany' but both methods are too inefficient, so I need to do a bulk update, but I am not familiar with how to do this. I have searched for hours but I cannot figure this out!
todays_date = datetime.now().strftime("%d-%b-%Y")
status = str("DONE")
try:
bind_values = {"status" : str(status),
"todays_date" : todays_date,
"keys_list" : list_of_keys}
query = ("""FORALL i IN :keys_list.FIRST .. :keys_list.LAST
UPDATE TABLE_NAME
SET COLUMN1 = :status,
UPDATE_DATE = :todays_date
WHERE KEY = :i""")
cursor.execute(query, bind_values)
conn.commit()
self.CloseConnection(conn)
except cx_Oracle.DatabaseError, e:
error, = e.args
print(" >> Database error: %s" % format(e))
conn.rollback()
return False
Any help would be appreciated.
UPDATE @abarnert - thank you very much for the suggestion, you are definitely on to something here, I managed to get this far
cursor.execute("""CREATE GLOBAL TEMPORARY TABLE TodaysKeys
(key STRING PRIMARY KEY)
on commit delete rows
AS (INSERT INTO TodaysKeys VALUES (:i))
UPDATE TABLE_NAME
SET COLUMN1 = :status,
UPDATE_DATE = :todays_date
WHERE KEY IN (SELECT * FROM TodaysKeys)
TABLE TodaysKeys""", i=keys_list,
status=str(updatestatus),
todays_date=todays_date)
But now all I get is an error: "ORA-01036: illegal variable name/number". I am sure it is something really obvious but I have checked and rechecked but can't for the life of me see where I am going wrong!
From all the research into this approach, it seems to be the right method...if I can just get it working to test! Please help.
UPDATEstatement across 250K rows is too slow, the problem is almost certainly in your data model or your database configuration, and there's nothing you can do from Python that will speed that up.LOAD DATA(or maybe use the separate bulk loader)… but I doubt it.