Autor Subiect: Stergere duplicate din tabele mysql  (Citit de 8994 ori)

Admin

  • Administrator
  • guru
  • *****
  • Mesaje postate: 58
  • Karma: +0/-0
Stergere duplicate din tabele mysql
« : Iunie 01, 2019, 12:43:38 p.m. »
De multe ori se intampla sa avem duplicate. Asta e cum importurile, se mai dupliciteaza.
Ideea este ca mai bine importi apoi elimini, castigi timp. Fireste ca daca ai deja slug-uri care sunt indexate ai face bine sa verifici ce stergi si adaugi in xml-ul sitemap, warever :)

Ex. Tabele dup

mysql> select * from dup;
+----+------------+------------+
| id | producator | slug       |
+----+------------+------------+
|  1 | Sony       | tv-led     |
|  2 | Samsung    | galaxy-s10 |
|  3 | Huawei     | huawei-p30 |
|  4 | Samsung    | galaxy-s10 |
+----+------------+------------+

id 2 si 4 au slug identic deci

ALTER IGNORE TABLE `dup` ADD UNIQUE (producator, slug);

mysql> select * from dup;
+----+------------+------------+
| id | producator | slug       |
+----+------------+------------+
|  3 | Huawei     | huawei-p30 |
|  2 | Samsung    | galaxy-s10 |
|  1 | Sony       | tv-led     |
+----+------------+------------+

Indexii arata asa:
show index from dup;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| dup   |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| dup   |          0 | producator |            1 | producator  | A         |           2 |     NULL | NULL   | YES  | BTREE      |         |               |
| dup   |          0 | producator |            2 | slug        | A         |           2 |     NULL | NULL   | YES  | BTREE      |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

iar daca vrei sa stergeti:

drop index producator on dup;


bafta