perf: drop all old parent indexes

- re-add parent index on custom_docperm
This commit is contained in:
Ankush Menat 2022-09-14 00:58:35 +05:30 committed by Ankush Menat
parent 3a5a45d8af
commit 7453e8e3a1
4 changed files with 60 additions and 1 deletions

View file

@ -36,6 +36,7 @@ class ActivityLog(Document):
def on_doctype_update():
"""Add indexes in `tabActivity Log`"""
frappe.db.add_index("Activity Log", ["reference_doctype", "reference_name"])
frappe.db.add_index("Activity Log", ["timeline_doctype", "timeline_name"])
def add_authentication_log(subject, user, operation="Login", status="Success"):

View file

@ -212,7 +212,8 @@
"fieldname": "parent",
"fieldtype": "Data",
"label": "Reference Document Type",
"read_only": 1
"read_only": 1,
"search_index": 1
},
{
"default": "0",

View file

@ -211,3 +211,4 @@ frappe.patches.v14_0.set_suspend_email_queue_default
frappe.patches.v14_0.different_encryption_key
frappe.patches.v14_0.update_multistep_webforms
execute:frappe.delete_doc('Page', 'background_jobs', ignore_missing=True, force=True)
frappe.patches.v14_0.drop_unused_indexes

View file

@ -0,0 +1,56 @@
"""
This patch just drops some known indexes which aren't being used anymore or never were used.
"""
import click
import frappe
UNUSED_INDEXES = [
("Comment", ["link_doctype", "link_name"]),
("Activity Log", ["link_doctype", "link_name"]),
]
def execute():
if frappe.db.db_type == "postgres":
return
db_tables = frappe.db.get_tables(cached=False)
# All parent indexes
parent_doctypes = frappe.get_all(
"DocType",
{"istable": 0, "is_virtual": 0, "issingle": 0},
pluck="name",
)
db_tables = frappe.db.get_tables(cached=False)
for doctype in parent_doctypes:
table = f"tab{doctype}"
if table not in db_tables:
continue
_drop_index_if_exists(table, "parent")
# Unused composite indexes
for doctype, index_fields in UNUSED_INDEXES:
table = f"tab{doctype}"
index_name = frappe.db.get_index_name(index_fields)
if table not in db_tables:
continue
_drop_index_if_exists(table, index_name)
def _drop_index_if_exists(table: str, index: str):
if not frappe.db.has_index(table, index):
click.echo(f"- Skipped {index} index for {table} because it doesn't exist")
return
try:
frappe.db.sql_ddl(f"ALTER TABLE `{table}` DROP INDEX `{index}`")
except Exception as e:
frappe.log_error("Failed to drop index")
click.secho(f"x Failed to drop index {index} from {table}\n {str(e)}", fg="red")
return
click.echo(f"✓ dropped {index} index from {table}")