<p>Hey everyone,</p>
<p>I have to query about 15000 rows from a Postgres database, of 1 or 2 columns.
I do this with the pq driver and regular database/sql. Run rows, err := db.Query(...), start a for rows.Next() loop and Scan into some variables. However, this will go well for a few thousand rows and then start sporadically halt for dozens of seconds, at what appears to be the rows.Next() point.</p>
<p>I've searched all over but I can't seem to find an explanation for this. Does a query page through the results? As in, fetch a bunch and requery for the next batch once the end is reached?</p>
<p>Any help would be greatly appreciated</p>
<p>Thanks!</p>
<hr/>**评论:**<br/><br/>hell_0n_wheel: <pre><blockquote>
<p>I've searched all over but I can't seem to find an explanation for this.</p>
</blockquote>
<p>Because the explanation is on your server, not on the internet. And we aren't going to be able to explain it to you because we can't see what's on your server either. This is all you. </p>
<p>Your next steps might go something like: find the option(s) and/or tool(s) provided with your DB to start logging slow queries. Find out what are the slowest, then use EXPLAIN to see what's going on... it might tell you that you're missing an index or something.</p>
<p>You might also look at lock contention, to see if your query is hung up waiting for some row(s) to become unlocked.</p>
<p>You could also take a packet capture of the traffic between the DB and your program. Could be some clues there.</p>
<blockquote>
<p>at what appears to be the rows.Next() point.</p>
</blockquote>
<p>You might also profile your program, to make sure this is what's happening. You could spend all day looking at your DB but if your program's locking itself, all that DB work is for naught.</p></pre>watr: <pre><p>If you figure this out, please make sure to post the solution. I'm very interested. </p>
<p>First step is always to isolate the problem. Make sure it's not postgres first, then focus on your app. Postgres has a large and helpful community.</p></pre>Everlag: <pre><p>Without the actual code, I can't really give you more than 'use the profiler'.</p>
<p>Specifically, <a href="https://github.com/pkg/profile" rel="nofollow">this package</a> should drop into your <code>main()</code>. Since you probably want to ctrl-c your program rather than wait once the slowdown happens, <a href="https://stackoverflow.com/a/11269077" rel="nofollow">catch the signal</a> and call <code>profile.Stop()</code> using a closure to capture the result from <code>profile.Start()</code>.</p>
<p>Some potential issues could be heap allocation or constant regex compilation. Profiling should reveal this.</p></pre>hobbified: <pre><p>The profiler is going to say "socket read", most likely.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传