From 4cbc92649fd0f6b2b1b0f3addcd0d0e9974f398f Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 21 Apr 2025 22:16:07 +0530 Subject: [PATCH] perf: Only created company indexes in multi-company setups A vast vast majority of installations don't have multiple companies, so this unnecessary index is an overhead for them. --- erpnext/accounts/doctype/gl_entry/gl_entry.json | 5 ++--- erpnext/accounts/doctype/gl_entry/gl_entry.py | 9 ++++++++- erpnext/setup/doctype/company/company.py | 5 +++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/doctype/gl_entry/gl_entry.json b/erpnext/accounts/doctype/gl_entry/gl_entry.json index 401fe086451..59186107e57 100644 --- a/erpnext/accounts/doctype/gl_entry/gl_entry.json +++ b/erpnext/accounts/doctype/gl_entry/gl_entry.json @@ -242,8 +242,7 @@ "label": "Company", "oldfieldname": "company", "oldfieldtype": "Link", - "options": "Company", - "search_index": 1 + "options": "Company" }, { "fieldname": "finance_book", @@ -358,7 +357,7 @@ "idx": 1, "in_create": 1, "links": [], - "modified": "2025-04-21 22:03:45.548936", + "modified": "2025-04-21 22:10:25.844576", "modified_by": "Administrator", "module": "Accounts", "name": "GL Entry", diff --git a/erpnext/accounts/doctype/gl_entry/gl_entry.py b/erpnext/accounts/doctype/gl_entry/gl_entry.py index 2243420f13c..d460a89d2bc 100644 --- a/erpnext/accounts/doctype/gl_entry/gl_entry.py +++ b/erpnext/accounts/doctype/gl_entry/gl_entry.py @@ -439,8 +439,15 @@ def update_against_account(voucher_type, voucher_no): def on_doctype_update(): frappe.db.add_index("GL Entry", ["voucher_type", "voucher_no"]) - frappe.db.add_index("GL Entry", ["posting_date", "company"]) frappe.db.add_index("GL Entry", ["party_type", "party"]) + add_company_indexes() + + +def add_company_indexes(): + """Only add company indexes if more than one company exists.""" + if frappe.db.count("Company", {"name": ("not like", "%(Demo)%")}) > 1: + frappe.db.add_index("GL Entry", ["posting_date", "company"]) + frappe.db.add_index("GL Entry", ["company"]) def rename_gle_sle_docs(): diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py index 8146083075e..af2aeea2035 100644 --- a/erpnext/setup/doctype/company/company.py +++ b/erpnext/setup/doctype/company/company.py @@ -707,6 +707,11 @@ class Company(NestedSet): ): frappe.flags.parent_company_changed = True + def before_insert(self): + from erpnext.accounts.doctype.gl_entry.gl_entry import add_company_indexes + + add_company_indexes() + def get_name_with_abbr(name, company): company_abbr = frappe.get_cached_value("Company", company, "abbr")