我在SQL Server 2008中有一个删除作业,需要16个多小时才能完成。
这是工作的内容。有可能进行调整吗?
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;
--Modified as per the new request as a part of project XIRI P2 Quick Hit Solution--banu
DECLARE @DELETECOUNT int
DECLARE @DELETEATATIME int
-- DECLARE @TBLCOUNT int
--DECLARE @MAXRETENTION int
-- DECLARE @ROWSDELETEDCOUNT int
DECLARE @CutOffDate datetime
DECLARE @HourRetained smallint
set @HourRetained = 24
begin Try
--SELECT @TBLCOUNT = COUNT(*) FROM tblcalldatastore WITH (READPAST) WHERE IsTestCase=0
--SET @MAXRETENTION = 100000
set @CutOffDate = getutcdate()
select @DELETECOUNT = count(*)
from [tblcalldatastore]
where istestcase=0
and datediff(hour,receiveddate,@CutOffDate)>@HourRetained
SET @DELETEATATIME = 2000
-- CREATE TABLE #Temptbl
-- (
-- CollTag varchar(255),
--ReceivedDate datetime
-- )
-- INSERT INTO #Temptbl
-- SELECT TOP (@DELETECOUNT) CollTag, ReceivedDate
FROM tblcalldatastore WITH (NOLOCK)
WHERE IsTestCase=0 and datediff(hour,receiveddate,getutcdate())>@HourRetained
--ORDER BY ReceivedDate ASC
--SELECT @ROWSDELETEDCOUNT = count(*) from #Temptbl
WHILE @DELETECOUNT > 0
BEGIN
DELETE TOP(@DELETEATATIME) FROM tblcalldatastore WITH (ROWLOCK) WHERE IsTestCase=0 and datediff(hour,receiveddate,@CutOffDate)>@HourRetained
--DELETE FROM tblcalldatastore WITH (ROWLOCK)
--WHERE CollTag IN
--(SELECT TOP (@DELETEATATIME) CollTag FROM #TempTbl ORDER BY ReceivedDate ASC)
SET @DELETECOUNT = @DELETECOUNT - @DELETEATATIME
--DELETE FROM #TempTbl
--WHERE CollTag in (SELECT TOP (@DELETEATATIME) CollTag FROM #TempTbl ORDER BY ReceivedDate ASC)
--SET @DELETEATATIME = @DELETEATATIME + 2000
END
--DROP TABLE #Temptbl
--PRINT @ROWSDELETEDCOUNT
end try
BEGIN CATCH
SELECT 'FAILED - ' + ERROR_MESSAGE() AS ErrorMessage
END CATCH
END
表上的索引
USE [GRSQWS]
/****** Object: Index [idx_CollTag_IsTestCase] Script Date: 07/03/2019 07:12:37 ******/
CREATE UNIQUE NONCLUSTERED INDEX [idx_CollTag_IsTestCase] ON [dbo]. [tblCallDataStore]
(
[CollTag] ASC,
[IsTestCase] 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, FILLFACTOR = 80) ON [PRIMARY]
GO
/****** Object: Index [idx_CollTag_SerialNo_Model] Script Date: 07/03/2019 07:12:37 ******/
CREATE UNIQUE NONCLUSTERED INDEX [idx_CollTag_SerialNo_Model] ON [dbo].[tblCallDataStore]
(
[CollTag] ASC
)
INCLUDE ( [SerialNumber],
[MachineModel],
[ReceivedDate],
[IsTestCase],
[DeviceData]) 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, FILLFACTOR = 80) ON [PRIMARY]
GO
/****** Object: Index [idx_IsTestCase_ReceivedDate] Script Date: 07/03/2019 07:12:37 ******/
CREATE NONCLUSTERED INDEX [idx_IsTestCase_ReceivedDate] ON [dbo].[tblCallDataStore]
(
[IsTestCase] ASC
)
INCLUDE ( [ReceivedDate]) 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
/****** Object: Index [IX_tblcalldatastore_receiveddate] Script Date: 07/03/2019 07:12:37 ******/
CREATE NONCLUSTERED INDEX [IX_tblcalldatastore_receiveddate] ON [dbo].[tblCallDataStore]
(
[ReceivedDate] 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
/****** Object: Index [PK_tblCallDataStore_1] Script Date: 07/03/2019 07:12:37 ******/
ALTER TABLE [dbo].[tblCallDataStore] ADD CONSTRAINT [PK_tblCallDataStore_1] PRIMARY KEY CLUSTERED
(
[CollTag] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS enter code here= ON, FILLFACTOR = 80) ON [PRIMARY]
GO