Finding Linked Documents Extremely Slow

Hi all!

I am learning more and more about Frappe and ERPNext as I go. While building a Delivery Trip Builder (coordinating many Delivery Notes to many Delivery Trips on a given day), I have needed to pull Delivery Notes from Sales Orders with a specific Delivery Date.

This is the code in my app’s API:

import frappe
from frappe.desk.form.linked_with import get_linked_docs

def get_notes_to_deliver(day):
    sales_orders = frappe.get_all('Sales Order', filters={
        'delivery_date': day,  # Day was chosen in frontend
        'status': 'To Bill'         # Delivery Note was made
    }, fields=["name"])

    delivery_notes = []
    for sales_order in sales_orders:
        docs = get_linked_docs('Sales Order',, linkinfo={
            'Delivery Note': {
                'child_doctype': 'Delivery Note Item',
                'fieldname': [ 'against_sales_order' ]
            } # this dict was cached from get_linked_doctypes
        }, for_doctype="Delivery Note")

        for note in docs['Delivery Note']:
                frappe.get_doc('Delivery Note',

    return delivery_notes

I used the code from this post about linked docs.

Of course this operation cannot be fast, since it is a many-to-many-to-many relationship (Sales Order > Delivery Note Item > Delivery Note). However, is this the correct way to do this? A request with <10 Sales Orders take upwards of 3-4 seconds to process; we are hoping to organize 150+ deliveries PER DAY with this tool. I feel like aggregating Delivery Notes from Sales Orders should be a smoother process.

Any help would be much appreciated!