From cdc86bd76c5e3f3db8cae9c6abcb5ad2522cb4f9 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 31 Jul 2023 17:39:31 +0530 Subject: [PATCH] perf: use `LEFT JOIN` instead of `NOT EXISTS` (backport #36221) (#36384) * perf: use `LEFT JOIN` instead of `NOT EXISTS` (cherry picked from commit 58d867503b38fb72c850a2008ec5f33455e643d0) # Conflicts: # erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py * fix: long queue `process_boms_cost_level_wise` (cherry picked from commit 148d466ae53b4754b5ea7209d9d67f6731f6a2d3) * chore: `conflicts` * fix: failing test case --------- Co-authored-by: s-aga-r --- .../doctype/bom_update_log/bom_update_log.py | 1 + .../bom_update_log/bom_updation_utils.py | 21 +++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py index 9c35e49b20b4..9365424b5a7c 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py @@ -79,6 +79,7 @@ def on_submit(self): else: frappe.enqueue( method="erpnext.manufacturing.doctype.bom_update_log.bom_update_log.process_boms_cost_level_wise", + queue="long", update_doc=self, now=frappe.flags.in_test, ) diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py index 3d52cd811bb8..d0eaa37b06be 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py @@ -157,12 +157,21 @@ def _all_children_are_processed(parent_bom): def get_leaf_boms() -> List[str]: "Get BOMs that have no dependencies." - return frappe.db.sql_list( - """select name from `tabBOM` bom - where docstatus=1 and is_active=1 - and not exists(select bom_no from `tabBOM Item` - where parent=bom.name and ifnull(bom_no, '')!='')""" - ) + bom = frappe.qb.DocType("BOM") + bom_item = frappe.qb.DocType("BOM Item") + + boms = ( + frappe.qb.from_(bom) + .left_join(bom_item) + .on((bom.name == bom_item.parent) & (bom_item.bom_no != "")) + .select(bom.name) + .where((bom.docstatus == 1) & (bom.is_active == 1) & (bom_item.bom_no.isnull())) + .distinct() + ).run(as_list=True) + + boms = [bom[0] for bom in boms] + + return boms def _generate_dependence_map() -> defaultdict: