To prevent duplicate entry issue - replace INSERT into with REPLACE INTO .
DEF: REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
REPLACE into table (id, name, age) values(1, "A", 19)
Run Optimize of All tables of a database:
mysqlcheck -o db_to_optimize -udb_user -p