From 5a258cbc2dc33c88d0746564f1ad95176484ddd5 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Mon, 21 Nov 2016 14:57:36 +0530 Subject: [PATCH] [optimize] remove count(*) from queries --- erpnext/controllers/tests/test_recurring_document.py | 2 +- erpnext/patches/v4_0/fix_employee_user_id.py | 2 +- erpnext/selling/page/sales_funnel/sales_funnel.py | 8 ++++---- erpnext/setup/doctype/email_digest/email_digest.py | 6 +++--- erpnext/setup/doctype/item_group/item_group.py | 2 +- erpnext/startup/boot.py | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/erpnext/controllers/tests/test_recurring_document.py b/erpnext/controllers/tests/test_recurring_document.py index d47c5c7701..58558c4a8e 100644 --- a/erpnext/controllers/tests/test_recurring_document.py +++ b/erpnext/controllers/tests/test_recurring_document.py @@ -110,7 +110,7 @@ def _test_recurring_document(obj, base_doc, date_field, first_and_last_day): no_of_months = ({"Monthly": 1, "Quarterly": 3, "Yearly": 12})[base_doc.recurring_type] def _test(i): - obj.assertEquals(i+1, frappe.db.sql("""select count(*) from `tab%s` + obj.assertEquals(i+1, frappe.db.sql("""select count(name) from `tab%s` where recurring_id=%s and (docstatus=1 or docstatus=0)""" % (base_doc.doctype, '%s'), (base_doc.recurring_id))[0][0]) diff --git a/erpnext/patches/v4_0/fix_employee_user_id.py b/erpnext/patches/v4_0/fix_employee_user_id.py index 6f449f97bb..2a8f76b9cb 100644 --- a/erpnext/patches/v4_0/fix_employee_user_id.py +++ b/erpnext/patches/v4_0/fix_employee_user_id.py @@ -18,6 +18,6 @@ def execute(): frappe.db.sql("""update `tabEmployee` set user_id=null where user_id=%s and name!=%s""", (user_id, employee)) else: - count = frappe.db.sql("""select count(*) from `tabEmployee` where user_id=%s""", user_id)[0][0] + count = frappe.db.sql("""select count(name) from `tabEmployee` where user_id=%s""", user_id)[0][0] frappe.db.sql("""update `tabEmployee` set user_id=null where user_id=%s limit %s""", (user_id, count - 1)) diff --git a/erpnext/selling/page/sales_funnel/sales_funnel.py b/erpnext/selling/page/sales_funnel/sales_funnel.py index 4d12efd99b..71dc8bd05c 100644 --- a/erpnext/selling/page/sales_funnel/sales_funnel.py +++ b/erpnext/selling/page/sales_funnel/sales_funnel.py @@ -8,7 +8,7 @@ from frappe import _ @frappe.whitelist() def get_funnel_data(from_date, to_date): - active_leads = frappe.db.sql("""select count(*) from `tabLead` + active_leads = frappe.db.sql("""select count(name) from `tabLead` where (date(`modified`) between %s and %s) and status != "Do Not Contact" """, (from_date, to_date))[0][0] @@ -16,15 +16,15 @@ def get_funnel_data(from_date, to_date): where (date(`modified`) between %s and %s) and status != "Passive" """, (from_date, to_date))[0][0] - opportunities = frappe.db.sql("""select count(*) from `tabOpportunity` + opportunities = frappe.db.sql("""select count(name) from `tabOpportunity` where (date(`creation`) between %s and %s) and status != "Lost" """, (from_date, to_date))[0][0] - quotations = frappe.db.sql("""select count(*) from `tabQuotation` + quotations = frappe.db.sql("""select count(name) from `tabQuotation` where docstatus = 1 and (date(`creation`) between %s and %s) and status != "Lost" """, (from_date, to_date))[0][0] - sales_orders = frappe.db.sql("""select count(*) from `tabSales Order` + sales_orders = frappe.db.sql("""select count(name) from `tabSales Order` where docstatus = 1 and (date(`creation`) between %s and %s)""", (from_date, to_date))[0][0] return [ diff --git a/erpnext/setup/doctype/email_digest/email_digest.py b/erpnext/setup/doctype/email_digest/email_digest.py index ac999ed23b..791e54e370 100644 --- a/erpnext/setup/doctype/email_digest/email_digest.py +++ b/erpnext/setup/doctype/email_digest/email_digest.py @@ -177,7 +177,7 @@ class EmailDigest(Document): if not user_id: user_id = frappe.session.user - return frappe.db.sql("""select count(*) from `tabToDo` + return frappe.db.sql("""select count(name) from `tabToDo` where status='Open' and (owner=%s or assigned_by=%s)""", (user_id, user_id))[0][0] @@ -202,7 +202,7 @@ class EmailDigest(Document): def get_issue_count(self): """Get count of Issue""" - return frappe.db.sql("""select count(*) from `tabIssue` + return frappe.db.sql("""select count(name) from `tabIssue` where status in ('Open','Replied') """)[0][0] def get_project_list(self, user_id=None): @@ -221,7 +221,7 @@ class EmailDigest(Document): def get_project_count(self): """Get count of Project""" - return frappe.db.sql("""select count(*) from `tabProject` + return frappe.db.sql("""select count(name) from `tabProject` where status='Open' and project_type='External'""")[0][0] def set_accounting_cards(self, context): diff --git a/erpnext/setup/doctype/item_group/item_group.py b/erpnext/setup/doctype/item_group/item_group.py index 12899cddd0..e63816168c 100644 --- a/erpnext/setup/doctype/item_group/item_group.py +++ b/erpnext/setup/doctype/item_group/item_group.py @@ -127,7 +127,7 @@ def get_item_for_list_in_html(context): def get_group_item_count(item_group): child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(item_group)]) - return frappe.db.sql("""select count(*) from `tabItem` + return frappe.db.sql("""select count(name) from `tabItem` where docstatus = 0 and show_in_website = 1 and (item_group in (%s) or name in (select parent from `tabWebsite Item Group` diff --git a/erpnext/startup/boot.py b/erpnext/startup/boot.py index 97ef3299a3..9b16ad79ef 100644 --- a/erpnext/startup/boot.py +++ b/erpnext/startup/boot.py @@ -23,7 +23,7 @@ def boot_session(bootinfo): "Notification Control") # if no company, show a dialog box to create a new company - bootinfo.customer_count = frappe.db.sql("""select count(*) from tabCustomer""")[0][0] + bootinfo.customer_count = frappe.db.sql("""select count(name) from tabCustomer""")[0][0] if not bootinfo.customer_count: bootinfo.setup_complete = frappe.db.sql("""select name from