Standard configuration for our production SQL servers includes a dedicated drive for tempdb, SQL server's 'scratchpad' database. Our configuration follows Microsoft's recommended best practices. With a complex query, DTA encountered the following SQL error:
The issue was caused by a FROM clause sub-query doing three things:
This essentially forced SQL to do an ORDER of the entire table in tempdb. The fix is to have the outer query and sub-query use the same index. Wrong version was: SELECT ... FROM Debt (NOLOCK) INNER JOIN Ledger (nolock) ON Ledger.LedgerID = ( ... ) INNER JOIN LedgerItem (nolock) -- this uses PK_LedgerItem ON LedgerItem.LedgerItemID = ( SELECT Top 1 LI.LedgerItemID FROM LedgerItem LI (nolock) WHERE LI.LedgerID = Ledger.LedgerID -- this uses IX_LedgerItemByLedgerID AND ... ORDER BY Li.PostedDate DESC, Li.LedgerItemID DESC )to this: SELECT ... FROM Debt (NOLOCK) INNER JOIN Ledger (nolock) ON Ledger.LedgerID = ( ... ) INNER JOIN LedgerItem (nolock) ON LedgerItem.LedgerID = Ledger.LedgerID -- this uses IX_LedgerItemByLedgerIDAND LedgerItem.LedgerItemID = ( SELECT Top 1 LI.LedgerItemID FROM LedgerItem LI (nolock) WHERE LI.LedgerID = Ledger.LedgerID -- this also uses IX_LedgerItemByLedgerID AND ISNULL(LI.LedgerTemplateItemID,1) not in (55,56,57,175) AND ...ORDER BY Li.PostedDate DESC, Li.LedgerItemID DESC ) WHERE ... |
Troubleshooting >