0

我正在使用 pg8000 从 csv 文件更新 Azure 上我的 postgres 数据库中的数据,但执行速度非常慢,

这是我的代码



command = "INSERT INTO public.tempcumulativesales (combined, cumulative_sales) VALUES (%s, %s)"
with open('./UpdateDatabaseFiles/rows1.csv', 'r') as file:
    next(file)
    data = list(csv.reader(file))
    cursor.executemany(command, data)


有谁知道我怎样才能提高速度?

csv 文件有 12,000 多个条目

4

1 回答 1

0

如果每个条目都很大,则 12000 个条目可能很大。如果没有关于表结构、约束或索引存在的更多信息,我们无法给出明确的答案。

可能的改进:

  • 不要等到读完所有东西就开始插入表中 - 直接使用阅读器迭代器说不同:

    data = csv.reader(file)
    
  • 如果 12000 太大并且更短的提交可能更有效(通过减少回滚段),请使用块:

      def grouper(iterable, n, fillvalue=None):
          "Collect data into fixed-length chunks or blocks"
          # from itertools recipes
          # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
          args = [iter(iterable)] * n
          return zip_longest(*args, fillvalue=fillvalue)
      ...
      chunksize = 1024    # experiment to find best one
      for chunk in grouper(data, chunksize):
          cursor.executemany(command,filter(lambda x: x is not None, i))
          db_con.commit() 
    
于 2021-06-25T14:28:39.087 回答