小编for*_*dyt的帖子

在Psycopg2中有效插入大量行

我需要有效地向我的PostgreSQL数据库插入大约500k(给出或接受100k)数据行.经过大量的谷歌搜索,我已经得到了这个解决方案,平均大约150(挂钟)秒.

def db_insert_spectrum(curs, visual_data, recording_id):
    sql = """
        INSERT INTO spectrums (row, col, value, recording_id)
        VALUES %s
    """

    # Mass-insertion technique
    # visual_data is a 2D array (a nx63 matrix)
    values_list = []
    for rowIndex, rowData in enumerate(visual_data):
        for colIndex, colData in enumerate(rowData): # colData is the value
            value = [(rowIndex, colIndex, colData, recording_id)]
            values_list.append(value)

    psycopg2.extras.execute_batch(curs, sql, values_list, page_size=1000)
Run Code Online (Sandbox Code Playgroud)

有更快的方法吗?

python postgresql psycopg2

4
推荐指数
1
解决办法
2837
查看次数

标签 统计

postgresql ×1

psycopg2 ×1

python ×1