Run sql in parallel using future: but sql fails

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 called with

(run 100)

      

it will print 1..99, however if you check the row number of table a the row number will not be incremented which means sql is not executed. How do I run sql in parallel?

+3


source to share


1 answer


The only suspicious thing I see in your code is the fact that you never waited for the futures to complete (so maybe not?).

You need to collect the values ​​returned by the calls future

and then lock them until they run out 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

      

+3


source







All Articles