8000 perf: remove `order_by` from linked document checks by ankush · Pull Request #19229 · frappe/frappe · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

perf: remove order_by from linked document checks #19229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions frappe/desk/form/linked_with.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def get_link_sources(self):
def get_submittable_doctypes(self) -> list[str]:
"""Returns list of submittable doctypes."""
if not self._submittable_doctypes:
self._submittable_doctypes = frappe.get_all("DocType", {"is_submittable": 1}, pluck="name")
self._submittable_doctypes = frappe.get_all(
"DocType", {"is_submittable": 1}, pluck="name", order_by=None
)
return self._submittable_doctypes


Expand All @@ -155,13 +157,15 @@ def get_child_tables_of_doctypes(doctypes: list[str] = None):
fields=["parent", "fieldname", "options as child_table"],
filters=filters_for_docfield,
as_list=1,
order_by=None,
)

links += frappe.get_all(
"Custom Field",
fields=["dt as parent", "fieldname", "options as child_table"],
filters=filters_for_customfield,
as_list=1,
order_by=None,
)

child_tables_by_doctype = defaultdict(list)
Expand Down Expand Up @@ -275,13 +279,15 @@ def get_references_across_doctypes_by_dynamic_link_field(
fields=["parent as doctype", "fieldname", "options as doctype_fieldname"],
filters=filters_for_docfield,
as_list=1,
order_by=None,
)

links += frappe.get_all(
"Custom Field",
fields=["dt as doctype", "fieldname", "options as doctype_fieldname"],
filters=filters_for_customfield,
as_list=1,
order_by=None,
)

links_by_doctype = defaultdict(list)
Expand Down Expand Up @@ -328,17 +334,21 @@ def get_referencing_documents(

if not link_info.get("is_child"):
filters.extend(parent_filters or [])
return {from_table: frappe.get_all(from_table, filters, pluck="name")}
return {from_table: frappe.get_all(from_table, filters, pluck="name", order_by=None)}

filters.extend(child_filters or [])
res = frappe.get_all(from_table, filters=filters, fields=["name", "parenttype", "parent"])
res = frappe.get_all(
from_table, filters=filters, fields=["name", "parenttype", "parent"], order_by=None
)
documents = defaultdict(list)

for parent, rows in itertools.groupby(res, key=lambda row: row["parenttype"]):
if allowed_parents and parent not in allowed_parents:
continue
filters = (parent_filters or []) + [["name", "in", tuple(row.parent for row in rows)]]
documents[parent].extend(frappe.get_all(parent, filters=filters, pluck="name") or [])
documents[parent].extend(
frappe.get_all(parent, filters=filters, pluck="name", order_by=None) or []
)
return documents


Expand Down Expand Up @@ -447,7 +457,7 @@ def get_linked_docs(doctype: str, name: str, linkinfo: dict | None = None) -> di

try:
if link.get("filters"):
ret = frappe.get_all(doctype=dt, fields=fields, filters=link.get("filters"))
ret = frappe.get_all(doctype=dt, fields=fields, filters=link.get("filters"), order_by=None)

elif link.get("get_parent"):
ret = None
Expand All @@ -456,9 +466,11 @@ def get_linked_docs(doctype: str, name: str, linkinfo: dict | None = None) -> di
if not frappe.get_meta(doctype).istable:
continue

me = frappe.db.get_value(doctype, name, ["parenttype", "parent"], as_dict=True)
me = frappe.db.get_value(doctype, name, ["parenttype", "parent"], as_dict=True, order_by=None)
if me and me.parenttype == dt:
ret = frappe.get_all(doctype=dt, fields=fields, filters=[[dt, "name", "=", me.parent]])
ret = frappe.get_all(
doctype=dt, fields=fields, filters=[[dt, "name", "=", me.parent]], order_by=None
)

elif link.get("child_doctype"):
or_filters = [
Expand All @@ -471,7 +483,12 @@ def get_linked_docs(doctype: str, name: str, linkinfo: dict | None = None) -> di
filters.append([link.get("child_doctype"), link.get("doctype_fieldname"), "=", doctype])

ret = frappe.get_all(
doctype=dt, fields=fields, filters=filters, or_filters=or_filters, distinct=True
doctype=dt,
fields=fields,
filters=filters,
or_filters=or_filters,
distinct=True,
order_by=None,
)

else:
Expand All @@ -483,7 +500,9 @@ def get_linked_docs(doctype: str, name: str, linkinfo: dict | None = None) -> di
# dynamic link
if link.get("doctype_fieldname"):
filters.append([dt, link.get("doctype_fieldname"), "=", doctype])
ret = frappe.get_all(doctype=dt, fields=fields, filters=filters, or_filters=or_filters)
ret = frappe.get_all(
doctype=dt, fields=fields, filters=filters, or_filters=or_filters, order_by=None
)

else:
ret = None
Expand Down
0