Merge pull request #7012 from rmehta/remove-count-star

[optimize] remove count(*) from queries
This commit is contained in:
Nabin Hait 2016-11-21 15:28:01 +05:30 committed by GitHub
commit d50d6a559c
6 changed files with 11 additions and 11 deletions

View File

@ -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] no_of_months = ({"Monthly": 1, "Quarterly": 3, "Yearly": 12})[base_doc.recurring_type]
def _test(i): 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'), where recurring_id=%s and (docstatus=1 or docstatus=0)""" % (base_doc.doctype, '%s'),
(base_doc.recurring_id))[0][0]) (base_doc.recurring_id))[0][0])

View File

@ -18,6 +18,6 @@ def execute():
frappe.db.sql("""update `tabEmployee` set user_id=null frappe.db.sql("""update `tabEmployee` set user_id=null
where user_id=%s and name!=%s""", (user_id, employee)) where user_id=%s and name!=%s""", (user_id, employee))
else: 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 frappe.db.sql("""update `tabEmployee` set user_id=null
where user_id=%s limit %s""", (user_id, count - 1)) where user_id=%s limit %s""", (user_id, count - 1))

View File

@ -8,7 +8,7 @@ from frappe import _
@frappe.whitelist() @frappe.whitelist()
def get_funnel_data(from_date, to_date): 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) where (date(`modified`) between %s and %s)
and status != "Do Not Contact" """, (from_date, to_date))[0][0] 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) where (date(`modified`) between %s and %s)
and status != "Passive" """, (from_date, to_date))[0][0] 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) where (date(`creation`) between %s and %s)
and status != "Lost" """, (from_date, to_date))[0][0] 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) where docstatus = 1 and (date(`creation`) between %s and %s)
and status != "Lost" """, (from_date, to_date))[0][0] 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] where docstatus = 1 and (date(`creation`) between %s and %s)""", (from_date, to_date))[0][0]
return [ return [

View File

@ -177,7 +177,7 @@ class EmailDigest(Document):
if not user_id: if not user_id:
user_id = frappe.session.user 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)""", where status='Open' and (owner=%s or assigned_by=%s)""",
(user_id, user_id))[0][0] (user_id, user_id))[0][0]
@ -202,7 +202,7 @@ class EmailDigest(Document):
def get_issue_count(self): def get_issue_count(self):
"""Get count of Issue""" """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] where status in ('Open','Replied') """)[0][0]
def get_project_list(self, user_id=None): def get_project_list(self, user_id=None):
@ -221,7 +221,7 @@ class EmailDigest(Document):
def get_project_count(self): def get_project_count(self):
"""Get count of Project""" """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] where status='Open' and project_type='External'""")[0][0]
def set_accounting_cards(self, context): def set_accounting_cards(self, context):

View File

@ -127,7 +127,7 @@ def get_item_for_list_in_html(context):
def get_group_item_count(item_group): def get_group_item_count(item_group):
child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(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 where docstatus = 0 and show_in_website = 1
and (item_group in (%s) and (item_group in (%s)
or name in (select parent from `tabWebsite Item Group` or name in (select parent from `tabWebsite Item Group`

View File

@ -23,7 +23,7 @@ def boot_session(bootinfo):
"Notification Control") "Notification Control")
# if no company, show a dialog box to create a new company # 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: if not bootinfo.customer_count:
bootinfo.setup_complete = frappe.db.sql("""select name from bootinfo.setup_complete = frappe.db.sql("""select name from