mirror of
https://github.com/frappe/erpnext.git
synced 2026-03-17 05:52:10 +00:00
perf: faster count estimation (#46550)
These count queries themselves take quite a long time. `estimate_count`
uses info_schema stats to guess the time.
Co-authored-by: Nabin Hait <nabinhait@gmail.com>
(cherry picked from commit e47a87839b)
Co-authored-by: Ankush Menat <ankush@frappe.io>
This commit is contained in:
@@ -139,7 +139,7 @@ class PeriodClosingVoucher(AccountsController):
|
|||||||
self.cancel_gl_entries()
|
self.cancel_gl_entries()
|
||||||
|
|
||||||
def make_gl_entries(self):
|
def make_gl_entries(self):
|
||||||
if self.get_gle_count_in_selected_period() > 5000:
|
if frappe.db.estimate_count("GL Entry") > 100_000:
|
||||||
frappe.enqueue(
|
frappe.enqueue(
|
||||||
process_gl_and_closing_entries,
|
process_gl_and_closing_entries,
|
||||||
doc=self,
|
doc=self,
|
||||||
@@ -154,16 +154,6 @@ class PeriodClosingVoucher(AccountsController):
|
|||||||
else:
|
else:
|
||||||
process_gl_and_closing_entries(self)
|
process_gl_and_closing_entries(self)
|
||||||
|
|
||||||
def get_gle_count_in_selected_period(self):
|
|
||||||
return frappe.db.count(
|
|
||||||
"GL Entry",
|
|
||||||
{
|
|
||||||
"posting_date": ["between", [self.period_start_date, self.period_end_date]],
|
|
||||||
"company": self.company,
|
|
||||||
"is_cancelled": 0,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
def get_pcv_gl_entries(self):
|
def get_pcv_gl_entries(self):
|
||||||
self.pl_accounts_reverse_gle = []
|
self.pl_accounts_reverse_gle = []
|
||||||
self.closing_account_gle = []
|
self.closing_account_gle = []
|
||||||
|
|||||||
@@ -10,25 +10,14 @@ from pypika import functions as fn
|
|||||||
|
|
||||||
from erpnext.stock.doctype.warehouse.warehouse import apply_warehouse_filter
|
from erpnext.stock.doctype.warehouse.warehouse import apply_warehouse_filter
|
||||||
|
|
||||||
SLE_COUNT_LIMIT = 10_000
|
SLE_COUNT_LIMIT = 100_000
|
||||||
|
|
||||||
|
|
||||||
def _estimate_table_row_count(doctype: str):
|
|
||||||
table = get_table_name(doctype)
|
|
||||||
return cint(
|
|
||||||
frappe.db.sql(
|
|
||||||
f"""select table_rows
|
|
||||||
from information_schema.tables
|
|
||||||
where table_name = '{table}' ;"""
|
|
||||||
)[0][0]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def execute(filters=None):
|
def execute(filters=None):
|
||||||
if not filters:
|
if not filters:
|
||||||
filters = {}
|
filters = {}
|
||||||
|
|
||||||
sle_count = _estimate_table_row_count("Stock Ledger Entry")
|
sle_count = frappe.db.estimate_count("Stock Ledger Entry")
|
||||||
|
|
||||||
if (
|
if (
|
||||||
sle_count > SLE_COUNT_LIMIT
|
sle_count > SLE_COUNT_LIMIT
|
||||||
|
|||||||
Reference in New Issue
Block a user