CartMetrix - Do you know yours?

« Mint Pepper DLoads v0.70 | Home | Southwestern Bell Customer Service - Again »

1/17/2006

Delete Duplicate Rows in MySQL

1.5 million rows in a products_categories table. That’s 1,500,000 products to categories relations. I needed a quick way to remove products from multiple categories to test the performance hit of the HUGE products_categories table. A quick and dirty is to remove duplicate productids from the table. Here’s a quick howto.

Create a temporary table with only one row for each productid (the GROUP BY clause). Change this GROUP BY to be the field with duplicates to remove. You can also add some logic here to select which records to keep.

CREATE TABLE products_categories_temp AS SELECT * FROM products_categories GROUP BY productid

Empty the original table (you created a backup already right?).

TRUNCATE products_categories

Copy the ‘clean’ records back to the original table.

INSERT INTO products_categories SELECT * FROM products_categories_temp

This can be used to normalize a table to add a primary key, data mining, cleaning a database or just for testing.

Popularity: 30%

Trackback:

Related Posts

One Response to “Delete Duplicate Rows in MySQL”

  1. Arpit said:

    Its a good solution…for removing duplicates

Post your opinion

Verification Image

Please type the letters you see in the picture.

Subscribe without commenting


damonparker.org is proudly powered by WordPress
Entries (RSS) and Comments (RSS).

copyright © 2002-2009 damonparker.org. all rights reserved.

Close
E-mail It