简体   繁体   English

psycopg2-更快地插入具有多列的多行

[英]psycopg2 - Inserting multiple rows that have multiple columns faster

I'm trying to insert multiple rows into my database, and currently I do not know a way to insert them all at the same time or any other method which will help save time (sequentially it takes about ~30s for around 300 rows). 我正在尝试在数据库中插入多行,目前我不知道同时插入所有行的方法或任何其他有助于节省时间的方法(大约300行大约需要30秒)。

My 'rows' are are tuples in a list of tuples (converted into tuple of tuples), eg [(col0, col1, col2), (col0, col1, col2), (.., .., ..), ..] 我的“行”是元组列表中的元组(转换为元组的元组),例如[(col0, col1, col2), (col0, col1, col2), (.., .., ..), ..]

def commit(self, tuple):
    cursor = self.conn.cursor()
    for tup in tuple:
        try:
            sql = """insert into "SSENSE_Output" ("productID", "brand", "categoryID", "productName", "price", "sizeInfo", "SKU", "URL", "dateInserted", "dateUpdated")
              values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""

            cursor.execute(sql, tup)
            self.conn.commit()
        except psycopg2.IntegrityError:
            self.conn.rollback()
            sql = 'insert into "SSENSE_Output" ' \
                  '("productID", "brand", "categoryID", "productName", "price", "sizeInfo", "SKU", "URL", "dateInserted", "dateUpdated")' \
                  'values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) on conflict ("productID") do update set "dateUpdated" = EXCLUDED."dateUpdated"'
            cursor.execute(sql, tup)
            self.conn.commit()
        except Exception as e:
            print(e)

I have also tried commiting after the for loop is done, but still results in the same amount of time. 我也尝试在for循环完成后提交,但仍然导致相同的时间。 Are there any ways to make this insert significantly faster? 有什么方法可以使插入速度更快吗?

Building one large INSERT statement instead of many of them will considerably improve the execution time, you should take a look here . 构建一个大的INSERT语句而不是其中的许多语句将大大缩短执行时间,您应该在这里看看。 It is for mysql, but I think a similar approach apply for postgreSQL 它用于mysql,但我认为类似的方法适用于postgreSQL

In postgres you can use a format like: 在postgres中,您可以使用以下格式:

INSERT INTO films (code, title, did, date_prod, kind) VALUES
('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),
('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy');

Due to your record base exception handling you can better first resolve the duplicates before generating this query as the whole query might fail when an integrity error occurs. 由于您的记录库异常处理,您最好在生成此查询之前先解决重复项,因为发生完整性错误时整个查询可能会失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM