Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run sql in parallel using future: but the sql is not executed

Tags:

clojure

future

I have the following function

(defn run [x]          
  (doseq [i  (range 1 x)]
     (println i)
     (future (j/execute! vertica-db ["insert /*+ direct */ into a select * from a limit 1"]))
   ))

when call it using

(run 100)

it will print 1..99, however if check the row number of table a, the row number is not increased which mean the sql is not executed. How to run the sql in parallel?

like image 877
Daniel Wu Avatar asked May 15 '26 20:05

Daniel Wu


1 Answers

The only suspicious thing I see in your code is the fact that you never wait for the futures to finish (so maybe they don't ?).

You need to collect the values returned by future calls and then block until they finish by using (deref f)/@f (i.e. dereferencing the future) where f is one of those values.

Something like this should work:

(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
like image 112
soulcheck Avatar answered May 18 '26 16:05

soulcheck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!