浏览代码

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

[optimize] remove count(*) from queries
version-14
Nabin Hait 8 年前
committed by GitHub
父节点
当前提交
e26e80cf5c
共有 15 个文件被更改,包括 21 次插入21 次删除
  1. +1
    -1
      frappe/boot.py
  2. +4
    -4
      frappe/core/doctype/user/user.py
  3. +2
    -2
      frappe/core/notifications.py
  4. +1
    -1
      frappe/custom/doctype/custom_field/custom_field.py
  5. +2
    -2
      frappe/database.py
  6. +1
    -1
      frappe/desk/doctype/desktop_icon/desktop_icon.py
  7. +1
    -1
      frappe/desk/page/chat/chat.py
  8. +1
    -1
      frappe/email/doctype/auto_email_report/auto_email_report.py
  9. +1
    -1
      frappe/email/doctype/email_group/email_group.py
  10. +1
    -1
      frappe/patches/v6_1/rename_file_data.py
  11. +1
    -1
      frappe/print/doctype/letter_head/letter_head.py
  12. +1
    -1
      frappe/utils/bot.py
  13. +2
    -2
      frappe/utils/nestedset.py
  14. +1
    -1
      frappe/website/doctype/blog_post/blog_post.py
  15. +1
    -1
      frappe/www/contact.py

+ 1
- 1
frappe/boot.py 查看文件

@@ -94,7 +94,7 @@ def get_allowed_pages():
# pages where role is not set are also allowed
for p in frappe.db.sql("""select name, modified, title
from `tabPage` where
(select count(*) from `tabPage Role`
(select count(name) from `tabPage Role`
where `tabPage Role`.parent=tabPage.name) = 0""", as_dict=1):

page_info[p.name] = {"modified":p.modified, "title":p.title}


+ 4
- 4
frappe/core/doctype/user/user.py 查看文件

@@ -586,7 +586,7 @@ def sign_up(email, full_name, redirect_to):
else:
return _("Already Registered")
else:
if frappe.db.sql("""select count(*) from tabUser where
if frappe.db.sql("""select count(name) from tabUser where
HOUR(TIMEDIFF(CURRENT_TIMESTAMP, TIMESTAMP(modified)))=1""")[0][0] > 300:

frappe.respond_as_web_page(_('Temperorily Disabled'),
@@ -676,19 +676,19 @@ def get_system_users(exclude_users=None, limit=None):

def get_active_users():
"""Returns No. of system users who logged in, in the last 3 days"""
return frappe.db.sql("""select count(*) from `tabUser`
return frappe.db.sql("""select count(name) from `tabUser`
where enabled = 1 and user_type != 'Website User'
and name not in ({})
and hour(timediff(now(), last_active)) < 72""".format(", ".join(["%s"]*len(STANDARD_USERS))), STANDARD_USERS)[0][0]

def get_website_users():
"""Returns total no. of website users"""
return frappe.db.sql("""select count(*) from `tabUser`
return frappe.db.sql("""select count(name) from `tabUser`
where enabled = 1 and user_type = 'Website User'""")[0][0]

def get_active_website_users():
"""Returns No. of website users who logged in, in the last 3 days"""
return frappe.db.sql("""select count(*) from `tabUser`
return frappe.db.sql("""select count(name) from `tabUser`
where enabled = 1 and user_type = 'Website User'
and hour(timediff(now(), last_active)) < 72""")[0][0]



+ 2
- 2
frappe/core/notifications.py 查看文件

@@ -44,7 +44,7 @@ def get_todays_events(as_list=False):
def get_unread_messages():
"returns unread (docstatus-0 messages for a user)"
return frappe.db.sql("""\
SELECT count(*)
select count(name)
FROM `tabCommunication`
WHERE communication_type in ('Chat', 'Notification')
AND reference_doctype = 'User'
@@ -55,7 +55,7 @@ def get_unread_messages():

def get_unseen_likes():
"""Returns count of unseen likes"""
return frappe.db.sql("""select count(*) from `tabCommunication`
return frappe.db.sql("""select count(name) from `tabCommunication`
where
communication_type='Comment'
and modified >= DATE_SUB(NOW(),INTERVAL 1 YEAR)


+ 1
- 1
frappe/custom/doctype/custom_field/custom_field.py 查看文件

@@ -77,7 +77,7 @@ def get_fields_label(doctype=None):
def create_custom_field_if_values_exist(doctype, df):
df = frappe._dict(df)
if df.fieldname in frappe.db.get_table_columns(doctype) and \
frappe.db.sql("""select count(*) from `tab{doctype}`
frappe.db.sql("""select count(name) from `tab{doctype}`
where ifnull({fieldname},'')!=''""".format(doctype=doctype, fieldname=df.fieldname))[0][0]:

create_custom_field(doctype, df)


+ 2
- 2
frappe/database.py 查看文件

@@ -777,10 +777,10 @@ class Database:
"""Returns `COUNT(*)` for given DocType and filters."""
if filters:
conditions, filters = self.build_conditions(filters)
return frappe.db.sql("""select count(*)
return frappe.db.sql("""select count(name)
from `tab%s` where %s""" % (dt, conditions), filters, debug=debug)[0][0]
else:
return frappe.db.sql("""select count(*)
return frappe.db.sql("""select count(name)
from `tab%s`""" % (dt,))[0][0]




+ 1
- 1
frappe/desk/doctype/desktop_icon/desktop_icon.py 查看文件

@@ -110,7 +110,7 @@ def add_user_icon(_doctype, label=None, link=None, type='link', standard=0):
else:
idx = frappe.db.sql('select max(idx) from `tabDesktop Icon` where owner=%s',
frappe.session.user)[0][0] or \
frappe.db.sql('select count(*) from `tabDesktop Icon` where standard=1')[0][0]
frappe.db.sql('select count(name) from `tabDesktop Icon` where standard=1')[0][0]

module = frappe.db.get_value('DocType', _doctype, 'module')
module_icon = frappe.get_value('Desktop Icon', {'standard':1, 'module_name':module},


+ 1
- 1
frappe/desk/page/chat/chat.py 查看文件

@@ -61,7 +61,7 @@ def get_list(arg=None):
@frappe.whitelist()
def get_active_users():
data = frappe.db.sql("""select name,
(select count(*) from tabSessions where user=tabUser.name
(select count(name) from tabSessions where user=tabUser.name
and timediff(now(), lastupdate) < time("01:00:00")) as has_session
from tabUser
where enabled=1 and


+ 1
- 1
frappe/email/doctype/auto_email_report/auto_email_report.py 查看文件

@@ -35,7 +35,7 @@ class AutoEmailReport(Document):

def validate_report_count(self):
'''check that there are only 3 enabled reports per user'''
count = frappe.db.sql('select count(*) from `tabAuto Email Report` where user=%s and enabled=1', self.user)[0][0]
count = frappe.db.sql('select count(name) from `tabAuto Email Report` where user=%s and enabled=1', self.user)[0][0]
if count > max_reports_per_user:
frappe.throw(_('Only {0} emailed reports are allowed per user').format(max_reports_per_user))



+ 1
- 1
frappe/email/doctype/email_group/email_group.py 查看文件

@@ -49,7 +49,7 @@ class EmailGroup(Document):
return self.total_subscribers

def get_total_subscribers(self):
return frappe.db.sql("""select count(*) from `tabEmail Group Member`
return frappe.db.sql("""select count(name) from `tabEmail Group Member`
where email_group=%s""", self.name)[0][0]

def on_trash(self):


+ 1
- 1
frappe/patches/v6_1/rename_file_data.py 查看文件

@@ -29,7 +29,7 @@ def execute():

# reset file size
for folder in frappe.db.sql("""select name from tabFile f1 where is_folder = 1 and
(select count(*) from tabFile f2 where f2.folder = f1.name and f2.is_folder = 1) = 0"""):
(select count(name) from tabFile f2 where f2.folder = f1.name and f2.is_folder = 1) = 0"""):
folder = frappe.get_doc("File", folder[0])
folder.save()



+ 1
- 1
frappe/print/doctype/letter_head/letter_head.py 查看文件

@@ -10,7 +10,7 @@ from frappe.model.document import Document
class LetterHead(Document):
def validate(self):
if not self.is_default:
if not frappe.db.sql("""select count(*) from `tabLetter Head` where ifnull(is_default,0)=1"""):
if not frappe.db.sql("""select count(name) from `tabLetter Head` where ifnull(is_default,0)=1"""):
self.is_default = 1

def on_update(self):


+ 1
- 1
frappe/utils/bot.py 查看文件

@@ -88,7 +88,7 @@ class CountBot(BotParser):
if self.startswith('how many'):
self.tables = self.reply.identify_tables(self.query.split(None, 1)[1])
if self.tables:
return str(frappe.db.sql('select count(*) from `tab{0}`'.format(self.get_doctype()))[0][0])
return str(frappe.db.sql('select count(name) from `tab{0}`'.format(self.get_doctype()))[0][0])

class FindBot(BotParser):
def get_reply(self):


+ 2
- 2
frappe/utils/nestedset.py 查看文件

@@ -224,7 +224,7 @@ class NestedSet(Document):

def validate_one_root(self):
if not self.get(self.nsm_parent_field):
if frappe.db.sql("""select count(*) from `tab%s` where
if frappe.db.sql("""select count(name) from `tab%s` where
ifnull(%s, '')=''""" % (self.doctype, self.nsm_parent_field))[0][0] > 1:
frappe.throw(_("""Multiple root nodes not allowed."""), NestedSetMultipleRootsError)

@@ -240,7 +240,7 @@ class NestedSet(Document):
def get_root_of(doctype):
"""Get root element of a DocType with a tree structure"""
return frappe.db.sql("""select t1.name from `tab{0}` t1 where
(select count(*) from `tab{1}` t2 where
(select count(name) from `tab{1}` t2 where
t2.lft < t1.lft and t2.rgt > t1.rgt) = 0""".format(doctype, doctype))[0][0]

def get_ancestors_of(doctype, name):


+ 1
- 1
frappe/website/doctype/blog_post/blog_post.py 查看文件

@@ -41,7 +41,7 @@ class BlogPost(WebsiteGenerator):
self.published_on = today()

# update posts
frappe.db.sql("""update tabBlogger set posts=(select count(*) from `tabBlog Post`
frappe.db.sql("""update tabBlogger set posts=(select count(name) from `tabBlog Post`
where ifnull(blogger,'')=tabBlogger.name)
where name=%s""", (self.blogger,))



+ 1
- 1
frappe/www/contact.py 查看文件

@@ -38,7 +38,7 @@ def send_message(subject="Website Query", message="", sender=""):
return

# guest method, cap max writes per hour
if frappe.db.sql("""select count(*) from `tabCommunication`
if frappe.db.sql("""select count(name) from `tabCommunication`
where `sent_or_received`="Received"
and TIMEDIFF(%s, modified) < '01:00:00'""", now())[0][0] > max_communications_per_hour:
frappe.response["message"] = "Sorry: we believe we have received an unreasonably high number of requests of this kind. Please try later"


正在加载...
取消
保存