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 8650113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:44:12+00:00 2026-06-12T13:44:12+00:00

I have a table with this column (among others): id int identity(1,1) not null

  • 0

I have a table with this column (among others):

id int identity(1,1) not null

I’ve added a column to store the approximate date the record was inserted:

insertdate smalldatetime null

I’ve filled in the insertdate where I can using the earliest reference found in forensic searches of related tables and logs. This does, however, leave a number of NULL “gaps” in the data, as well as situations where a record with a lower ID number has a more recent insertdate value than subsequent ID values.

The identity attribute provides an adequate basis to assume that a record must have been created before any record with a higher ID value, so I’ve decided to update the insertdate for any record where it is null or a subsequent ID has an earlier date:

UPDATE
table
SET
insertdate = (SELECT MIN(insertdate) 
      FROM table t2
      WHERE
        t2.id >= table.id 
        AND t2.insertdate IS NOT NULL
      )

Unfortunately, updating like this is eating the server’s lunch… 1 hour and counting for 2.5 million records.

Any ideas for how to do this more efficiently?

It only needs to be done once, but this is a production server, so I’d prefer to not lock up the table for any longer than necessary.

  • 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-12T13:44:14+00:00Added an answer on June 12, 2026 at 1:44 pm

    Not sure this will help but you don’t need to test for null.
    Min() will not consider nulls.

    UPDATE
    table 
    SET
    insertdate = (SELECT MIN(t2.insertdate) 
                    FROM table t2
                   WHERE t2.id >= table.id 
                     AND t2.ID < table.id + 10000)  
    

    Could you limit to the next x rows?
    Is there a point at which you are pretty sure you are not going to find a smaller date?

    And you could limit table and t2 to the ID of the first row when you set a default on date.

    Might fill up the transaction log and roll the whole thing back.
    If that happens just be patient and let it roll back.
    If you abort it now it is going to have to roll back.
    Breaking up in batches of 100,000 is going to let the transaction log clear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table: ID(INT) DATE(DATETIME) Under the DATE column there are a lot
Assume I have a table like this: column A(int) column B(int) 1 2 2
I have a database with a table which includes among others, one column of
I have a table called customer which has, among others, a column called name
Imagine I have this table: Column A | Column B | Column C ------------------------------
I have table and this table contain result column with some entries. I just
I have a table with a datetime column. Into this column I have written
I have a table I'd like to sort with a priority column. This column
If i have a sql table with column like this id version subversion 1
If I have a table column with data and create an index on this

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.