'Performance strategies for selecting large amounts of data from child tables

I have a fairly standard database structure for storing invoices and their line items. We have an AccountingDocuments table for header details, and AccountingDocumentItems child table. The child table has a foreign key of AccountingDocumentId with associated non-clustered index.

Most of our Sales reports need to select almost all the columns in the child table, based on a passed date range and branch. So the basic query would be something like:

SELECT
    adi.TotalInclusive,
    adi.Description, etc.
FROM
    AccountingDocumentItems adi
LEFT JOIN AccountingDocuments ad on ad.AccountingDocumentId=adi.AccountingDocumentId
WHERE
    ad.DocumentDate > '1 Jan 2022' and ad.DocumentDate < '5 May 2022'
AND
    ad.SiteId='x'

This results in an index seek on the AccountingDocuments table, but due to the select requirements on AccountingDocumentItems we're seeing a full index scan here. I'm not certain how to index the child table given the filtering is taking place on the parent table.

At present there are only around 2m rows in the line items table, and the query performs quite well. My concern is how this will work once the table is significantly larger and what strategies there are to ensure good performance on these reports going forward.

I'm aware the answer may be to simply keep adding hardware, but want to investigate alternative strategies to this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source