Reputation: 1708
I have a simple query:
Select Distinct BOLTYPENAME, BOLTYPE.BOLTYPE From BOLTYPE
Inner Join WORKORDER on WORKORDER.BOLTYPE=BOLTYPE.BOLTYPE
Inner Join BOLMAIN On BOLMAIN.BOLID=WORKORDER.BOLID
Where BOLMAIN.CORID=156
When I run this query without the "Where" clause, it takes 0.1 secs. But adding the where clause causes it to take 1 minute to return. All tables have relevant indexes and they have been de-fragmented. The number of rows in the three tables is:
BOLTYPE: 11 rows
BOLMAIN: 71,455 rows
WORKORDER: 197,500 rows
Here are the execution plans:
Without the Where Clause (0.1 sec):
With the Where Clause (60 sec):
Any idea as to what could be the issue?
Update: Here are the relevant Index definitions:
CREATE NONCLUSTERED INDEX [BOLIDX] ON [dbo].[WORKORDER]
([BOLID] ASC)
GO
CREATE NONCLUSTERED INDEX [CORIDX] ON [dbo].[BOLMAIN]
([CORID] ASC)
INCLUDE ([BOLID])
GO
CREATE NONCLUSTERED INDEX [BOLTYPEIDX] ON [dbo].[WORKORDER]
([BOLTYPE] ASC)
GO
Upvotes: 4
Views: 121
Reputation: 31785
Recreate the CORIDX index so it covers BOLID. You're joining on BOLID, so you want it to be part of the index, not just one of the included columns.
In other words:
CREATE NONCLUSTERED INDEX [CORIDX] ON [dbo].[BOLMAIN]
([CORID] ASC, [BOLID] ASC)
Upvotes: 1