【问题标题】:Clojure jdbc - query single column flattened resultClojure jdbc - 查询单列展平结果
【发布时间】:2019-07-07 23:12:09
【问题描述】:

我正在尝试将数据(cca 760k 行)从单列读取到一个(扁平)向量中。 clojure.java.jdbc/query 的结果是地图的序列,例如({:key "a"} {:key "b"} ...)。提供选项:as-arrays? true 时,将返回[[:key] ["a"] ["b"] ...]。为了使结果变平,我还使用了选项:row-fn first 并得到了[:key "a" "b" ...]。最后,我申请了rest 摆脱了:key

用向量包装和展开行似乎是很多不必要的工作。我对表现也不满意。有更快/更惯用的方式吗?我试过了……

(jdbc/with-db-connection [con -db-spec-]
  (with-open [^Statement stmt (.createStatement (:connection con))
              ^ResultSet res  (.executeQuery stmt query)]
    (let [ret (ArrayList.)]
      (while (.next res)
        (.add ret (.getString res 1)))
      (into [] ret))))

...但它并没有快多少,而且很丑。


编辑

更好的方法是通过传感器(参见here):

(into []
      (map :key)
      (jdbc/reducible-query
       connection
       ["SELECT key FROM tbl"]
       {:raw? true}))

【问题讨论】:

    标签: jdbc clojure


    【解决方案1】:

    您可以使用:row-fn :key。不确定您期望的性能,但在我的 i5 PC 上,检索 760K 记录大约需要 3 秒(基于 H2 文件的数据库)

    (time
     (count
      (jdbc/query db ["select top 760000 key from table1"] {:row-fn :key})))
    ;; => 760000
    
    "Elapsed time: 3003.456295 msecs"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-19
      • 2012-09-14
      • 2019-12-17
      • 2012-09-23
      • 2014-07-08
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多