clojurekormasqlkorma

Does with clause in Korma fire one query per item in relationship?


The docs at Korma page says, under entities,

(has-many email)
      ;; assumes users.id = email.users_id
      ;; but gets the results in a second query
      ;; for each element

So, if I am understanding it correctly if one teacher has many students and I want all the students for a teacher nested within each teacher map, Korma will generate one SQL query per teacher ?

Won't this hurt the performance when we have like millions of teachers ?


Solution

  • Yes, it will execute 1 additional request for every teacher to get their students. You can check with-later macro in korma, it basically adds function that will be executed once korma retrieved all teachers. This function iteraters through collection of teaches and executes (select ...) for each each teacher to get his students and attach them to teacher's map.

    Won't this hurt the performance when we have like millions of teachers ?

    I believe it does hurt performance. But probably implementing it somehow more efficient by joining teacher and students tables and then group students by teachers is more complex to implement and more error-prone. Anyway querying millions of records and converting them to maps in clojure would be both memory and time consuming process.