We have a suspicion that sometimes there could be non-unique data even with a UNIQUE
index. Question is, can we rely on InnoDB guaranteeing that there won't ever be non-unique data, even in the presence of UNIQUE_CHECKS=0
, so there is no reason to check it?
Edited to add: If it is possible to have non-unique index entries for a UNIQUE
index in InnoDB (even with cheating), how is such a test-case created?
Or is it like foreign key constraints and InnoDB, where that guarantee can be broken by setting FOREIGN_KEY_CHECKS=0
, so that if we want to be sure, we should check it with SELECT
statements?
The documentation for UNIQUE_CHECKS
:
Setting this variable to 0 does not require storage engines to ignore duplicate keys. An engine is still permitted to check for them and issue duplicate-key errors if it detects them.
documentation for Bulk Data Loading for InnoDB Tables:
If you have UNIQUE constraints on secondary keys, you can speed up table imports by temporarily turning off the uniqueness checks during the import session:
SET unique_checks=0; ... SQL import statements ... SET unique_checks=1;
For big tables, this saves a lot of disk I/O because InnoDB can use its change buffer to write secondary index records in a batch. Be certain that the data contains no duplicate keys.
These seem to imply that UNIQUE_CHECKS=0
can be used to break the uniqueness guarantee.
MariaDB Enterprise documentation for unique_checks:
Set to 0 to speed up imports of large tables to InnoDB. The storage engine will still issue a duplicate key error if it detects one, even if set to 0.
This seems to imply that the guarantee holds, even with UNIQUE_CHECKS=0
.
This example SQL demonstrates UNIQUE_CHECKS=0
is ignored and uniqueness seems to be enforced still:
DROP TABLE IF EXISTS `FOO`;
CREATE TABLE `FOO` (
`foo` VARCHAR(255) NOT NULL,
UNIQUE INDEX (`foo`)
) ENGINE=InnoDB;
INSERT INTO FOO (foo) VALUES ('A');
SET UNIQUE_CHECKS=0;
-- This generates the error:
-- ERROR 1062 (23000) at line 11: Duplicate entry 'A' for key 'foo'
-- even though UNIQUE_CHECKS=0
INSERT INTO FOO (foo) VALUES ('A');
(Edit: Simplified example to only demonstrate that UNIQUE_CHECKS=0
is ignored.)
So which is it: Can we rely on InnoDB guaranteeing that there won't ever be non-unique data, even in the presence of UNIQUE_CHECKS=0
, so there is no reason to check it?
We're using MySQL version 5.7.23.