Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with dupe query

Status
Not open for further replies.

pandatime

Programmer
Jan 29, 2010
92
AU
Hi,

I have a query I'm using to eliminate duplicate rows from a table with about 5 mil rows. PK is a clustered index.

The problem is, it's extremely slow. When I look at the execution plan, it's very complex, and I see most of the cost is on "sort".

Is there a way to do this that is more efficient??

Thanks

Code:
	DELETE
	FROM myTable
	AND pk NOT IN
	(
	SELECT MAX(pk)
	FROM myTable
	GROUP BY
	col1,
	col2,
	col3,
	col4,
	col5,
	col6,
	col7,
	col8,
	col9
	)
 
SQL Server 2005 and up
Code:
;with cte as (select PK, row_Number() over (partition by col1, col2, col3, col4, col5, col6, col7, col8, col9 ORDER by PK Desc) as Row from myTable)

delete from cte where Row > 1

PluralSight Learning Library
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top