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.
This commit is contained in:
Ankush Menat
2025-04-21 22:16:07 +05:30
parent 1b85fbf0e5
commit 4cbc92649f
3 changed files with 15 additions and 4 deletions

View File

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

View File

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

View File

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