Solved

Query help - delete all but 50 rows of data

Posted on 2007-11-16
5
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In this article I will describe the Backup & Restore 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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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