-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…8913) perf: Drop unused/duplicate/sub-optimal indexes (#38884) * ci: enable more checks * perf: Drop unused/duplicate indexes (cherry picked from commit 7873338) Co-authored-by: Ankush Menat <ankush@frappe.io>
- Loading branch information
1 parent
a99470e
commit 308c6ff
Showing
9 changed files
with
56 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 20 additions & 8 deletions
28
erpnext/stock/doctype/delivery_note/patches/drop_unused_return_against_index.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,27 @@ | ||
import click | ||
import frappe | ||
|
||
UNUSED_INDEXES = [ | ||
("Delivery Note", ["customer", "is_return", "return_against"]), | ||
("Sales Invoice", ["customer", "is_return", "return_against"]), | ||
("Purchase Invoice", ["supplier", "is_return", "return_against"]), | ||
("Purchase Receipt", ["supplier", "is_return", "return_against"]), | ||
] | ||
|
||
|
||
def execute(): | ||
"""Drop unused return_against index""" | ||
for doctype, index_fields in UNUSED_INDEXES: | ||
table = f"tab{doctype}" | ||
index_name = frappe.db.get_index_name(index_fields) | ||
drop_index_if_exists(table, index_name) | ||
|
||
|
||
def drop_index_if_exists(table: str, index: str): | ||
if not frappe.db.has_index(table, index): | ||
return | ||
|
||
try: | ||
frappe.db.sql_ddl( | ||
"ALTER TABLE `tabDelivery Note` DROP INDEX `customer_is_return_return_against_index`" | ||
) | ||
frappe.db.sql_ddl( | ||
"ALTER TABLE `tabPurchase Receipt` DROP INDEX `supplier_is_return_return_against_index`" | ||
) | ||
frappe.db.sql_ddl(f"ALTER TABLE `{table}` DROP INDEX `{index}`") | ||
click.echo(f"✓ dropped {index} index from {table}") | ||
except Exception: | ||
frappe.log_error("Failed to drop unused index") | ||
frappe.log_error("Failed to drop index") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import frappe | ||
from frappe.tests.utils import FrappeTestCase | ||
|
||
INDEXED_FIELDS = { | ||
"Bin": ["item_code"], | ||
"GL Entry": ["voucher_type", "against_voucher_type"], | ||
"Purchase Order Item": ["item_code"], | ||
"Stock Ledger Entry": ["warehouse"], | ||
} | ||
|
||
|
||
class TestPerformance(FrappeTestCase): | ||
def test_ensure_indexes(self): | ||
# These fields are not explicitly indexed BUT they are prefix in some | ||
# other composite index. If those are removed this test should be | ||
# updated accordingly. | ||
for doctype, fields in INDEXED_FIELDS.items(): | ||
for field in fields: | ||
self.assertTrue( | ||
frappe.db.sql( | ||
f"""SHOW INDEX FROM `tab{doctype}` | ||
WHERE Column_name = "{field}" AND Seq_in_index = 1""" | ||
) | ||
) |