问题
We're having a problem where a mysqldump script is spending 90% of it's time populating a small handful of the tables it deals with. Eliminating FK's and indexes eliminates the speed problem, but is not an acceptable solution.
The dump script does have:
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
Can we expect any different behavior from ALTER TABLE foo DISABLE KEYS
?
Also, is disable keys
session-scoped or is it permanent until I re-enable it? Can I disable keys from one mysql session and have it effect the import issued from another session?
回答1:
Yes, you should get significant benefits out of DISABLE KEYS
. It isn't session-scoped, it's a table property, so your keys will be dead for everybody until you do ENABLE KEYS
.
回答2:
DISABLE KEYS
is MyISAM only:
If you use ALTER TABLE on a MyISAM table, all nonunique indexes are created in a separate batch (as for REPAIR TABLE). This should make ALTER TABLE much faster when you have many indexes.
This feature can be activated explicitly for a MyISAM table. ALTER TABLE ... DISABLE KEYS tells MySQL to stop updating nonunique indexes. ALTER TABLE ... ENABLE KEYS then should be used to re-create missing indexes. MySQL does this with a special algorithm that is much faster than inserting keys one by one, so disabling keys before performing bulk insert operations should give a considerable speedup. Using ALTER TABLE ... DISABLE KEYS requires the INDEX privilege in addition to the privileges mentioned earlier.
While the nonunique indexes are disabled, they are ignored for statements such as SELECT and EXPLAIN that otherwise would use them.
from ALTER TABLE Syntax
Using DISABLE KEYS
with any other storage engine results in a warning:
mysql> ALTER TABLE `foo` DISABLE KEYS;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show warnings \G
*************************** 1. row ***************************
Level: Note
Code: 1031
Message: Table storage engine for 'foo' doesn't have this option
1 row in set (0.00 sec)
来源:https://stackoverflow.com/questions/1307476/mysql-set-unique-checks-set-foreign-key-checks-vs-alter-table-disable-key