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 <surajshetty3416@gmail.com>
This commit is contained in:
parent
f926a2aa48
commit
86263e05f4
6 changed files with 25 additions and 7 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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."""
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue