dvader0571
Programmer
This is bizarre to me however, hopefully someone can explain what is happening here.
When I check the execution plan of the following query:
SELECT A.TRANDTTM
FROM [imsv7].ACCTTRAN A, [imsv7].CUSTBL C
WHERE A.ACCTKEY = @dwAcctKey AND A.BLKEY = C.BLKEY
AND A.TRANDTTM >= @tsDueDate AND C.DUEDATE <= @tsDueDate
It correctly uses a Clustered index ACTTRANAX in the ACCTTRAN table for the AcctKey field.
CREATE CLUSTERED INDEX [ACTRANAX] ON [IMSV7].[ACCTTRAN]([ACCTKEY]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
However, as soon as I add this query to a Stored Procedure and check the Execution Plan, it decides to use a completely non-related index ACTRANKX instead.
CREATE UNIQUE INDEX [ACTRANKX] ON [IMSV7].[ACCTTRAN]([ACTRANNO]) ON [PRIMARY]
GO
I am frustrated with this because the 93 milliseconds is going to be a killer with 93,000 accounts it needs to process (roughly 139 minutes), as opposed to the 3 milliseconds when it uses the correct index.
Any ideas would be of great help thanks!
When I check the execution plan of the following query:
SELECT A.TRANDTTM
FROM [imsv7].ACCTTRAN A, [imsv7].CUSTBL C
WHERE A.ACCTKEY = @dwAcctKey AND A.BLKEY = C.BLKEY
AND A.TRANDTTM >= @tsDueDate AND C.DUEDATE <= @tsDueDate
It correctly uses a Clustered index ACTTRANAX in the ACCTTRAN table for the AcctKey field.
CREATE CLUSTERED INDEX [ACTRANAX] ON [IMSV7].[ACCTTRAN]([ACCTKEY]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
However, as soon as I add this query to a Stored Procedure and check the Execution Plan, it decides to use a completely non-related index ACTRANKX instead.
CREATE UNIQUE INDEX [ACTRANKX] ON [IMSV7].[ACCTTRAN]([ACTRANNO]) ON [PRIMARY]
GO
I am frustrated with this because the 93 milliseconds is going to be a killer with 93,000 accounts it needs to process (roughly 139 minutes), as opposed to the 3 milliseconds when it uses the correct index.
Any ideas would be of great help thanks!