Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8621375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:48:23+00:00 2026-06-12T06:48:23+00:00

I have a Delete Query that each day needs to run deleting any data

  • 0

I have a Delete Query that each day needs to run deleting any data that is greater than 7 days old which is about 6 Million Records.

My table should only store data for the last 7 days.

Here is the query I am running:

DELETE FROM [tblTSS_DataCollection]
Where [DatapointDate] < DATEADD(D, -7, GETDATE()) 

This query takes 5.5 minutes to execute. I have an index setup that includes this so I don’t think it should be taking this long to execute:

CREATE UNIQUE NONCLUSTERED INDEX [IX_tblTSS_DataCollection] ON [dbo].  [tblTSS_DataCollection] 
(
[DataPointID] ASC,
[DatapointDate] ASC,
[AssetID] ASC
)

Is there a better way to delete this data? It takes forever and I really need to be able to delete this data quickly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:48:24+00:00Added an answer on June 12, 2026 at 6:48 am

    Your index doesn’t really support the query since the query doesn’t reference the leading key column. So, it has to scan the entire table with or without this index. You might consider an index on DataPointDate alone to support this delete operation if it’s something you run often.

    If DataPointID is an IDENTITY column, and DataPointDate is entered sequentially, you might also consider this variation:

    DECLARE @maxID INT;
    
    SELECT @maxID = MAX(DataPointID) 
      FROM dbo.tblTSS_DataCollection
      WHERE [DatapointDate] < DATEADD(D, -7, GETDATE());
    
    DELETE dbo.tblTSS_DataCollection
      WHERE DataPointID <= @maxID;
    

    Another thing you might consider doing (if it’s the delete and not the scan contributing to the slowness) is (a) making sure your log has enough room to accommodate the delete, and isn’t killing you with a bunch of autogrows, and (b) performing the work in batches:

    BEGIN TRANSACTION;
    
    SELECT 1;
    
    WHILE @@ROWCOUNT > 0
    BEGIN
      COMMIT TRANSACTION;
    
      DELETE TOP (1000) dbo.tblTSS_DataCollection WHERE ...
    END
    
    COMMIT TRANSACTION;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button that will delete all users that fits into this query:
I have a query like DELETE from tablename where colname = value; which takes
This code is what i have tried to process the query, either delete or
In PostgreSQL I have a query like the following which will delete 250k rows
We have a data model that has some requirements. I would like to find
I have 4 tables that stores different information about a user in each. Each
I have this mysql query that finds duplicates and the number of occurances for
I have a Todo List (an ASP.MVC UserControl) that for each row has the
I have a table that gets updated very regularly throughout the day, so Im
I don't understand how two duplicate queries that each delete a single row against

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.