                     collect (cl-loop for hook in (alist-get 'hooks repo)
                                      collect (alist-get 'id hook))))))
 
+(defvar python-pre-commit-database-cache nil)
 (defun python-pre-commit-virtualenv-path (hook-id)
   (use-package emacsql-sqlite)
-  (let ((db (emacsql-sqlite
-             (concat
-              (file-name-as-directory
-               (or (getenv "PRE_COMMIT_HOME")
-                   (getenv "XDG_CACHE_HOME")
-                   "~/.cache/"))
-              "pre-commit/db.db")
-             :debug t)))
-    (unwind-protect
-        (let* ((repo-config
-                (seq-find
-                 (lambda (repo)
-                   (seq-find
-                    (lambda (hook)
-                      (equal (alist-get 'id hook) hook-id))
-                    (alist-get 'hooks repo)))
-                 (alist-get 'repos (python-pre-commit-config))))
-               (additional-deps
-                (alist-get 'additional_dependencies
-                           (seq-find
-                            (lambda (hook)
-                              (equal (alist-get 'id hook) hook-id))
-                            (alist-get 'hooks repo-config))))
-               (repo-path
-                (caar (emacsql db [:select [path]
-                                   :from repos
-                                   :where (and (= repo $r1)
-                                               (= ref $r2))]
-                               (concat (alist-get 'repo repo-config)
-                                       (if additional-deps
-                                           (concat ":" (string-join additional-deps ":"))))
-                               (alist-get 'rev repo-config))))
-               (result
-                (car
-                 (list
-                  (file-expand-wildcards
-                   (concat (file-name-as-directory (symbol-name repo-path)) "py_env-*")
-                   t)))))
-          result)
-      (emacsql-close db))))
+  (when-let* ((db-file (concat
+                        (file-name-as-directory
+                         (or (getenv "PRE_COMMIT_HOME")
+                             (getenv "XDG_CACHE_HOME")
+                             "~/.cache/"))
+                        "pre-commit/db.db"))
+              (db (and (file-exists-p db-file)
+                       (let ((db-file-mod-time
+                              (file-attribute-modification-time (file-attributes db-file))))
+                         (or (and (equal
+                                   (cdr python-pre-commit-database-cache)
+                                   db-file-mod-time)
+                                  (car python-pre-commit-database-cache))
+                             (when-let ((conn (emacsql-sqlite db-file :debug t)))
+                               (unwind-protect
+                                   (progn
+                                     (setf python-pre-commit-database-cache
+                                           (cons
+                                            (emacsql conn [:select * :from repos])
+                                            db-file-mod-time))
+                                     (car python-pre-commit-database-cache))
+                                 (emacsql-close conn)))))))
+              (repo-config
+               (seq-find
+                (lambda (repo)
+                  (seq-find
+                   (lambda (hook)
+                     (equal (alist-get 'id hook) hook-id))
+                   (alist-get 'hooks repo)))
+                (alist-get 'repos (python-pre-commit-config)))))
+
+    (let* ((additional-deps
+            (alist-get 'additional_dependencies
+                       (seq-find
+                        (lambda (hook)
+                          (equal (alist-get 'id hook) hook-id))
+                        (alist-get 'hooks repo-config))))
+           (repo-url
+            (concat (alist-get 'repo repo-config)
+                    (if additional-deps
+                        (concat ":" (string-join additional-deps ":")))))
+           (repo-dir (symbol-name
+                      (nth 2 (seq-find
+                              (lambda (row)
+                                (and (equal (symbol-name (car row))
+                                            repo-url)
+                                     (equal (symbol-name (cadr row))
+                                            (alist-get 'rev repo-config))))
+                              db)))))
+      (car
+       (last
+        (file-expand-wildcards
+         (concat (file-name-as-directory repo-dir) "py_env-*")
+         t))))))
 
 (use-package flycheck
   :delight

         "\\.handlebars\\'"
         "\\.underscore\\'"
         "\\.html?\\'"
         "\\.jinja\\'"
         "\\.mako\\'"
         "\\.dtl\\'"
         "\\.jsp\\'"

          (lambda ()
            (use-package python-docstring
              :delight
              :config (python-docstring-mode 1))

            (when (python-use-poetry-p)
              (make-local-variable 'python-shell-interpreter)
