擅长:python、mysql、java
<p>尝试使用内置于<a href="https://www.postgresql.org/docs/9.2/static/sql-copy.html" rel="nofollow noreferrer">COPY</a>的Postgres,或者如果您确实需要在Python中检索结果(例如,您不能通过COPY直接写入磁盘),您可以使用COPY via psycopgs<a href="http://initd.org/psycopg/docs/cursor.html#cursor.copy_expert" rel="nofollow noreferrer">copy_expert</a>函数:</p>
<pre><code>cur = conn.cursor()
outputquery = "COPY ({0}) TO STDOUT WITH CSV HEADER".format(query)
with open('resultsfile', 'w') as f:
cur.copy_expert(outputquery, f)
conn.close()
</code></pre>
<p>这应该避免全部序列化。在</p>