TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: How to increase PHP+Postgres speed? my testing result is slow

1 点作者 Elect2大约 6 年前
# PHP: ` $dbconn = pg_connect(&quot;host=127.0.0.1 dbname=test user=test password=test&quot;); $start = microtime(true); for($i=1;$i&lt;=5000;$i++) { pg_query(&quot;insert into largedb(uid,sid) values($i,$i)&quot;); } echo ( (microtime(true) - $start) *1000).&#x27; ms&#x27;; exit; ` &#x2F;&#x2F; result: 15504.348993301 ms<p># Shell: ` begin for r in 1..5000 loop insert into largedb (uid,sid) values(r,r); end loop; end; $$; ` &#x2F;&#x2F; result: 43 ms<p>PHP&amp;PG are in same server. Shell&#x27;s command is very fast, so seems there is much time spend in interact between PHP and PG. How can I increase this? I&#x27;ve also tested PHP+Mongodb, insert same amount of data only takes 200ms.

2 条评论

gregjor大约 6 年前
More of Stack Overflow question. Not clear on how the Shell script works... insert into not a Shell command. Maybe try prepared statement. Impossible to diagnose with seeing the table definition.
gregjor大约 6 年前
I assume uid and sid have indexes on them. If they do, note that inserting sorted values into a b-tree (today structure for a database indeed) creates worst-case performance.