Solved

Can't Update a Bit Column in 227 Million Row Table

Posted on 2011-02-15
9
467 Views
Last Modified: 2012-05-11
I tried last night (after turning of all websites, applications and even SQL jobs that use the DB) to update a bit column to 0 on a table that contains 227 million rows.   After 5.5 hours and it being 5:30 AM, I had to give up and rollback the change (which probably took about 3 hours).

Strangely, a week prior I update a DateTime field to '1/1/1900' in the same table without any issue.  I don't understand why I'm not able to run the update in a reasonable amount of time.  Perhaps I need to delete all the indexes first and then build them back up after?  That's a royal pain.

0
Comment
Question by:davidcahan
  • 4
  • 3
  • 2
9 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34898835
Can you post the table / indexes definition including the update statement?
0
 

Author Comment

by:davidcahan
ID: 34898918
FileDisplay (FDID int, CMID int, LMID int, Date datetime, Dateadded Datetime, IsFileDrop bit)

IX_FD primary key, nonclustered, 70% fill
CMID, nonclustered, 70% fill
LMID, nonclusterd, 70% fill
Date nonclustered, 70% fill

Update FileDisplay
Set IsFileDrop = 0
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34900463
I don't see anything special about your table or update.

Concurrency issues is what comes to my mind.  Do you know what other processes were doing at that time?  Those processes may have locked rows on the FileDisplay table or stressed the server.
0
 

Author Comment

by:davidcahan
ID: 34900606
I actually shut down all sql jobs, web sites and other processes that would have been hitting the DB.  I was getting a lot of pageiolatch_sh wait times with an Average Latch Wait of 876 ms.  I may try a simple reboot of the server and see if anything seems to clear up.

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:davidcahan
ID: 34900620
either that, or I may have to batch it out somehow.  what a pain that would be...
0
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 34907935
I would write a batch update code and run it in a SQL job until all values are updated. Something like below should help and please always keep in mind that bit field datatype is different than int or smallint therefor you should use 0x0 = 0, and 0x1 = 1 for bit fields especialy if they are indexed.

SET NOCOUNT ON;

                  --Set rowcount to 200 to limit number of deletes per batch
                  SET ROWCOUNT 200

                  DECLARE @rc int
                  SET @rc=200

                  WHILE @rc=200
                  BEGIN
                            UPDATE TOP(200) FileDisplay  WITH(ROWLOCK)
                            SET IsFileDrop  = 0x1
                            WHERE IsFileDrop is null

                        SELECT @rc=@@rowcount
                  END

                  SET ROWCOUNT 0      
SET NOCOUNT OFF;
0
 

Author Comment

by:davidcahan
ID: 34910562
Little confused by code snippet.  If I'm only updating the top 200 that are null then why would I need to use any rowcount or while loop.  Everyday (or hour, or 30 mins) I could just update top X rows until it's updated.

0
 
LVL 39

Expert Comment

by:lcohan
ID: 34910702
You should test and change the batch size to a value that works and is NOT impacting your production assuming this is done in production database. As far as how often you could run it even every minute as if it's a sql job next run won't start is ther's one running already.
The code is written like that on purpose - think you have an update less than 200 rows because a value in the top 200 already changed so if you use this batch in a loop it may be broken.
Hope all this makes sense - please let me know if you have more questions.
0
 
LVL 39

Expert Comment

by:lcohan
ID: 34910834
Oh and I forgot to mention one other thing - hopefully there are no triggers on the table you need to update.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now