How could I optimize my 2 queries. They execute very slow in my database. Any help is appreciated.
INSERT INTO InventoryDetails(InventoryID, AttributeKeyID, LSSMIN)
select '194572', 1, LSS.AttributeKey from TransactionLogs TL INNER JOIN LSSInventory LSS ON LSS.SAPSONumber = TL.SAP Where TL.SAP = '5001044323'
INSERT INTO StockMovement(InventoryDetailID, MovementCode, DateCreated,CompanyID,StatusID)
SELECT InventoryDetailID, 'WHRD', GETDATE(),'41',4 FROM InventoryDetails WHERE InventoryID = '194572'
There are two queries. Both are slow ? Try to execute separately.
And inside query, the select part it's slow?
If yes, do you have the right indexes defined on it?
This tables are heap tables or they have a clustered index on it?
At the first look, index on SAPSONumber column (with including AttributeKey) and on SAP column
For the second query, on InventoryID - probably this one already exists
The select part makes the execution very slow. Is there any way to optimize it?
Take a look at the execution plan for the select part
Also , if it's allowed add indexes ; see below
SAP field how it's defined in the table ? from where clause , this it's a string value '5001044323' , but it could be defined as integer/numeric value
TL.SAP is defined as string, same as LSS.SAPSONumber.