Solved

Delete double rows in mysql database

Posted on 2014-01-28
4
525 Views
Last Modified: 2014-01-28
Hi,

I have a table (table1) with 3 columns, id, text, flag.
The table contains tens of thousands of rows with some text.

Some of these rows are doubles.
I tried this query below to delete these double entries, but it runs for hours and deletes nothing.

DELETE n1 FROM table1 n1, table1 n2 WHERE n1.id > n2.id AND n1.text = n2.text

Does anybody have a better (working) and faster query?

Thanks!
0
Comment
Question by:peps03
  • 2
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
Comment Utility
Create an empty table2.

INSERT INTO table2 (id, text, flag) (SELECT id, text, flag FROM table1 GROUP BY table1.text)

HTH,
Dan
0
 

Author Comment

by:peps03
Comment Utility
Thanks Dan!
That worked! Very quickly!!

What was wrong with my query? A lot?
0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
You query looked more like what a programmer would try than what SQL would expect.

You were JOINining your table with itself on the text field, which I guess was not indexed.

It created a huge table and I don't really know what MySQL was trying to do with it.
0
 

Author Comment

by:peps03
Comment Utility
Ok, thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
The viewer will learn how to dynamically set the form action using jQuery.

771 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

10 Experts available now in Live!

Get 1:1 Help Now