简体   繁体   中英

how to set fetch-size for a query in clojure jdbc?

In the clojure jdbc file of https://github.com/clojure/java.jdbc/blob/master/src/main/clojure/clojure/java/jdbc.clj we could set fetch size for function of prepare-statement. But it's very often to just query a large table and also need to set the fetch size. Won't it better to also provide an option to the function of "query"?

The query function used to allow options to be passed through to the prepare-statement call as part of the "SQL & params" vector but it was not well-documented:

(query [{:fetch-size 50} "SELECT * FROM students WHERE age = ?" 24])

As of java.jdbc 0.6.0-rc2, prepare-statement options can be passed in the normal options map position:

(query ["SELECT * FROM students WHERE age = ?" 24] {:fetch-size 50})

The older way, above, will work on versions prior to 0.6.0-rc2 but is no longer supported (it will throw an exception). See http://dev.clojure.org/jira/browse/JDBC-125 for more details.

If I understand you correctly, you would like to set the maximum number of rows that are returned from a query. In that case, you can use LIMIT , like this

(clojure.java.jdbc/query
 ["SELECT * FROM students WHERE age = 24 ORDER BY created LIMIT 50"])

This should be helpful: Clojure and Large Result Sets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM