Qt 10000 插入
我想在数据库中插入 10000 次以上。我使用这个简单的代码:
int i = 0;
for (i = 0; i < model->rowCount(); i++)
{
query.clear();
query.prepare("INSERT INTO item (title, x, y, z) VALUES (:title, "
":x, :y, :z);");
query.bindValue(":title", title);
query.bindValue(":x", model->data(model->index(i, 0)));
query.bindValue(":y", model->data(model->index(i, 1)));
query.bindValue(":z", model->data(model->index(i, 2)));
if (! query.exec())
return;
}
但是当模型中找到太多记录时,它太慢了。我怎样才能加快速度? Qt 中可能有一些数据包插入功能吗?
我正在使用:Qt 4.7.0、PostgreSQL 8.4.8 和 Qt SQL 驱动程序。
I want to make above 10000 inserts in database. I use this simple code:
int i = 0;
for (i = 0; i < model->rowCount(); i++)
{
query.clear();
query.prepare("INSERT INTO item (title, x, y, z) VALUES (:title, "
":x, :y, :z);");
query.bindValue(":title", title);
query.bindValue(":x", model->data(model->index(i, 0)));
query.bindValue(":y", model->data(model->index(i, 1)));
query.bindValue(":z", model->data(model->index(i, 2)));
if (! query.exec())
return;
}
but it is too slow while too many record are in model found. How can I speed it up? May be there are some packet insertion abilities available in Qt?
I am using: Qt 4.7.0, PostgreSQL 8.4.8 and Qt SQL driver.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只准备一次,执行多次?我对 QT 一点也不熟悉,但这可能会加快整个过程:
Just prepare once, execute many? I'm not at all familiar with QT, but this might speed the whole process up:
创建一个事务,插入值,然后提交。
Create a transaction, insert the values, then commit.
@Berry Langerak 的解决方案对于 Qt 方面来说是正确的......如果您成功 开始交易。
如果做不到这一点,您可能需要查看 COPY,但是我不认为这是你的问题。
如果您不知道如何开始事务,快速更改 postgresql.conf 设置可能是一个快速解决方案。如果您在事务之外,则每个 INSERT 都会导致 fsync(2) 调用。
如果您发现 PostgreSQL 是瓶颈,网上有大量性能调优信息,但我猜您是在事务中完成工作。
@Berry Langerak's solution is the correct one for the Qt side of things.... if you've successfully started a transaction.
Failing that, you might want to look in to COPY, but I don't think that's your problem.
A quick change to your postgresql.conf settings might be a quick fix if you can't figure out how to BEGIN a transaction. If you are outside of a transaction, every INSERT is resulting in an fsync(2) call.
There's a host of performance tuning information online if you find that PostgreSQL is the bottleneck, but I'm guessing you're doing your work out of a transaction.