Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 944717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:38:19+00:00 2026-05-15T22:38:19+00:00

I saw the solution to create an alternate temporary MySQL table with unique rows,

  • 0

I saw the solution to create an alternate temporary MySQL table with unique rows, but I didn’t like that idea, as my tables are very large and would be a hassle to move them (and would create huge problems if there would be errors during the move).

I did, however, find the following. What do you think of this (where the duplicates to check is “field_name”)?

DELETE FROM table1
USING table1, table1 as vtable
WHERE (NOT table1.ID=vtable.ID)
AND (table1.field_name=vtable.field_name)

Somebody said this should work, but I’m not quite sure. What do you think? Also, will having indexes at all alter the performance of this command, say, having an index on “field_name”?

EDIT: Would there be any way to test the query before running it? As far as I know, MySQL doesn’t support “explain” on DELETE queries.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:38:19+00:00Added an answer on May 15, 2026 at 10:38 pm

    Note that the query you show will delete both duplicates. I would assume you want to keep one or the other.

    Here’s how I would write this query:

    DELETE t1 FROM table1 AS t1 JOIN table1 AS t2 
      ON t1.id > t2.id AND t1.field_name = t2.field_name;
    

    By using greater-than instead of not-equals-to, you only delete one row (the later one), instead of both.

    A compound index over (id, field_name) may help. You should confirm this with MySQL’s EXPLAIN to get the optimization report. But EXPLAIN only supports SELECT queries so you should run an equivalent SELECT to confirm the optimization:

    EXPLAIN SELECT * FROM table1 AS t1 JOIN table1 AS t2 
      ON t1.id > t2.id AND t1.field_name = t2.field_name;
    

    You also asked about testing. I’d recommend copying a sample of rows containing duplicates to a table in your test database:

    CREATE TABLE test.table1test SELECT * FROM realdb.table1 LIMIT 10000;
    

    Now you can perform experiments on your sample data until you’re satisfied the DELETE solution is correct.

    USE test;
    SET autocommit = 0;
    DELETE ... 
    ROLLBACK;
    

    I’d recommend naming your scratch table in the test database something distinct from your real table in your real database. Just in case you run an experimental DELETE while you are accidentally still using your real database as the default database!


    Re your comments:

    USE test is a mysql client builtin command. It sets the test database as the default database. This will be the default database when you name tables in your queries without qualifying them with a database name. See http://dev.mysql.com/doc/refman/5.1/en/use.html

    SET autocommit = 0 turns off the default behavior of committing a transaction for each query implicitly. So you must explicitly give the COMMIT or ROLLBACK command to finish a transaction. See http://dev.mysql.com/doc/refman/5.1/en/commit.html

    It’s worthwhile to use ROLLBACK when you’re experimenting because it discards the changes made in that transaction. It’s a quick way to return to the initial state of your test data so you can try another experiment.

    DELETE t1 is not a typo. DELETE deletes rows, not whole tables. t1 is an alias to each row that satisfies the conditions of the statement (although it is possible that the conditions include every row in the table). See description of multi-table delete at http://dev.mysql.com/doc/refman/5.1/en/delete.html

    Sort of like when you run a loop in PHP and you use a variable to iterate over the loop: for ($i=0; $i<100; ++$i) … The variable $i takes on a series of values, and each time through the loop it has a different value.

    Here’s a demo showing how my solution deletes multiple duplicates. I ran this in my test database and I’m pasting the result directly from my command window:

    mysql> create table table1 (id serial primary key, field_name varchar(10));
    Query OK, 0 rows affected (0.45 sec)
    
    mysql> insert into table1 (field_name) 
           values (42), (42), (42), (42), (42), (42);
    Query OK, 6 rows affected (0.00 sec)
    Records: 6  Duplicates: 0  Warnings: 0
    
    mysql> select * from table1;
    +----+------------+
    | id | field_name |
    +----+------------+
    |  1 | 42         | 
    |  2 | 42         | 
    |  3 | 42         | 
    |  4 | 42         | 
    |  5 | 42         | 
    |  6 | 42         | 
    +----+------------+
    6 rows in set (0.00 sec)
    
    mysql> delete t1 from table1 t1 join table1 t2 
           on t1.id > t2.id and t1.field_name = t2.field_name;
    Query OK, 5 rows affected (0.00 sec)
    
    mysql> select * from table1;
    +----+------------+
    | id | field_name |
    +----+------------+
    |  1 | 42         | 
    +----+------------+
    1 row in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 514k
  • Answers 514k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here is the basic idea: struct myclass{ //myclass() : x(2){}… May 16, 2026 at 6:19 pm
  • Editorial Team
    Editorial Team added an answer I don't believe that the outline is applied by the… May 16, 2026 at 6:19 pm
  • Editorial Team
    Editorial Team added an answer Yes, with a few caveats. A POSIX socket connect will… May 16, 2026 at 6:19 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I saw similar questions here, but I couldn't find solution to my problem. I
I saw a similar question and hoped for a solution, but simply giving an
I saw some documentation and previous questions about this, but couln't find a solution.
I want to create a file that only resides in memory... In looking through
I Need help on browser close event. I saw a tutorial but it is
We have a large number of projects within a solution mostly simple class libraries
I would like to log all the output of a Python script. I tried:
I was searching online for a solution to passing a string array to a
I have two folders in my asp.net web application in which I create new
is it possible to add some kind of class like 'arrow' or a span

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.