We have an odd error this morning.
there are a bunch of SQLs that count unique values in table columns. One of these returns with:
Error: SQLSTATE[HY000]: General error: 1034 Duplicate key 1 for record at 16044 against record at 15485
The SQL is:
SELECT COUNT(*) as numb, `AddressLine1` FROM <table> GROUP BY `AddressLine1`
- The column is
varchar(180)
- The table is an InnoDb table
- There are no keys or indexes on this column.
- Table and column collations are both
utf8mb4_unicode_ci
- MariaDB 5.5.68 (I am aware it's old; it's 3rd party)
What this Isn't
I search "Duplicate key 1 for record at ..." and get many answers to similar sounding error labels but which do not seem to relate to this issue, such as INSERT KEY UPDATE
and collation issues.
Typically answers reference the phrase "ERROR 1034: Incorrect key file" but this isn't what our error says.
Further MariaDBs error code listings show:
1034 HY000 ER_NOT_KEYFILE Incorrect key file for table '%s'; try to repair it
What I have done
I have run CHECK TABLE <table>
and it returns ok.
I also try to run repairing the table but this is InnoDB so table repairs are much more fiddly. Nothing else suggests the table is problematic aside from this one error.
I have removed the Primary Key from the column (and the auto_increment
) and then re-added both the Primary Key and auto_increment (in that order). The error still appears and the numbers are consistent (always the same 15485 / 16044 reference points).
Further, removing all indexes (keys) from the table and then readding them does not resolve the issue. The same "rows" throw up this same error.
There are foriegn keys on the table but they don't / should not relate to this column.
My Conclusion So Far
I suspect the table keys need rebuilding. I have seen this Question with numerous answers saying the issue is server space, but that doesn't appear to be the case in our situation.
InnoDB does not allow table repair and reading up about it this looks like a right pain in the poops to carry out (disk imaging etc.). Any advise on repairing InnoDb tables would probably be useful here.
What can I do to fix this issue?
Optimize Table:
As per comment, running
OPTIMIZE TABLE <table>;
Gives
| optimize | note | Table does not support optimize, doing recreate + analyze instead
| optimize | status | OK
As requested, rows 16044 and 15485 are as below:
SELECT * FROM <table> WHERE id IN (16044, 15485)
Results (The data has had to have been cleaned for public display but all <data / AppId / RefId>
values are varchar
):
15485 | <refid> | <AppId> | Rejected | 2012-07-19 | 2012/2013 | 0 | NULL | NULL | NULL | 7 | BAME | Female | Regular | <data> | <data> | 2008-09-01 | 4 | University | University of Hertfordshire | HATFIELD | Club de Havilland | De Havilland Campus | Mosquito Way | United Kingdom | AL10 9EU | 24 | 0 | 0 |
16044 | <refid> | <AppId> | Withdrawn | 2012-08-01 | 2012/2013 | 0 | <data> | 35 | 6 | 2 | NON-BAME | Female | Regular | <data> | <data> | 2009-09-01 | 1 | NULL | The Henley College | HENLEY-ON-THAMES | Deanfield Avenue | NULL | NULL | United Kingdom | RG9 1UH | 19 | 0 | 0
AddressLine1
On each record is:
15485 | Club de Havilland
16044 | Deanfield Avenue
Update re Rick James' Answer;
Dumping, dropping and then reimporting the table does not solve the problem.
And because someone always complains that the whole SQL construction isn't added into the Question, here's the table DDL:
create table <table>
(
id int(8) auto_increment primary key,
various_string_columns varchar(180) null,
various_string_columns varchar(180) null,
...
an_id tinyint unsigned null comment 'reference id ',
a_region tinyint(3) null comment 'A region id from the a_area table ',
CandidateEthnicityId int(3) null,
various_string_columns varchar(180) null,
ee_fromdate date null,
EstablishmentName varchar(180) null,
Establishment_TownCity varchar(180) null,
AddressLine1 varchar(180) null,
AddressLine2 varchar(180) null,
AddressLine3 varchar(180) null,
Country varchar(180) null,
Postcode varchar(180) null,
AgeAtRecord tinyint unsigned null,
ATC_Membership varchar(180) null,
...
various_string_columns varchar(180) null,
...
see_id smallint(6) unsigned null,
constraint a_regionarea
foreign key (a_region) references a_areas (area_id)
on update cascade on delete set null,
constraint afconame
foreign key (a_id) references afco (id)
on update cascade on delete set null
);
create or replace index ApplicationRef
on <table> (ApplicationRef);
create or replace index a_id
on <table> (a_id);
create or replace index a_region
on <table> (a_region);
create or replace index ee_fromdate_2
on <table> (ee_fromdate);
Thanks.