gpt4 book ai didi

python-3.x - 在遍历非常大的 ndarray 时,是否有更快的方法将记录插入 postgresql 数据库?

转载 作者:行者123 更新时间:2023-11-29 13:10:35 25 4
gpt4 key购买 nike

我正在尝试遍历 ndarray 以将其索引和值记录到 postgresql。这是我的代码:

    for idx, val in enumerate(data):
cur.execute("INSERT INTO public.spams(review_id, label, confidence_level, aoc, created_at) VALUES (%s, %s, %s, %s, %s)", (idx+1, spamlabel, 0, 0, dt.now()))

ndarray 的大小为 762k,插入这些值需要 8 小时以上。有没有更有效的方法来做到这一点?

最佳答案

使用 psycopg2 的 execute_values辅助方法,还提供常量来限制我们必须传输的数据,例如:

from psycopg2 import extras

extras.execute_values(
cur,
"INSERT INTO public.spams(review_id, label, confidence_level, aoc, created_at) VALUES %s",
enumerate(data),
template = "(%s + 1, %s, 0, 0, CURRENT_TIMESTAMP)")

您还可以尝试使用 page_size 参数进一步调整吞吐量。

关于python-3.x - 在遍历非常大的 ndarray 时,是否有更快的方法将记录插入 postgresql 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55323126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com