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

  • SEARCH
  • Home
  • 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 8395703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:22:05+00:00 2026-06-09T20:22:05+00:00

I have a script to find duplicate rows in my MySql table, the table

  • 0

I have a script to find duplicate rows in my MySql table, the table contains 40,000,000 rows. but it is very slow going, is there an easier way to find the duplicate records without going in and out of php?

This is the script i currently use

 $find = mysql_query("SELECT * FROM pst_nw ID < '1000'");
        while ($row = mysql_fetch_assoc($find))
        {
            $find_1 = mysql_query("SELECT * FROM pst_nw add1 = '$row[add1]' AND add2 = '$row[add2]' AND add3 = '$row[add3]' AND add4 = '$row[add4]'");
                if (mysql_num_rows($find_1) > 0) {
                                                    mysql_query("DELETE FROM pst_nw WHERE ID ='$row[ID]'}

         }
  • 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-06-09T20:22:06+00:00Added an answer on June 9, 2026 at 8:22 pm

    You have a number of options.

    Let the DB do the work

    Create a copy of your table with a unique index – and then insert the data into it from your source table:

    CREATE TABLE clean LIKE pst_nw;
    ALTER IGNORE TABLE clean ADD UNIQUE INDEX (add1, add2, add3, add4);
    INSERT IGNORE INTO clean SELECT * FROM pst_nw;
    DROP TABLE pst_nw;
    RENAME TABLE clean pst_nw;
    

    The advantage of doing things this way is you can verify that your new table is correct before dropping your source table. The disadvantage is it takes up twice as much space and is (relatively) slow to execute.

    Let the DB do the work #2

    You can also achieve the result you want by doing:

    set session old_alter_table=1;
    ALTER IGNORE TABLE pst_nw ADD UNIQUE INDEX (add1, add2, add3, add4);
    

    The first command is required as a workaround for the ignore flag being .. ignored

    The advantage here is there’s no messing about with a temporary table – the disadvantage is you don’t get to check that your update does exactly what you expect before you run it.

    Example:

     CREATE TABLE `foo` (
      `id` int(10) NOT NULL AUTO_INCREMENT,
      `one` int(10) DEFAULT NULL,
      `two` int(10) DEFAULT NULL,
      PRIMARY KEY (`id`)
    )
    
    insert into foo values (null, 1, 1);
    insert into foo values (null, 1, 1);
    insert into foo values (null, 1, 1);
    
    select * from foo;
    +----+------+------+
    | id | one  | two  |
    +----+------+------+
    |  1 |    1 |    1 |
    |  2 |    1 |    1 |
    |  3 |    1 |    1 |
    +----+------+------+
    3 row in set (0.00 sec)
    
    set session old_alter_table=1;
    ALTER IGNORE TABLE foo ADD UNIQUE INDEX (one, two);
    
    select * from foo;
    +----+------+------+
    | id | one  | two  |
    +----+------+------+
    |  1 |    1 |    1 |
    +----+------+------+
    1 row in set (0.00 sec)
    

    Don’t do this kind of thing outside the DB

    Especially with 40 million rows doing something like this outside the db is likely to take a huge amount of time, and may not complete at all. Any solution that stays in the db will be faster, and more robust.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created script to find selectivity of each columns for every tables. In
I have a script that I need to find the full Distinguished name (
Currently I have a bash script which runs the find command, like so: find
I have a Perl script called replaceUp: #!/usr/bin/perl search=$1 replace=$2 find . -type f
For testing purposes I have this shell script #!/bin/bash echo $$ find / >/dev/null
I have searched left and right. And I am trying to find a script
I have a fairly large script and am trying to find out, why $(document).on(
I am writing a shell script in which I have to find the last
I have checked for a solution here but cannot seem to find one. I
Possible Duplicate: javascript date creation, can't set the correct month I have script like:

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.