I have a table with 15 million rows. There are 5 columns. There is a clustered index on 2 columns.I have to delete about 30 rows every hour.The deletion takes 30 mins. So I get help from our SQL guru and he explains how to do the deletion and used the index. To test this new query/method I do a Select Into a new table. Of course the new table does not have any indexes due to the method it was created. I run the new query anyway, on the new table, and it finishes in 2 secs flat (there are 15million rows in it). I run it on the original table and it still takes 30 mins... with the old query or with the new query. I don't understand why it is so fast with the table with no index but a very long time on the original table with the index. I have added the table creation scripts below and the query. Can anyone explain this?
Original table:USE [Reporting]GO/****** Object: Table [dbo].[TagDataDaily] Script Date: 05/03/2010 12:41:30 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[TagDataDaily]( [Tagkey] [int] NOT NULL CONSTRAINT [DF_TagDataDaily_Tagkey] DEFAULT (0), [DateTime] [smalldatetime] NOT NULL, [Value] [real] NULL, [Quality] [tinyint] NULL, [RevLevel] [tinyint] NULL, CONSTRAINT [NoDailyDuplicates] UNIQUE NONCLUSTERED ( [Tagkey] ASC, [DateTime] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOALTER TABLE [dbo].[TagDataDaily] WITH NOCHECK ADD CONSTRAINT [FK_TagDataDaily_Tag] FOREIGN KEY([Tagkey])REFERENCES [dbo].[Tag] ([TagKey])GOALTER TABLE [dbo].[TagDataDaily] CHECK CONSTRAINT [FK_TagDataDaily_Tag]
New table:USE [Reporting]GO/****** Object: Table [dbo].[tagdatadailyTest3] Script Date: 05/03/2010 12:42:20 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[tagdatadailyTest3]( [Tagkey] [int] NOT NULL, [DateTime] [smalldatetime] NOT NULL, [Value] [real] NULL, [Quality] [tinyint] NULL, [RevLevel] [tinyint] NULL) ON [PRIMARY]
Deletion querydelete METSINFO.REPORTING.DBO.tagdatadailytest3 where (tagkey = 14846 and [datetime] = dateadd(dy,@thisDay,DATEADD(yy, DATEDIFF(yy,0,@thisYear), 0))) delete METSINFO.REPORTING.DBO.tagdatadailytest3 where (tagkey = 14844 and [datetime] = dateadd(dy,@thisDay,DATEADD(yy, DATEDIFF(yy,0,@thisYear), 0))) delete METSINFO.REPORTING.DBO.tagdatadailytest3 where (tagkey = 14845 and [datetime] = dateadd(dy,@thisDay,DATEADD(yy, DATEDIFF(yy,0,@thisYear), 0)))
thank you