improving query performance considerably.

This commit is contained in:
BC
2019-01-26 22:14:47 -08:00
parent d18f68a15c
commit e2050ce8ee
2 changed files with 49 additions and 36 deletions

View File

@@ -4,13 +4,13 @@
[auto-ap.graphql.utils :refer [limited-clients]] [auto-ap.graphql.utils :refer [limited-clients]]
[clj-time.coerce :as c])) [clj-time.coerce :as c]))
(defn sort-fn [args] (defn sort-fn [sort-by]
(cond (cond
(= "client" (:sort-by args)) (= "client" sort-by)
#(-> % :transaction/client :client/name) #(-> % :transaction/client :client/name)
:else :else
(keyword "transaction" (:sort-by args)))) (keyword "transaction" sort-by)))
(defn add-arg [query name value where & rest] (defn add-arg [query name value where & rest]
(let [query (-> query (let [query (-> query
@@ -19,9 +19,9 @@
(update-in [:query :where] conj where))] (update-in [:query :where] conj where))]
(reduce #(update-in %1 [:query :where] conj %2) query rest))) (reduce #(update-in %1 [:query :where] conj %2) query rest)))
(defn apply-sort [args results ] (defn apply-sort [args sort-fn results ]
(cond->> results (cond->> results
(:sort-by args) (sort-by (sort-fn args)) sort-fn (sort-by sort-fn)
(= (:asc args) false) (reverse))) (= (:asc args) false) (reverse)))
(defn apply-pagination [args results] (defn apply-pagination [args results]
@@ -29,38 +29,49 @@
(drop (:start args 0)) (drop (:start args 0))
(take (:count args 100)))) (take (:count args 100))))
(defn raw-graphql-ids [args]
(->> (d/query
(cond-> {:query {:find [['pull '?e (into [:db/id] (condp = (:sort-by args)
"client" [{:transaction/client [:client/name]}]
nil []
[(keyword "transaction" (:sort-by args))]))
]]
:in ['$]
:where ['[?e :transaction/id]]}
:args [(d/db (d/connect uri))]}
(limited-clients (:id args)) (add-arg '[?xx ...] (set (map :db/id (limited-clients (:id args))))
'[?e :transaction/client ?xx])
(:client-id args) (add-arg '?client-id (:client-id args)
'[?e :transaction/client ?client-id])
(:original-id args) (add-arg '?original-id (cond-> (:original-id args) (string? (:original-id args)) Long/parseLong )
'[?e :transaction/client ?c]
'[?c :client/original-id ?original-id])))
(map first)
(map #(update % :transaction/date c/from-date))
(map #(update % :transaction/post-date c/from-date))))
(defn graphql-results [ids args] (defn raw-graphql-ids [db args]
(->
(cond-> {:query {:find (if (:sort-by args)
['?e '?sorter]
['?e])
:in ['$ ]
:where
(condp = (:sort-by args)
"client" ['[?e :transaction/id]
'[?e :transaction/client ?c]
'[?c :client/name ?sorter]]
nil [['?e :transaction/id]]
[['?e (keyword "transaction" (:sort-by args)) '?sorter]]
)
}
:args [db]}
(limited-clients (:id args)) (add-arg '[?xx ...] (set (map :db/id (limited-clients (:id args))))
'[?e :transaction/client ?xx])
(:client-id args) (add-arg '?client-id (:client-id args)
'[?e :transaction/client ?client-id])
(:original-id args) (add-arg '?original-id (cond-> (:original-id args) (string? (:original-id args)) Long/parseLong )
'[?e :transaction/client ?c]
'[?c :client/original-id ?original-id]))
(d/query)))
(defn graphql-results [ids db args]
(->> (d/query (->> (d/query
(cond-> {:query {:find ['(pull ?xx [* {:transaction/client [:client/name :db/id] (cond-> {:query {:find ['(pull ?xx [* {:transaction/client [:client/name :db/id]
:transaction/bank-account [:bank-account/name :bank-account/yodlee-account-id]}])] :transaction/bank-account [:bank-account/name :bank-account/yodlee-account-id]}])]
:in ['$] :in ['$]
:where []} :where []}
:args [(d/db (d/connect uri))]} :args [db]}
true (add-arg '[?xx ...] (set (map :db/id ids)) true (add-arg '[?xx ...] ids
'[?xx]))) '[?xx :transaction/id])))
(map first) (map first)
(map #(update % :transaction/date c/from-date)) (map #(update % :transaction/date c/from-date))
@@ -69,12 +80,14 @@
(defn get-graphql [args] (defn get-graphql [args]
(let [ids-to-retrieve (->> (raw-graphql-ids args) (let [db (d/db (d/connect uri))
(apply-sort args)) ids-to-retrieve (->> (raw-graphql-ids db args)
(apply-sort args (when (:sort-by args) second)))
matching-count (count ids-to-retrieve) matching-count (count ids-to-retrieve)
ids-to-retrieve (apply-pagination args ids-to-retrieve )] ids-to-retrieve (apply-pagination args ids-to-retrieve )]
[(graphql-results ids-to-retrieve args) [(->> (graphql-results (map first ids-to-retrieve) db args)
(apply-sort args (some-> (:sort-by args) sort-fn)))
matching-count])) matching-count]))

View File

@@ -12,10 +12,10 @@
[transactions transactions-count] (d-transactions/get-graphql (<-graphql args)) [transactions transactions-count] (d-transactions/get-graphql (<-graphql args))
transactions (map ->graphql transactions)] transactions (map ->graphql transactions)]
[{:transactions transactions [{:transactions transactions
:total transactions-count :total transactions-count
:count (count transactions) :count (count transactions)
:start (:start args 0) :start (:start args 0)
:end (+ (:start args 0) (count transactions))}])) :end (+ (:start args 0) (count transactions))}]))