- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 libpq 开发 postgresql。下面给出的代码花费了大量时间(代码末尾给出的时间)。
#include "stdafx.h"
#include <stdlib.h>
#include <libpq-fe.h>
#include <windows.h>
static void exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int _tmain(int argc, _TCHAR* argv[])
{
const TCHAR *conninfo;
PGconn *conn;
PGresult *res;
int nFields, i, j;
if (argc > 1)
conninfo = argv[1];
else
conninfo = _T("hostaddr=192.168.4.171 port=12345 dbname=mydb user=myname password=mypass");
conn = PQconnectdb(conninfo);
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
exit_nicely(conn);
}
/* Start a transaction block */
res = PQexec(conn, "BEGIN");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
TCHAR szVal1[200];
TCHAR szVal2[200];
TCHAR szBuffer[200];
TCHAR *paramValues[2];
int paramLengths[2];
int paramFormats[2] = {0,0};
ExecStatusType eStatus;
LARGE_INTEGER li;
QueryPerformanceFrequency(&li);
double dAppFreq = double(li.QuadPart)/1000.0;
QueryPerformanceCounter(&li);
LONGLONG siStartCounter = li.QuadPart;
TCHAR szStmt[512] = {0};
_tcscpy_s(szStmt, 512, _T("Insert50k"));
Oid oidTypes[2] = {0,0};
PGresult *pRes = PQprepare(conn,
szStmt,
_T("insert into details values($1,$2);"),
2,
oidTypes);
QueryPerformanceCounter(&li);
LONGLONG siEndCounter = li.QuadPart;
LONGLONG siLoop = 0;
double dDiff = (siEndCounter - siStartCounter)/dAppFreq;
printf("Prepared %.2lf\n", dDiff);
for(int i=0; i<50000; i++)
{
_stprintf_s(szVal1, 200, _T("%d"), i);
_stprintf_s(szVal2, 200, _T("Detail%d"), i);
paramValues[0] = szVal1;
paramValues[1] = szVal2;
paramLengths[0] = _tcslen(szVal1);
paramLengths[1] = _tcslen(szVal2);
siStartCounter = siEndCounter;
pRes = PQexecPrepared(conn,
szStmt,
2,
paramValues,
paramLengths,
paramFormats,
0);
QueryPerformanceCounter(&li);
siEndCounter = li.QuadPart;
siLoop += (siEndCounter - siStartCounter);
eStatus = PQresultStatus(res);
if (!res || (eStatus != PGRES_COMMAND_OK) )
{
PQclear(res);
exit_nicely(conn);
}
}
dDiff = siLoop/dAppFreq;
printf("Inserted %.2lf\n", dDiff);
siStartCounter = siEndCounter;
_tcscpy_s(szBuffer,200, _T("select count(*) from programdetails;"));
res = PQexec(conn, szBuffer);
eStatus = PQresultStatus(res);
if (!res || (eStatus != PGRES_TUPLES_OK) )
{
PQclear(res);
exit_nicely(conn);
}
/* first, print out the attribute names */
nFields = PQnfields(res);
for (i = 0; i < nFields; i++)
printf("%-15s", PQfname(res, i));
printf("\n\n");
/* next, print out the rows */
for (i = 0; i < PQntuples(res); i++)
{
for (j = 0; j < nFields; j++)
printf("%-15s", PQgetvalue(res, i, j));
printf("\n");
}
QueryPerformanceCounter(&li);
siEndCounter = li.QuadPart;
dDiff = (siEndCounter - siStartCounter)/dAppFreq;
printf("Printed %.2lf\n", dDiff);
/* end the transaction */
res = PQexec(conn, "COMMIT");
PQclear(res);
/* close the connection to the database and cleanup */
PQfinish(conn);
return 0;
}
示例输出(以毫秒为单位):
Prepared 0.55
Inserted 5527.52
count
50000
Printed 7.58
这里的查询是先准备,再执行。这个简单的插入大约需要 5.5 秒。有没有更好的方法来做同样的事情,还是我做错了什么?
最佳答案
在 TCP 连接上,每个 INSERT 都会导致到数据库的 TCP 往返。在 5.5 秒内完成 50000 次插入意味着一次 TCP 往返需要大约 0.1 毫秒。您必须将其与您的网络设备的 TCP 基准进行比较,但您可能不能指望使用这种方法会更快。
您应该考虑使用 COPY FROM STDIN
而不是单独的 INSERT。在内部,这将缓冲内容,并且由于与服务器的往返次数大大减少,您可能会看到速度显着提高。
参见 http://www.postgresql.org/docs/current/static/libpq-copy.html用于与这种形式的 COPY 相关的 libpq API。
关于c++ - 简单的 Postgresql libpq 代码太慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10510539/
我用 cudaMemcpy()一次将 1GB 的数据精确复制到设备。这需要 5.9 秒。反之则需要 5.1 秒。这是正常的吗?函数本身在复制之前有这么多开销吗? 理论上,PCIe 总线的吞吐量至少应为
我正在尝试读取图像的大小并在其高度大于 150 时调整其边距。但是在运行这段代码时,我总是在控制台中得到一个“0”: var coverImg; coverImg =
我正在开发一个 iPhone 应用程序,其中包含一些标准的“相机”功能。保存到相机胶卷真的太慢了,在iPhone 4上大约需要四秒钟。有什么办法可以提高速度吗? 如果您查看默认的 iPhone 相
我创建了一个将图像转换为灰度的类。但它的工作速度太慢了。有没有办法让它运行得更快? 这是我的类(class): @implementation PixelProcessing SYNTHESIZE_S
我使用以下代码,结果是正确的,但 gethostbyaddr 需要大约 30 秒。 function IPAddrToName(IPAddr: string): string; var So
我有以下案例, public class Test { private static final int MAX_NUMBER = 10_00_00; public static vo
我已经正确添加了所有必需的 JARS: Ucanaccess 3.0.4 commons-lang-2.6 commons-logging-1.1.1 hsqldbd jackcess-2.1.3 我
我为特定功能构建了一个多处理密码破解程序(使用单词列表),与使用单个进程相比,它减少了一半的时间。 最初的问题是,它会向您显示破解的密码并终止工作人员,但剩余的工作人员将继续工作,直到他们用完可哈希的
我在我的一个 JSP 中引入了 Sencha 网格。本地 sencha 相当快,但在外部服务器上它太慢了。 我在这里按照部署说明进行操作 http://docs.sencha.com/ext-js/4
我的查询加载时间有很大问题。在这种情况下,我需要 hg_ft_won 列(表:值)中的值,用于 home_team_id 和 away_team_id(表:匹配)。 它确实可以正常工作。加载只需要很长
我现在正在学习不同类型的排序,我发现,从某个点开始,我的快速排序算法根本无法快速工作。 这是我的代码: class QuickSort { // partitioning arr
为什么要模式 [0123]123456|98765 比在 Java 中执行 [0123]123456 然后 98765 慢两倍?所以单独搜索它们比用 OR 执行更快。有人有解释吗? UPD 查看带有结
我有带 Assets 的 Android 应用程序。它们包含 20,000 多个文件,其中大部分是简单的文本或 png 文件,分为不同的文件夹和子文件夹。1 个单个文件的最大大小为 500kb,其中
您好,我在查询中添加了 GROUP_CONCAT 函数,该函数终止了我的查询:/。我的查询是: SELECT u.username,a.user_id,a.id,a.text,a.lang as fr
我正在寻找优化查询的想法。 目前,我有一个 4M 行的表,我只想检索引用的最后 1000 行: SELECT * FROM customers_material_events WHERE refere
我在我的应用程序中使用 NSURLConnection,我在其中扫描条形码,通过 NSURLConnection 发送 XML,Java 服务向我发回 XML。我的问题是,使用 Wifi 时,响应时间
当我运行以下程序时,执行大约需要 7 到 8 分钟。我真的不确定我哪里弄错了,因为这个程序执行起来要花很多时间。 public class Test { public stat
我正在使用 NSFetchResultsController 从数据库中接收项目(有 80.000 个项目)。 这是我的谓词:@"(desc CONTAINS[cd] %@)", [any text]
我在 x_data 中有一个 3x2000 numpy 数组,在 y_data 中有一个 1x2000 numpy 数组,我将其传递给此函数 regress 以给我一条回归线。它工作正常。问题是我正在
我正在做一个项目,我需要改变图像的亮度和对比度,它是亮度而不是亮度。所以我一开始的代码是 for (int y = 0; y (y, x); // read pixel (0,0)
我是一名优秀的程序员,十分优秀!