clojure.java.jdbc惰性查询 [英] clojure.java.jdbc lazy query

查看:268
本文介绍了clojure.java.jdbc惰性查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本上是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:

Java JDBC忽略setFetchSize吗?

当前,您必须在clojure.java.jdbc源中进行挖掘

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆