MySQL “set unique_checks”, “set foreign_key_checks” vs. “alter table disable keys”

匆匆过客 提交于 2019-12-22 06:50:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!