运行 sql 并行使用 future: 但 sql 未执行
run sql in parallel using future: but the sql is not executed
我有以下功能
(defn run [x]
(doseq [i (range 1 x)]
(println i)
(future (j/execute! vertica-db ["insert /*+ direct */ into a select * from a limit 1"]))
))
使用
调用时
(run 100)
它会打印1..99,但是如果检查table a的行号,行号没有增加,这意味着sql没有执行。如何运行并行sql?
我在您的代码中看到的唯一可疑之处是您从不等待 futures 完成(所以也许他们不会?)。
您需要收集 future
调用返回的值,然后使用 (deref f)
/@f
(即取消引用未来)阻塞直到它们完成,其中 f
是这些值之一。
像这样的东西应该可以工作:
(defn run [x]
(let [db-insert (fn [i] ((println i) (future (j/execute! vertica-db ["insert /*+ direct */ into a select * from a limit 1"]))))
inserts (doall (map db-insert (range 1 x)))] ;force execution of all the db-insert calls
(doseq [insert inserts] @insert))) ;wait for all the futures to finish
我有以下功能
(defn run [x]
(doseq [i (range 1 x)]
(println i)
(future (j/execute! vertica-db ["insert /*+ direct */ into a select * from a limit 1"]))
))
使用
调用时(run 100)
它会打印1..99,但是如果检查table a的行号,行号没有增加,这意味着sql没有执行。如何运行并行sql?
我在您的代码中看到的唯一可疑之处是您从不等待 futures 完成(所以也许他们不会?)。
您需要收集 future
调用返回的值,然后使用 (deref f)
/@f
(即取消引用未来)阻塞直到它们完成,其中 f
是这些值之一。
像这样的东西应该可以工作:
(defn run [x]
(let [db-insert (fn [i] ((println i) (future (j/execute! vertica-db ["insert /*+ direct */ into a select * from a limit 1"]))))
inserts (doall (map db-insert (range 1 x)))] ;force execution of all the db-insert calls
(doseq [insert inserts] @insert))) ;wait for all the futures to finish