I currently have a list of id’s approx. of size 10,000. I need to update all rows in the mySQL table which have an id in the inactive_ids list that you see below. I need to change their active status to ‘No’ which is a column in the mySQL table.
I am using mysql.connector python library.
When I run the code below, it is taking about 0.7 seconds to execute each iteration in the for loop. Thats about a 2 hour run time for all 10,000 id’s to be changed. Is there a more optimal/quicker way to do this?
JavaScript
x
39
39
1
# inactive_ids are unique strings something like shown below
2
# inactive_ids = ['a9okeoko', 'sdfhreaa', 'xsdfasy', ..., 'asdfad']
3
4
# initialize connection
5
mydb = mysql.connector.connect(
6
user="REMOVED",
7
password="REMOVED",
8
host="REMOVED",
9
database="REMOVED"
10
)
11
12
# initialize cursor
13
mycursor = mydb.cursor(buffered=True)
14
15
# Function to execute multiple lines
16
def alter(state, msg, count):
17
result = mycursor.execute(state, multi=True)
18
result.send(None)
19
print(str(count), ': ', msg, result)
20
count += 1
21
return count
22
23
# Try to execute, throw exception if fails
24
try:
25
count = 0
26
for Id in inactive_ids:
27
28
# SAVE THE QUERY AS STRING
29
sql_update = "UPDATE test_table SET Active = 'No' WHERE NoticeId = '" + Id + "'"
30
31
# ALTER
32
count = alter(sql_update, "done", count)
33
34
# commits all changes to the database
35
mydb.commit()
36
except Exception as e:
37
mydb.rollback()
38
raise e
39
Advertisement
Answer
Do it with a single query that uses IN (...)
instead of multiple queries.
JavaScript
1
8
1
placeholders = ','.join(['%s'] * len(inactive_ids))
2
sql_update = f"""
3
UPDATE test_table
4
SET Active = 'No'
5
WHERE NoticeId IN ({placeholders})
6
"""
7
mycursor.execute(sql_update, inactive_ids)
8