From 031aad0d8524db5725a0fe4d43cb73d29a949c4e Mon Sep 17 00:00:00 2001 From: Suraj Shetty Date: Wed, 29 Apr 2020 12:28:08 +0530 Subject: [PATCH] perf: Cache get_tables values - To avoid redundant db calls --- frappe/database/database.py | 11 ++++++++++- frappe/database/schema.py | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/frappe/database/database.py b/frappe/database/database.py index b083ff1014..cc76745c2e 100644 --- a/frappe/database/database.py +++ b/frappe/database/database.py @@ -769,7 +769,16 @@ class Database(object): return ("tab" + doctype) in self.get_tables() def get_tables(self): - return [d[0] for d in self.sql("select table_name from information_schema.tables where table_schema not in ('pg_catalog', 'information_schema')")] + tables = frappe.cache().get_value('db_tables') + if not tables: + table_rows = self.sql(""" + SELECT table_name + FROM information_schema.tables + WHERE table_schema NOT IN ('pg_catalog', 'information_schema') + """) + tables = set([d[0] for d in table_rows]) + frappe.cache().set_value('db_tables', tables) + return tables def a_row_exists(self, doctype): """Returns True if atleast one row exists.""" diff --git a/frappe/database/schema.py b/frappe/database/schema.py index 28e055f382..3d484a1f1b 100644 --- a/frappe/database/schema.py +++ b/frappe/database/schema.py @@ -31,6 +31,7 @@ class DBTable: def sync(self): if self.is_new(): + frappe.cache().delete_key('db_tables') self.create() else: frappe.cache().hdel('table_columns', self.table_name)