fix: improved indexing for SLE queries. (backport #47194) (#47822)

* fix: improved indexing for SLE queries. (#47194)

(cherry picked from commit b49a835b4c)

# Conflicts:
#	erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.json

* chore: fix conflicts

---------

Co-authored-by: rohitwaghchaure <rohitw1991@gmail.com>
This commit is contained in:
mergify[bot]
2025-05-30 15:36:51 +05:30
committed by GitHub
parent 9df3b9b059
commit 3879cbd86d
11 changed files with 31 additions and 19 deletions

View File

@@ -407,3 +407,4 @@ erpnext.patches.v14_0.set_update_price_list_based_on
erpnext.patches.v15_0.set_cancelled_status_to_cancelled_pos_invoice
erpnext.patches.v15_0.rename_group_by_to_categorize_by_in_custom_reports
erpnext.patches.v14_0.update_full_name_in_contract
erpnext.patches.v15_0.drop_sle_indexes

View File

@@ -0,0 +1,17 @@
import click
import frappe
def execute():
table = "tabStock Ledger Entry"
index_list = ["posting_datetime_creation_index", "item_warehouse"]
for index in index_list:
if not frappe.db.has_index(table, index):
continue
try:
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 index")

View File

@@ -57,7 +57,6 @@
"options": "Item",
"print_width": "100px",
"read_only": 1,
"search_index": 1,
"width": "100px"
},
{
@@ -88,7 +87,6 @@
"options": "Warehouse",
"print_width": "100px",
"read_only": 1,
"search_index": 1,
"width": "100px"
},
{
@@ -101,7 +99,6 @@
"oldfieldtype": "Date",
"print_width": "100px",
"read_only": 1,
"search_index": 1,
"width": "100px"
},
{
@@ -357,13 +354,14 @@
"search_index": 1
}
],
"grid_page_length": 50,
"hide_toolbar": 1,
"icon": "fa fa-list",
"idx": 1,
"in_create": 1,
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-12-23 18:03:05.171023",
"modified": "2025-04-22 12:37:41.304109",
"modified_by": "Administrator",
"module": "Stock",
"name": "Stock Ledger Entry",

View File

@@ -345,5 +345,4 @@ class StockLedgerEntry(Document):
def on_doctype_update():
frappe.db.add_index("Stock Ledger Entry", ["voucher_no", "voucher_type"])
frappe.db.add_index("Stock Ledger Entry", ["batch_no", "item_code", "warehouse"])
frappe.db.add_index("Stock Ledger Entry", ["warehouse", "item_code"], "item_warehouse")
frappe.db.add_index("Stock Ledger Entry", ["posting_datetime", "creation"])
frappe.db.add_index("Stock Ledger Entry", ["item_code", "warehouse", "posting_datetime", "creation"])

View File

@@ -6,7 +6,7 @@ from collections import defaultdict
import frappe
from frappe import _
from frappe.query_builder.functions import Sum
from frappe.utils import flt, today
from frappe.utils import flt, get_datetime, today
def execute(filters=None):
@@ -167,7 +167,8 @@ def get_query_based_on_filters(query, batch, table, filters):
query = query.where(batch.batch_qty > 0)
else:
query = query.where(table.posting_date <= filters.to_date)
to_date = get_datetime(str(filters.to_date) + " 23:59:59")
query = query.where(table.posting_datetime <= to_date)
if filters.warehouse:
lft, rgt = frappe.db.get_value("Warehouse", filters.warehouse, ["lft", "rgt"])

View File

@@ -115,7 +115,6 @@ def get_stock_ledger_entries_for_batch_no(filters):
& (sle.posting_datetime < posting_datetime)
)
.groupby(sle.voucher_no, sle.batch_no, sle.item_code, sle.warehouse)
.orderby(sle.item_code, sle.warehouse)
)
query = apply_warehouse_filter(query, sle, filters)
@@ -160,7 +159,6 @@ def get_stock_ledger_entries_for_batch_bundle(filters):
& (sle.posting_datetime <= to_date)
)
.groupby(sle.voucher_no, batch_package.batch_no, batch_package.warehouse)
.orderby(sle.item_code, sle.warehouse)
)
query = apply_warehouse_filter(query, sle, filters)

View File

@@ -66,7 +66,7 @@ def get_stock_ledger_entries(filters):
"Stock Ledger Entry",
fields=SLE_FIELDS,
filters=sle_filters,
order_by="timestamp(posting_date, posting_time), creation",
order_by="posting_datetime, creation",
)

View File

@@ -5,8 +5,8 @@ import datetime
import frappe
from frappe import _, scrub
from frappe.query_builder.functions import CombineDatetime
from frappe.utils import get_datetime, get_first_day_of_week, get_quarter_start, getdate
from frappe.utils import get_first_day as get_first_day_of_month
from frappe.utils import get_first_day_of_week, get_quarter_start, getdate
from frappe.utils.nestedset import get_descendants_of
from erpnext.accounts.utils import get_fiscal_year
@@ -294,9 +294,8 @@ def get_stock_ledger_entries(filters, items):
sle.batch_no,
)
.where((sle.docstatus < 2) & (sle.is_cancelled == 0))
.orderby(CombineDatetime(sle.posting_date, sle.posting_time))
.orderby(sle.posting_datetime)
.orderby(sle.creation)
.orderby(sle.actual_qty)
)
if items:
@@ -314,7 +313,8 @@ def apply_conditions(query, filters):
frappe.throw(_("'From Date' is required"))
if to_date := filters.get("to_date"):
query = query.where(sle.posting_date <= to_date)
to_date = get_datetime(str(to_date) + " 23:59:59")
query = query.where(sle.posting_datetime <= to_date)
else:
frappe.throw(_("'To Date' is required"))

View File

@@ -44,7 +44,7 @@ def get_stock_ledger_entries(filters):
"Stock Ledger Entry",
fields=SLE_FIELDS,
filters={"item_code": filters.item_code, "warehouse": filters.warehouse, "is_cancelled": 0},
order_by="timestamp(posting_date, posting_time), creation",
order_by="posting_datetime, creation",
)

View File

@@ -1818,7 +1818,7 @@ def get_valuation_rate(
# Get valuation rate from last sle for the same item and warehouse
if last_valuation_rate := frappe.db.sql( # nosemgrep
"""select valuation_rate
from `tabStock Ledger Entry` force index (item_warehouse)
from `tabStock Ledger Entry`
where
item_code = %s
AND warehouse = %s
@@ -1962,7 +1962,6 @@ def get_next_stock_reco(kwargs):
sle.actual_qty,
sle.has_batch_no,
)
.force_index("item_warehouse")
.where(
(sle.item_code == kwargs.get("item_code"))
& (sle.warehouse == kwargs.get("warehouse"))

View File

@@ -5,7 +5,6 @@ INDEXED_FIELDS = {
"Bin": ["item_code"],
"GL Entry": ["voucher_no", "posting_date", "company", "party"],
"Purchase Order Item": ["item_code"],
"Stock Ledger Entry": ["warehouse"],
}