I have a table with 300,000 records. There are duplicae rows in this table, and I want to update the "flag" column
Table
------------------------------------ |number | flag | ... more column ...| ------------------------------------ |ABCD | 0 | ...................| |ABCD | 0 | ...................| |ABCD | 0 | ...................| |BCDE | 0 | ...................| |BCDE | 0 | ...................|
I use this query to update the flag column:
UPDATE table i INNER JOIN (SELECT number FROM table GROUP BY number HAVING count(number) > 1 ) i2 ON i.number = i2.number SET i.flag = '1'
This query is very slow (over 600 seconds) for these 300,000 records.
How can I optimize this query?
STRUCTURE OF MY TABLE
CREATE TABLE IF NOT EXISTS `inv` ( `id` int(11) NOT NULL AUTO_INCREMENT, `pn` varchar(10) NOT NULL COMMENT 'Part Number', `qty` int(5) NOT NULL, `qty_old` int(5) NOT NULL, `flag_qty` tinyint(1) NOT NULL, `name` varchar(60) NOT NULL, `vid` int(11) NOT NULL , `flag_d` tinyint(1) NOT NULL , `flag_u` tinyint(1) NOT NULL , `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `pn` (`pn`), KEY `name` (`name`), KEY `vid` (`vid`), KEY `pn_2` (`pn`), KEY `flag_qty` (`flag_qty`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;
If the "name" is duplicated, I want to update the qty_ flag
source share