Mysql外键检查花了太多时间

时间:2015-01-12 18:45:44

标签: mysql

SELECT
    *
FROM products
WHERE products.deleted_at is null
    AND products.section_id = '1'
    AND products.status = '2'
ORDER BY products.created_at DESC
LIMIT 10 OFFSET 0

时间:3秒

SELECT
    *
FROM products
WHERE products.deleted_at is null
    AND products.status = '2'
ORDER BY products.created_at DESC
LIMIT 10 OFFSET 0

时间:0.2秒

为什么这个外键检查(where section_id='1')占用了太多时间? section_id指的是id部分表。 表结构是:

CREATE TABLE IF NOT EXISTS `bishal`.`products` (
  ......
  `section_id` INT(10) UNSIGNED NOT NULL,
  ......
  `status` TINYINT(4) NOT NULL,
  ......
  `deleted_at` TIMESTAMP NULL DEFAULT NULL,
  `created_at` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
  ......
  INDEX `products_section_id_index` (`section_id` ASC),
  ......
  CONSTRAINT `products_section_id_foreign`
    FOREIGN KEY (`section_id`)
    REFERENCES `bishal`.`sections` (`id`),
  ......
ENGINE = InnoDB
AUTO_INCREMENT = 225532
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;

0 个答案:

没有答案