我需要从DB中获取数据并将其写入CSV文件中的相应列。 以下代码执行速度非常慢(iteratevly,逐个)
async def fetch_and_write():
conn = await asyncpg.connect('...')
with open('/Users/mac/Desktop/input.csv','r') as csvinput:
with open('/Users/mac/Desktop/output.csv', 'w') as csvoutput:
reader = csv.reader(csvinput)
writer = csv.writer(csvoutput, lineterminator='\n')
all = []
row = next(reader)
row.append('new_column_name')
all.append(row)
for row in reader:
query = "SELECT .. FROM .. WHERE id = '%s';"
query = query % row[14]
try:
result = await conn.fetch(query)
except BaseException:
print("Oops!That was no valid number.")
continue
row.append(result[0][0])
all.append(row)
writer.writerows(all)
如何以块的形式从CSV中读取id并使用"在"提高绩效的条款?
答案 0 :(得分:1)
您可以使用postgres'Copy
命令来完成这项工作。
例如您的查询应该是
Copy (Select * From foo) To '/tmp/test.csv' With CSV DELIMITER ',';
答案 1 :(得分:0)
根据我在评论中的建议,您可以获取" n"一个查询中的记录数。以下是您提供的修改后的代码版本。
未经测试
async def fetch_and_write():
n = 500 # fetch #rows at once
conn = await asyncpg.connect('...')
with open('/Users/mac/Desktop/input.csv','r') as csvinput:
with open('/Users/mac/Desktop/output.csv', 'w') as csvoutput:
reader = csv.reader(csvinput)
writer = csv.writer(csvoutput, lineterminator='\n')
all = []
ids_list = []
row = next(reader)
row.append('new_column_name')
all.append(row)
for row in reader:
ids_list.append(row[14])
if len(ids_list) >= n:
in_p=', '.join(map(lambda x: '%s', args))
query = "SELECT .. FROM .. WHERE id in '%s';"
query = query % in_p
try:
result = await conn.fetch(query)
except BaseException:
print("Oops!That was no valid number.")
continue
ids_list = []
row.append(result[0][0])
writer.writerows(all)
all = []
if len(ids_list)>0:
in_p=', '.join(map(lambda x: '%s', args))
query = "SELECT .. FROM .. WHERE id in '%s';"
query = query % in_p
try:
result = await conn.fetch(query)
except BaseException:
print("Oops!That was no valid number.")
continue
row.append(result[0][0])
writer.writerows(all)