Solved

Query help - delete all but 50 rows of data

Posted on 2007-11-16
5
144 Views
Last Modified: 2010-03-19
Hi:

What is the fastest way to delete all but 50 rows  of data from a database table? Thanks.
0
Comment
Question by:sdc248
5 Comments
 
LVL 17

Expert Comment

by:Chris Mangus
ID: 20301442
Do you care which 50 rows you keep?  If not:

Delete From myTable Where myPK Not In (Select Top (50) myPK From myTable)
0
 
LVL 10

Accepted Solution

by:
pai_prasad earned 125 total points
ID: 20302204
depends on the number of records in the table...
 if u have 100 recds above query is gud ./

if u have 2 millon recds.. i wud pref

Select Top 50*
into myTable_bkp
from myTable

Truncate MyTable

Insert into myTable
Select * from myTable_bkp

0
 
LVL 17

Assisted Solution

by:Chris Mangus
Chris Mangus earned 50 total points
ID: 20302285
Assuming the table doesn't have an identity value that needs to be retained this will work.  If you have an identity value you'll need to turn Identity Insert on if you put records back into myTable.
0
 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 50 total points
ID: 20303831
seeing that you are "removing all but 50", this is probably a dev db. i assume there aren't any foreign keys to care about? you probably want the "latest" data then?

delete mytable where idcol < (
select min(idcol) from
(select top 50 idcol from mytable order by idcol desc) x)

you can want raw speed, nothing beats truncate. but it has two major drawbacks:
resets identity columns, and fails when certain constraints are defined (i.e. foreign keys)
it's   TRUNCATE TABLE <tablename> btw, you can drop the "from" in DELETE FROM but not the "table" from TRUNCATE TABLE
0
 

Author Comment

by:sdc248
ID: 20313292
Thanks guys. The "truncate table" command helps a lot. The old "delete from mytable where" method took forever to delete data from a couple of huge tables (200 million rows) and eventually failed because the transaction log grew rapidly and fill up the server.  Luckily these huge tables don't have identity columns or foreign keys.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Record open by another user 6 50
Why do I get extra rows when I do inner join? 12 38
SQL Agent Timeout 5 48
Is there any Easy way to copy CSV to SQL Server using C# 3 70
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

19 Experts available now in Live!

Get 1:1 Help Now