From 87310e1117c6590f70dc9c35deee8db4bd5459ac Mon Sep 17 00:00:00 2001 From: Remco van 't Veer Date: Sun, 19 May 2024 22:25:19 +0200 Subject: [PATCH] Speedup final view insert with analyse Sometimes (when track_counts=off maybe?) the query planner takes the wrong approach when selecting data for the "bag" table and the statement takes many hours to finish instead of minutes. --- src/straatnaam/lvbag.clj | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/straatnaam/lvbag.clj b/src/straatnaam/lvbag.clj index dbb4d90..1375716 100644 --- a/src/straatnaam/lvbag.clj +++ b/src/straatnaam/lvbag.clj @@ -304,7 +304,14 @@ (defn- insert-bag! [db sn] (log/debug "inserting bag data for" sn) (sql/db-do-commands db true - [(str "SET SCHEMA '" sn "'") + [;; Force running analyze to help query planner + ;; in the next insert step, otherwise it may + ;; take a very long time. + (str "ANALYSE " (->> template-tables + (map #(str sn "." %)) + (string/join ", "))) + + (str "SET SCHEMA '" sn "'") (str "INSERT INTO bag " ;; Keeping the query in the source code ;; because putting it in the template -- 2.45.2