CREATE TABLE [dbo].[mytab](
[Ino] [numeric](10, 0) NOT NULL,
[mytype] varchar NULL,
[mydate] [int] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE UNIQUE CLUSTERED INDEX [mytabin] ON [dbo].[mytab]
(
[Ino] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [t1] ON [dbo].[mytab]
(
[mytype] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
insert into mytab values(1,'751ZW',20150101)
go
insert into mytab values(2,'751ZW',0)
go
insert into mytab values(3,'751ZA',0)
go
insert into mytab values(4,'751FG',0)
go
SELECT D.mydate, mytype FROM mytab D with( index(t1))
WHERE CONVERT(DATETIME,LEFT(D.mydate,10),103) BETWEEN '01-Jan-2015' AND '01-Jan-2015'
AND D.mydate <> 0
AND RIGHT(D.mytype,2) IN ('ZW','NI','SB','SV')
AND LEFT(D.mytype,2) IN ( '75')
Please note this is a test table i created for replicating. the real time table has more than 10 million records and we cannot correct the data now. Also cannot change underlying sql Because the same sql is running fine with out index and also with the index properly in another environment
I am forcing the index here just because to show the SELECT gives error when using the index. In real time without forcing the select picks up the index automatically
I want to know
- Why an index is giving error.
2.Why it runs properly in 1 environment and not properly in another environment