December 1, 2010 at 2:33 am
Hi,
Can you please suggest me the best query to delete the duplicate records in a table with large number of records.
Thanks in advance.
--Chandra
December 1, 2010 at 2:45 am
Hi ..
Try this one...
declare @tab table (id int,name varchar(10))
insert into @tab (id,name)
select 1,'Sumit' union all
select 1,'Sumit' union all
select 2,'Sumit2' union all
select 2,'Sumit2'
select id,name from ( select row_number() over (partition by id,name order by id )as row ,id,name from @tab )as t
where row= 1
December 1, 2010 at 2:59 am
chitturiii (12/1/2010)
Hi,Can you please suggest me the best query to delete the duplicate records in a table with large number of records.
Thanks in advance.
--Chandra
You may have to delete in batches if there are a very large number of rows to be deleted. Do you have a query which identifies the rows to delete? If not, post the structure of the table as CREATE TABLE etc and any indexes, also which columns are used to identify dupes.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
December 1, 2010 at 9:15 am
If you want to filter duplicate, just use group by clause to filter duplicate row.
select column_name, min(PK_Column_name) PrimaryKeyColumn from tablename group by column_name having count(*)>1
The above script will identify duplicate value and show minimum Primary key value. If you intend to keep one row, you can keep the minimum Primary key value row. (of course you can change the condition)
The below will delete all duplicate rows and leaving one distinct value row(minimum Primary key value row)
with Tempinfo (columename, PrimaryKeyColumn)
as
(select column_name, min(PK_Column_name) PrimaryKeyColumn from tablename group by column_name having count(*)>1)
delete a
from tablename a join Tempinfo b on a.column_name=b.column_name and a.PrimaryKeyColumn<>b.PrimaryKeyColumn
December 3, 2010 at 10:47 pm
Also it is a better idea to put the recover model to SIMPLE or else your log file will bloat.But make sure you take a Full and a T log backup before changing the recovery model to SIMPLE.
--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
December 4, 2010 at 11:25 am
Sachin Nandanwar (12/3/2010)
Also it is a better idea to put the recover model to SIMPLE or else your log file will bloat.But make sure you take a Full and a T log backup before changing the recovery model to SIMPLE.
Agh... be careful now. Changing to SIMPLE breaks the backup chain. This is normally one of the worst things you can do.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 7, 2010 at 3:01 am
Jeff Moden (12/4/2010)
Sachin Nandanwar (12/3/2010)
Also it is a better idea to put the recover model to SIMPLE or else your log file will bloat.But make sure you take a Full and a T log backup before changing the recovery model to SIMPLE.Agh... be careful now. Changing to SIMPLE breaks the backup chain. This is normally one of the worst things you can do.
But that's the reason I suggested to take a FULL backup before changing it to SIMPLE.Then delete the records.Change it to FULL.Again take a Full Backup.
Correct me if I am missing something.
--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
December 7, 2010 at 3:12 am
Sachin Nandanwar (12/7/2010)
Jeff Moden (12/4/2010)
Sachin Nandanwar (12/3/2010)
Also it is a better idea to put the recover model to SIMPLE or else your log file will bloat.But make sure you take a Full and a T log backup before changing the recovery model to SIMPLE.Agh... be careful now. Changing to SIMPLE breaks the backup chain. This is normally one of the worst things you can do.
But that's the reason I suggested to take a FULL backup before changing it to SIMPLE.Then delete the records.Change it to FULL.Again take a Full Backup.
Correct me if I am missing something.
Execute the query inside the Begin transaction - End Transaction, If you satisfy with the result then commit it otherwise rollback it. So, it will not change anything in your database in case of any error or issue. Take backup & restore require more time.
Once you change the backup type, your backup chain is no longer valid. You need to take FULL backup once again and do the differential/transaction log backup as per your backup strategy.
Thanks
December 12, 2010 at 7:27 pm
Hardy21 (12/7/2010)
Sachin Nandanwar (12/7/2010)
Jeff Moden (12/4/2010)
Sachin Nandanwar (12/3/2010)
Also it is a better idea to put the recover model to SIMPLE or else your log file will bloat.But make sure you take a Full and a T log backup before changing the recovery model to SIMPLE.Agh... be careful now. Changing to SIMPLE breaks the backup chain. This is normally one of the worst things you can do.
But that's the reason I suggested to take a FULL backup before changing it to SIMPLE.Then delete the records.Change it to FULL.Again take a Full Backup.
Correct me if I am missing something.
Execute the query inside the Begin transaction - End Transaction, If you satisfy with the result then commit it otherwise rollback it. So, it will not change anything in your database in case of any error or issue. Take backup & restore require more time.
Once you change the backup type, your backup chain is no longer valid. You need to take FULL backup once again and do the differential/transaction log backup as per your backup strategy.
The whole point is to try to avoid sending the LOG file through the roof with a bazillion deletes and to try to accelerate the rate of the deletions. Simply adding an explicit transaction will have neither of those effects.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply