[英]Python: Export Large SQL Server Query Result to .txt File
将超过 600,000,000 行的 SQL Server 查询结果复制到本地.txt
文件的性能和内存效率最高的方法是什么? 您可能认为我没有从 SQL Server Management Studio 导出的用户权限。 出于这个原因,Python 似乎是我最好的选择。
我目前正在使用 Python pyodbc
包:
connection = pyodbc.connect('Driver=DRIVER;' \
'Server=SERVER;' \
'Database=DATABASE;' \
'uid=USERNAME;' \
'pwd=PASSWORD')
cursor = connection.cursor()
try:
cursor.execute("SELECT * FROM %s" % table)
except:
print('===== WAITING ===== EXECUTE ERROR =====')
time.sleep(15)
cursor.execute("SELECT * FROM %s" % table)
try:
data = cursor.fetchall()
except:
print('===== WAITING ===== FETCH ERROR =====')
time.sleep(15)
data = cursor.fetchall()
with open(output_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f, delimiter=delimiter)
writer.writerow([x[0] for x in cursor.description]) # column headers
for row in data:
writer.writerow(row)
cursor.close()
旁注:我的目标是将数百个 SQL 表作为 .txt 文件传输到 Amazon S3 存储桶。 有没有更好的方法来代替将文件下载到本地驱动器然后上传到 S3?
这取决于结果集,但作为一般规则,我会使用fetchmany
抓取一堆行,而不是将所有内容都拉入内存:
fetch_rows = 1000
rows = cursor.fetchmany(fetch_rows)
while rows is not None:
for row in rows:
do_something()
rows = cursor.fetchmany(fetch_rows)
祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.