【发布时间】:2019-09-23 01:05:04
【问题描述】:
我应该重复使用游标对象还是为每个查询创建一个新的对象?
重用光标:
# we're going to connect to s3 and mysql
db = mysql_connect(host="localhost",
user="user",
passwd="pass",
database="db")
# Reusing the cursor
cursor = db.cursor()
# loop through all the files in the bucket one by one
for key in bucket.get_all_keys():
# the document id is the filename in the s3 bucket
doc_id = key.name.split("/")[-1]
cursor.execute("SELECT document_name FROM laws_docs WHERE id = %i", (doc_id,))
doc_name = cursor.fetchone()[0]
cursor.close()
- 或 -
每次新光标:
# we're going to connect to s3 and mysql
db = mysql_connect(host="localhost",
user="user",
passwd="pass",
database="db")
# loop through all the files in the bucket one by one
for key in bucket.get_all_keys():
# new cursor
cursor = db.cursor()
# the document id is the filename in the s3 bucket
doc_id = key.name.split("/")[-1]
cursor.execute("SELECT document_name FROM laws_docs WHERE id = %i", (doc_id,))
doc_name = cursor.fetchone()[0]
ursor.close()
这还重要吗?该循环将至少运行 50,000 次。
【问题讨论】:
标签: python-3.x mysql-python mysql-connector