Sfoglia il codice sorgente

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>
version-14
Suraj Shetty 5 anni fa
committed by mergify[bot]
parent
commit
86263e05f4
6 ha cambiato i file con 25 aggiunte e 7 eliminazioni
  1. +0
    -1
      frappe/core/doctype/doctype/doctype.py
  2. +11
    -4
      frappe/database/database.py
  3. +6
    -0
      frappe/database/mariadb/framework_mariadb.sql
  4. +6
    -0
      frappe/database/postgres/framework_postgres.sql
  5. +1
    -0
      frappe/database/schema.py
  6. +1
    -2
      frappe/model/meta.py

+ 0
- 1
frappe/core/doctype/doctype/doctype.py Vedi File

@@ -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


+ 11
- 4
frappe/database/database.py Vedi File

@@ -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."""


+ 6
- 0
frappe/database/mariadb/framework_mariadb.sql Vedi File

@@ -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,


+ 6
- 0
frappe/database/postgres/framework_postgres.sql Vedi File

@@ -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,


+ 1
- 0
frappe/database/schema.py Vedi File

@@ -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):


+ 1
- 2
frappe/model/meta.py Vedi File

@@ -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)


Caricamento…
Annulla
Salva