【问题标题】:libpqxx: How to bind array of parameterslibpqxx:如何绑定参数数组
【发布时间】:2018-05-26 20:03:55
【问题描述】:

我想尽快在 postgres 表中插入许多参数。

现在我浪费了太多时间来一一绑定参数。代码看起来几乎是这样的:

pqxx::connection my_connection(c_string);
my_connection.prepare("insert_to_db", "INSERT INTO t (id, name) VALUES ($1, $2));

pqxx::work W(my_connection);
for (int i = 0; i < 10000; i++)
{
  W.prepared("insert_to_db")(i)("Max").exec();
}
W.commit();

如我所见,commit 10 000 个元素需要 0.001 秒甚至更少,但绑定大约需要 10 秒。

我想将所有参数绑定为值数组。如何使用 pqxx 做到这一点? 或者有更好的方法来减少时间?

【问题讨论】:

标签: c++ postgresql libpqxx


【解决方案1】:

尽可能使用 pqxx::prepare::invocation 并在执行前绑定更多值,因为它更稳定且可以防止错误,但我在下面描述了一种更快的方法。在您的示例中,您每次都执行准备好的语句,因此您与数据库进行了不必要的通信。

我。 通过调用:

pqxx::nontransaction W(C);
std::string m_insertCommand = "INSERT INTO tableforperftest(column1, column2) VALUES";


unsigned  int m_nCurrentRow = 32767;

for (size_t i = 0; i < m_nCurrentRow; i++)
{
    unsigned int countOf$ = i * 2;
    for (unsigned int i = 0; i < 2; ++i)
    {
        if (i == 0)
        {
            m_insertCommand += "(";
        }
        else
        {
            m_insertCommand += ", ";
        }
        m_insertCommand += "$";
        std::stringstream ss;
        ss << countOf$ + i + 1;
        m_insertCommand += ss.str();
    }
   if(i < m_nCurrentRow - 1)
    m_insertCommand += ") ,";
}
m_insertCommand += ")";

C.prepare("insert_into_db", m_insertCommand);
pqxx::prepare::invocation inv = W.prepared("insert_into_db");

for (size_t i = 0; i < m_nCurrentRow; i++)
{
    inv(i)(i);
}

inv.exec();

二。 使用存储过程获取更多参数值:

CREATE OR REPLACE FUNCTION insertintoboosted(valuesforinsert TEXT) RETURNS VOID AS
$$ 
BEGIN
     EXECUTE 'INSERT INTO tableforperftestproof(column1, column2) VALUES (' || valuesforinsert || ')';
END;
$$
LANGUAGE plpgsql;

代码:

for (size_t i = 0; i < m_nCurrentRow; i++)
{

    if (i == 0)
        ss  << i << "," << i;
    else
        ss << "(" << i << "," << i;

    if (i < m_nCurrentRow - 1)
        ss << "),";
}

C.prepare("prep2", "select insertintoboosted($1::text)");

W.prepared("prep2")(ss).exec();

三。 每次都有参数绑定和执行:

std::string m_insertCommand3 = "INSERT INTO tableforperftest(column1, column2) VALUES ($1, $2)";
C.prepare("insert_into_db3", m_insertCommand3);
for (size_t i = 0; i < m_nCurrentRow; i++)
{
    W.prepared("insert_into_db3")(i)(i).exec();
}

将解决方案与 32767 插入进行比较:

Invocation:                              --> Elapsed:    0.250292s
Stored Proc:                             --> Elapsed:    0.154507s 
Parameter binding + execution each time: --> Elapsed:    29.5566s

【讨论】:

    【解决方案2】:
    pqxx::connection c;
    pqxx::work w(c);
    c.prepare("prep", "select stored_proc($1::text[])");
    auto r = w.prepared("prep")("{v1, v2}").exec();
    

    【讨论】:

    • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation 将通过展示为什么这是解决问题的好方法,并使其对有其他类似问题的未来读者更有用,从而大大提高其长期价值。请edit您的回答添加一些解释,包括您所做的假设。 ref
    猜你喜欢
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 2020-08-11
    • 2013-04-20
    相关资源
    最近更新 更多