From 86263e05f457474adac7dbeccfdfc0c783457afa Mon Sep 17 00:00:00 2001 From: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> Date: Mon, 7 Oct 2019 19:09:40 +0530 Subject: [PATCH] perf: Cache db columns to avoid redundant database calls (#8543) * perf: Cache db columns to avoid redundant database calls * fix: Move cache clearing code from doctype to schema.py * fix: self.table_name instead of self.name * fix: Cache columns in "table_columns" key `table_columns` was cached in meta but columns were also getting accessed directly using frappe.db.get_table_columns. Now, it is cached at `frappe.db` layer Co-authored-by: Suraj Shetty --- frappe/core/doctype/doctype/doctype.py | 1 - frappe/database/database.py | 15 +++++++++++---- frappe/database/mariadb/framework_mariadb.sql | 6 ++++++ frappe/database/postgres/framework_postgres.sql | 6 ++++++ frappe/database/schema.py | 1 + frappe/model/meta.py | 3 +-- 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/frappe/core/doctype/doctype/doctype.py b/frappe/core/doctype/doctype/doctype.py index bfc5ba845a..1223d50878 100644 --- a/frappe/core/doctype/doctype/doctype.py +++ b/frappe/core/doctype/doctype/doctype.py @@ -312,7 +312,6 @@ class DocType(Document): clear_linked_doctype_cache() - def delete_duplicate_custom_fields(self): if not (frappe.db.table_exists(self.name) and frappe.db.table_exists("Custom Field")): return diff --git a/frappe/database/database.py b/frappe/database/database.py index 0a3d5da25d..95096ed2d9 100644 --- a/frappe/database/database.py +++ b/frappe/database/database.py @@ -845,10 +845,17 @@ class Database(object): def get_db_table_columns(self, table): """Returns list of column names from given table.""" - return [r[0] for r in self.sql(''' - select column_name - from information_schema.columns - where table_name = %s ''', table)] + columns = frappe.cache().hget('table_columns', table) + if columns is None: + columns = [r[0] for r in self.sql(''' + select column_name + from information_schema.columns + where table_name = %s ''', table)] + + if columns: + frappe.cache().hset('table_columns', table, columns) + + return columns def get_table_columns(self, doctype): """Returns list of column names from given doctype.""" diff --git a/frappe/database/mariadb/framework_mariadb.sql b/frappe/database/mariadb/framework_mariadb.sql index 07e4021107..7058ed0325 100644 --- a/frappe/database/mariadb/framework_mariadb.sql +++ b/frappe/database/mariadb/framework_mariadb.sql @@ -49,6 +49,12 @@ CREATE TABLE `tabDocField` ( `default` text, `description` text, `in_list_view` int(1) NOT NULL DEFAULT 0, + `fetch_if_empty` int(1) NOT NULL DEFAULT 0, + `in_filter` int(1) NOT NULL DEFAULT 0, + `remember_last_selected_value` int(1) NOT NULL DEFAULT 0, + `ignore_xss_filter` int(1) NOT NULL DEFAULT 0, + `print_hide_if_no_value` int(1) NOT NULL DEFAULT 0, + `allow_bulk_edit` int(1) NOT NULL DEFAULT 0, `in_standard_filter` int(1) NOT NULL DEFAULT 0, `in_preview` int(1) NOT NULL DEFAULT 0, `read_only` int(1) NOT NULL DEFAULT 0, diff --git a/frappe/database/postgres/framework_postgres.sql b/frappe/database/postgres/framework_postgres.sql index a2270c0235..df59de92df 100644 --- a/frappe/database/postgres/framework_postgres.sql +++ b/frappe/database/postgres/framework_postgres.sql @@ -49,6 +49,12 @@ CREATE TABLE "tabDocField" ( "default" text, "description" text, "in_list_view" smallint NOT NULL DEFAULT 0, + "fetch_if_empty" smallint NOT NULL DEFAULT 0, + "in_filter" smallint NOT NULL DEFAULT 0, + "remember_last_selected_value" smallint NOT NULL DEFAULT 0, + "ignore_xss_filter" smallint NOT NULL DEFAULT 0, + "print_hide_if_no_value" smallint NOT NULL DEFAULT 0, + "allow_bulk_edit" smallint NOT NULL DEFAULT 0, "in_standard_filter" smallint NOT NULL DEFAULT 0, "in_preview" smallint NOT NULL DEFAULT 0, "read_only" smallint NOT NULL DEFAULT 0, diff --git a/frappe/database/schema.py b/frappe/database/schema.py index 1663eed95f..88cda9340b 100644 --- a/frappe/database/schema.py +++ b/frappe/database/schema.py @@ -33,6 +33,7 @@ class DBTable: if self.is_new(): self.create() else: + frappe.cache().hdel('table_columns', self.table_name) self.alter() def create(self): diff --git a/frappe/model/meta.py b/frappe/model/meta.py index fe6483a3ea..0b1011b119 100644 --- a/frappe/model/meta.py +++ b/frappe/model/meta.py @@ -46,8 +46,7 @@ def load_meta(doctype): return Meta(doctype) def get_table_columns(doctype): - return frappe.cache().hget("table_columns", doctype, - lambda: frappe.db.get_table_columns(doctype)) + return frappe.db.get_table_columns(doctype) def load_doctype_from_file(doctype): fname = frappe.scrub(doctype)