为什么这个MySQL插入这么慢?

时间:2012-12-01 10:33:44

标签: mysql optimization

CREATE  TABLE IF NOT EXISTS `church` (
  `church_id` INT(11) NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(40) NOT NULL ,
  `city_id` SMALLINT(4) UNSIGNED ZEROFILL NOT NULL ,
  `validated` TINYINT(1) NULL ,
  PRIMARY KEY (`church_id`) ,
  INDEX `church.city_id-city.city_id_idx` (`city_id` ASC) ,
  UNIQUE INDEX `church-city_id` (`name` ASC, `city_id` ASC) ,
  CONSTRAINT `church.city_id-city.city_id`
    FOREIGN KEY (`city_id` )
    REFERENCES `city` (`city_id` )
    ON DELETE RESTRICT
    ON UPDATE RESTRICT)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8

我的localhost(笔记本电脑)上有这个表church。当我对它进行插入时(它现在有10行),需要160毫秒。

  • church_id在其他表中用作外键。
  • 名称和city_id应该是唯一的。
  • city_id是外键

这不是很多,而且我做错了什么?

1 个答案:

答案 0 :(得分:3)

插入必须做很多工作:

  • 锁定表格
  • 检查唯一索引约束
  • 检查外键约束
  • 撰写新记录
  • 更新索引
  • 解锁表格
  • 编写交易日志