Fastest Way to Delete a large number of data

I m trying to delete a large number of Data
this Proc will be running every day without disturbing the other Process
SET sql = 'DELETE TOP (100) FROM ' + @TableName + ' WHERE Id IN (Select Distinct Id FROM [state] WHERE ToDel = 1)'SET @Deleted_Rows = 1;SET @Rows = 0; WHILE (@Deleted_Rows > 0)BEGIN BEGIN TRANEXEC sp_executesql @sql SET @Deleted_Rows = (SELECT @@ROWCOUNT);SET @Rows = (@Rows + @Deleted_Rows);COMMIT TRANWaitFor DELAY '00:00:00:01' END
Have you any Idea how can I optimize my Query ?
Thank you very much

Hi, following are few thoughts.
1. If you can manage without dynamic sql then it may yield better performance as except table name there is no need for it as per your query.
2. Ensure indexes are in place.
3. If the table in question is not participant in replication then you may slightly go for higher batch size and again it depends on volume.
Please take a look at the tweaked code.
declare @Deleted_Rows int, @Rows int;
SET @Deleted_Rows = 1;
SET @Rows = 0;
WHILE (@Deleted_Rows > 0)
BEGIN
BEGIN TRAN;
DELETE TOP (100) t1
FROM
tabName as t1
WHERE exists (Select * FROM [state] as t2 WHERE t2.ToDel = 1 and t2.Id = t1.id);
SET @Deleted_Rows = @@ROWCOUNT ;
SET @Rows = @Rows + @Deleted_Rows;
COMMIT TRAN;
WaitFor DELAY '00:00:00:01';
END
Print 'Total rows deleted : '+convert(varchar(50), @Rows);

Similar Messages

  • How to optimize DELETE for large number of data?

    Hi Guys,
    I am creating a DELETE sql for deleting large amount of data, approximately a million records. When I tested it, it got a lot amount of execution time. Is there a way how can I optimize this query?
    I highly appreciate any idea from you guys!
    Thanks,
    Jay

    JayPavia wrote:
    I am creating a DELETE sql for deleting large amount of data, approximately a million records. When I tested it, it got a lot amount of execution time. Is there a way how can I optimize this query?
    I highly appreciate any idea from you guys!Jay,
    one quite nice approach is described in the AskTom thread: http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:2345591157689#2356959585102
    In a nutshell: You can create a partitioned table with the same layout (using e.g. a RANGE partitioning with a single default partition), insert the remaining data into that table and swap the two segments using ALTER TABLE EXCHANGE PARTITION.
    This allows you to load the data into the partitioned table using all means available to you, e.g. direct-path parallel insert with nologging attribute of the table to make it as fast as possible, rebuilding/creating any indexes in bulk after the load completed.
    Furthermore, at least seamless read-access to the table is possible while you're performing this operation, and you don't have to deal with renaming/granting etc. which you would need to do with a traditional CTAS approach.
    Of course, if the table needs to be online for DML while performing the DELETE you can't use these options.
    Regards,
    Randolf
    Oracle related stuff blog:
    http://oracle-randolf.blogspot.com/
    SQLTools++ for Oracle (Open source Oracle GUI for Windows):
    http://www.sqltools-plusplus.org:7676/
    http://sourceforge.net/projects/sqlt-pp/

  • TS4009 I deleted a large number of contacts on one iphone when i got a new one. icloud must have automatically synced to the old one, then synced my new one to that, thus deleting many contacts. can i restore them?

    I deleted a large number of contacts on one iphone when i got a new one. icloud must have automatically synced to the old one, then synced my new one to that, thus deleting many contacts. can i restore them? can i retrieve a snapshot of my icloud as it existed yesterday?

    When a device that is connected to an icloud account has it's contacts deleted, the same contacts at icloud.com are also deleted and then the changes propagate to other computers and devices also connected to the same icloud account.   In other words, the contacts are deleted everywhere.  Also, any iOS backup to icloud does not include contacts or any of the other Apple data, like calendars, reminders, etc.  The only way you can get that data back is if it was on a computer which has been frequently backed up.  You restore the data from the backup.  (This reference to "backup" is a local backup, it has nothing to do with icloud.)

  • I am trying to delete a large number of songs from my Itunes library without having to delete them one at a time.  How do I do that?

    I am trying to delete a large number of songs from my itunes library without having to do it one at a time.  How can I do this.  All of my songs have check marks by them.  Help!

    Then I suspect you do not have iTunes Match enabled. Using that "delete all" command in the Settings does not remove the music from the cloud, nor does it disable iTunes Match. With iTunes Match enabled you will still see all the music.
    So go to Settings > iTunes and App Store and make sure iTunes Match is set to ON (green).

  • I have accidentally deleted a large number of develped images in Lightroom before I did a backup. I reimported the original raw files back into Lightroom hoping the develop settings would be re-established but no luck. Notice system mau have done an auto-

    Question?
    I have accidentally deleted a large number of develped images in Lightroom before I did a backup. I reimported the original raw files back into Lightroom hoping the develop settings would be re-established but no luck. Notice system mau have done an auto-backup as have an lrcat-journal file. Can I use this to restore my develop settings. I also have jpgs generated from all the deleted images.

    Hello,
    if you have a backup of your catalog you can do the following:
    1. Backup your catalog first
    2. Restore your backup catalog to some location
    3. Open your current catalog and select "files->import from another catalog".
    4. Select your backup catalog and your lost images. LR ask you if you want to overwrite the current settings or save them as a virtual copy.
    As an alternativ you can open your backup catalog, select the "lost" images and save the development settings as xmp sidecar fiels (using ctrl-s). Then open your current catalog, seletct the images and use "Metadata->Read Metadata from files".

  • How do i go about deleting a large number of files at the same time?  where's the easiest place to do it?

    how do i go about deleting a large number of files at the same time?  where's the easiest place to do it?

    A bit vague as to what you intend, but the simple answer is to select all the files you want to delete then either drag to the Trash or CTRL- or RIGHT-click on the selection and choose Move to Trash from the contextual menu.

  • There are over 4000 duplicates in my iTunes. What is the fastest way to delete them?

    There are over 4000 duplicates in my iTunes. What is the fastest way to delete them?

    Hello there, yandere69keita.
    The following Knowledge Base clarifies your concern about your My Photo Stream counting towards your iCloud storage:
    iCloud: My Photo Stream FAQ
    http://support.apple.com/kb/ht4486
    Does My Photo Stream use my iCloud storage?
    No. Photos uploaded to My Photo Stream do not count against your iCloud storage.
    Thanks for reaching out to Apple Support Communities.
    Cheers,
    Pedro.

  • How can I most quickly delete a large number of outlook messages from my iphone?

    How can I most quickly delete a large number of outlook messages from my iphone?

    Who is the email account provider?
    If the email account is an exchange or IMAP account that you access with Outlook on your computer, delete the email with Outlook which will remove the email from your iPhone.

  • Oracle Error 01034 After attempting to delete a large number of rows

    I sent the command to delete a large number of rows from a table in an oracle database (Oracle 10G / Solaris). The database files are located at /dbo partition. Before the command the disk space utilization was at 84% and now it is at 100%.
    SQL Command I ran:
    delete from oss_cell_main where time < '30 jul 2009'
    If I try to connect to the database now I get the following error:
    ORA-01034: ORACLE not available
    df -h returns the following:
    Filesystem size used avail capacity Mounted on
    /dev/md/dsk/d6 4.9G 5.0M 4.9G 1% /db_arch
    /dev/md/dsk/d7 20G 11G 8.1G 59% /db_dump
    /dev/md/dsk/d8 42G 42G 0K 100% /dbo
    I tried to get the space back by deleting all the data in the table oss_cell_main :
    drop table oss_cell_main purge
    But no change in df output.
    I have tried solving it myself but could not find sufficient directed information. Even pointing me to the right documentation will be higly appreciated. I have already looking at the following:
    du -h :
    du -h8K ./lost+found
    1008M ./system/69333
    1008M ./system
    10G ./rollback/69333
    10G ./rollback
    27G ./data/69333
    27G ./data
    1K ./inx/69333
    2K ./inx
    3.8G ./tmp/69333
    3.8G ./tmp
    150M ./redo/69333
    150M ./redo
    42G .
    I think its the rollback folder that has increased in size immensely.
    SQL> show parameter undo
    NAME TYPE VALUE
    undo_management string AUTO
    undo_retention integer 10800
    undo_tablespace string UNDOTBS1
    select * from dba_tablespaces where tablespace_name = 'UNDOTBS1'
    TABLESPACE_NAME BLOCK_SIZE INITIAL_EXTENT NEXT_EXTENT MIN_EXTENTS
    MAX_EXTENTS PCT_INCREASE MIN_EXTLEN STATUS CONTENTS LOGGING FOR EXTENT_MAN
    ALLOCATIO PLU SEGMEN DEF_TAB_ RETENTION BIG
    UNDOTBS1 8192 65536 1
    2147483645 65536 ONLINE UNDO LOGGING NO LOCAL
    SYSTEM NO MANUAL DISABLED NOGUARANTEE NO
    Note: I can reconnect to the database for short periods of time by restarting the database. After some restarts it does connect but for a few minutes only but not long enough to run exp.

    Check the alert log for errors.
    Select file_name, bytes from dba_data_files order by bytes;
    Try to shrink some datafiles to get space back.

  • Is there a way of combining a large number of materials into a grouping cod

    Greetings,
    Is there a way of combining a large number of materials into a grouping code, so that and end-user can manage this grouping... adding and removing materials... for auctions?
    The materials will likely span material groups in R/3, and there could be thousands of materials. Shopping cart templates would be cumbersome.
    I was thinking about a special catalog characteristic with defined names to manage the various groups of materials.
    I appreciate any advice... also in awarding points.
    Jessica

    Hi Jessica
    This is my understanding:
    - You want to 'Specially group' materials other than material group. Each special group, may consists of materials from many material groups.
    - User(s) pick materials from this group during 'Auction' creation or 'shopping cart' creation.
    Am I right ?
    Looks like using 'Catalog' is the option.
    - MDM Catalog 'mask' or other Catalogs views to have user specific catalog view
    - Maintenance of this catalog by user could become a problem. Then, you need to give content mgmt rights to user
    Best regards
    Ramki

  • How do I delete a large number of songs off my iPad

    I have a large music collection and have downloaded a large number of songs onto my iPad (10GB).  I don't need that much music on my iPad and would like to put the songs back in the cloud.  I don't want to have to delete thousands of songs one at a time by swiping.  What is the best way to do this?
    I have a 4th gen iPad and I have iTunes Match.

    Then I suspect you do not have iTunes Match enabled. Using that "delete all" command in the Settings does not remove the music from the cloud, nor does it disable iTunes Match. With iTunes Match enabled you will still see all the music.
    So go to Settings > iTunes and App Store and make sure iTunes Match is set to ON (green).

  • How can I delete a large number of messages from my iCloud mailbox in bulk mode?

    I decided that I have a large number of e-mail messages in my iCloud mailbox that I do not need anymore and I would like to delete these messages in a single - simple move. How can I do it (Gmail offers a nice way to do this but I cannot find anything similar in iCloud...)?

    Archive the contacts you have in Address book, then delete them, they will be removed from iCloud as well. Restore the archive

  • How do I group delete the large number of duplicate songs I have accumulated?

    I have accumulated a large number of duplicate songs (1600+) due to several passes at restoring data to my itunes library.
    Is there a way to delete all but a single version of the files that show up when I click on "show duplicates"?

    Hey applejcn,
    I'd read over this article, it'll go over how to use the Show Duplicates feature in order to remove those 1600 duplicates from your library:
    How to find and remove duplicate items in your iTunes library
    http://support.apple.com/kb/ht2905
    Cheers,
    David

  • Which is the best way for posting a large number of records?

    I have around 12000 register to commit to dababase.
    Which is the best way for doing it?
    What depends on ?
    Nowadays I can't commit such a large number of register..The dabatase seems hanged!!!
    Thanks in advance

    Xavi wrote:
    Nowadays I can't commit such a large number of registerIt should be possible to insert tens of thousands of rows in a few seconds using an insert statement even with a complex query such as the all_objects view, and commit at the end.
    SQL> create table t as select * from all_objects where 0 = 1;
    Table created.
    Elapsed: 00:00:00.03
    SQL> insert into t select * from all_objects;
    32151 rows created.
    Elapsed: 00:00:09.01
    SQL> commit;
    Commit complete.
    Elapsed: 00:00:00.00
    I meant RECORDS instead of REGISTERS.Maybe that is where you are going wrong, records are for putting on turntables.

  • I was trying to delete a large number of e-mails from Stamp. I did something and now I can't delete any of my e-mails. What do I do to fix this.

    I tried to delete a large # of e-mail. I did something that caused the computer to hum for a few minutes. All of the e-mails came back and now I can't delete any of my old e-mails.  What did I do and how do I fix it?

    The clipboard is a place in memory where text that has been copied from is temporarily stored ready to be pasted elsewhere.
    The clipboard cannot be viewed, and is essentially invisible to the user. Copied text will stay on the clipboard until it is replaced by the next piece of copied text.
    Probably what happened is you copied your credit card number from somewhere, either intentionally or accidentally (perhaps you made a purchase on your iPad using this card recently), and the clipboard stored the number. Then, whilst you were using Facebook you (accidentally) pasted this credit card number from the clipboard into your Facebook message.
    That's the only likely reason I can think of. Hope I explained it well enough!

Maybe you are looking for

  • Add a custom field to Business Partner(master data screen)

    Hi MM experts, My requirement: Basically have to pass special accounting number (from FI module to MM module ). For that, we need to create new custom field in the standard screen (master data) of business partner. How to achieve this? Is that by cre

  • How to may a 16 x 9 quicktime play in that aspect ratio

    I've got a problem whereby quicktimes that are corrected created at 16 x 9, play in a 4 x 3 aspect ration screen when you open them in quicktime player. If there some setting I'm supposed to be changing so that the movie plays in the correct aspect r

  • Error in Preferences.app - picture frame

    I got a strange problem in my iPad 3rd gen. Since a couple of days the preferences settings page for picture frame is totally empty. At the lockscreen the picture frame button is still there but without any function. Any ideas?

  • Login page in jsp-urgent

    hello to all, i am using tomcat 4.1.24,i have created a login page where i am forwarding the username and password to a jsp file where it process the validation by instantiating the bean and sets the property,but when it (Process.jsp)calls a method i

  • After installing Firefox UK Comodo security keeps scanning 2 files in omni.jar folder as Trojan downloader agents, do I ignore?

    I have been using Property Bee as an add on for Right Move with previous version of Firefox. When it updated recently it kept showing alert warnings for trojans in temporary internet files.These were wiped and the whole system was scanned. Got rid of