clojure.java.jdbc 懒惰查询 [英] clojure.java.jdbc lazy query
问题描述
我有一个基本上是 select *
的查询.在开发中,这个表只有 30000 行,但在生产中它会更大.所以我想懒惰地使用这个查询.为什么下面的查询不是懒惰的?我使用的是 Postgres 9.5.4.1.
I have a query that is basically a select *
. In development this table is only 30000 rows, but in production it will much bigger. So I want to consume this query lazily. Why is the query below not lazy?
I am using Postgres 9.5.4.1.
(do
(def pg-uri {:connection-uri "jdbc:postgresql://localhost/..."})
(def row (atom 0))
(take 10 (clojure.java.jdbc/query
pg-uri
["select * from mytable"]
{:fetch-size 10
:auto-commit false
:row-fn (fn [r] (swap! row inc))}))
@row) ;;=> 300000
推荐答案
clojure.java.jdbc
这些天来支持本地对大型结果集的延迟处理(此处的其他答案早于本机支持).请在此处查看有关它的社区文档:
clojure.java.jdbc
supports lazy processing of large result sets natively these days (the other answers here predate that native support). See the community documentation about it here:
http://clojure-doc.org/articles/ecosystem/java_jdbc/using_sql.html#processing-a-result-set-lazily
特别是,请参阅其他选项?部分了解您可能需要的特定于数据库的调整.您可以指定 :auto-commit?false
在将打开新连接的任何函数上,您可以在任何查询相关函数上指定 :fetch-size
和各种光标控件.看到这个 StackOverflow 问题 &回答有关 PostgreSQL 可能需要什么的详细信息:
In particular, see the Additional Options? section for database-specific tweaks you might need. You can specify :auto-commit? false
on any function that would open a new connection, and you can specify :fetch-size
and the various cursor controls on any query-related function. See this StackOverflow question & answer for details of what PostgreSQL might need:
目前,您必须深入clojure.java.jdbc
源 或 prepare-statement
参考文档 以获取更多这些选项.我将继续编写社区文档以显示所有这些信息.
Currently, you'll have to dig in the clojure.java.jdbc
source or the prepare-statement
reference documentation for more of those options. I'm continuing to work on the community documentation to surface all of that information.
这篇关于clojure.java.jdbc 懒惰查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!