第一次执行时,MySQL查询被暂停

时间:2017-04-06 02:32:25

标签: mysql join indexing

我创建了4个表:

CREATE TABLE `Instruction` (
  `ID` BIGINT(20) NOT NULL AUTO_INCREMENT,
  `CompanyWorkOrderTaskID` VARCHAR(255) CHARACTER SET 'utf8mb4' NULL DEFAULT NULL,
  `CompanyID` CHAR(4) AS (SUBSTRING(CompanyWorkOrderTaskID, 1, 4)) STORED,
  `WorkOrderID` CHAR(8) AS (SUBSTRING(CompanyWorkOrderTaskID, 5, 8)) STORED,
  `WorkOrderTaskID` CHAR(3) AS (SUBSTRING(CompanyWorkOrderTaskID, 13, 3)) STORED,
  PRIMARY KEY (`ID`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;

CREATE TABLE `Company` (
  `keyCompany` BIGINT(20) NOT NULL AUTO_INCREMENT,
  `CompanyID` VARCHAR(255) CHARACTER SET 'utf8mb4' NOT NULL,
  `CompanyName` VARCHAR(255) CHARACTER SET 'utf8mb4' NOT NULL,
  PRIMARY KEY (`keyCompany`))
ENGINE = InnoDB
AUTO_INCREMENT = 5
DEFAULT CHARACTER SET = latin1;

CREATE TABLE `WorkOrder` (
  `keyWorkOrder` BIGINT(20) NOT NULL AUTO_INCREMENT,
  `WorkOrderID` VARCHAR(255) CHARACTER SET 'utf8mb4' NOT NULL,
  `WorkOrderDescription` VARCHAR(255) CHARACTER SET 'utf8mb4' NULL DEFAULT NULL,
  PRIMARY KEY (`keyWorkOrder`))
ENGINE = InnoDB
AUTO_INCREMENT = 5
DEFAULT CHARACTER SET = latin1;

CREATE TABLE `WorkOrderTask` (
  `keyWorkOrderTask` BIGINT(20) NOT NULL AUTO_INCREMENT,
  `yekWorkOrder` BIGINT(20) NOT NULL,
  `WorkOrderTaskID` VARCHAR(255) CHARACTER SET 'utf8mb4' NOT NULL,
  `WorkOrderTaskDescription` VARCHAR(255) CHARACTER SET 'utf8mb4' NULL DEFAULT NULL,
  PRIMARY KEY (`keyWorkOrderTask`))
ENGINE = InnoDB
AUTO_INCREMENT = 5
DEFAULT CHARACTER SET = latin1;

CREATE UNIQUE INDEX `AKCompanyID` ON `Company` (`CompanyID` ASC);
CREATE UNIQUE INDEX `AKyekCompanyWorkOrderID` ON `WorkOrder` (`yekCompany` ASC, `WorkOrderID` ASC);
CREATE UNIQUE INDEX `AKyekWorkOrderWorkOrderTaskID` ON `WorkOrderTask` (`yekWorkOrder` ASC, `WorkOrderTaskID` ASC);
CREATE INDEX Index1 ON Instruction (CompanyID);
CREATE INDEX Index2 ON Instruction (WorkOrderID);
CREATE INDEX Index3 ON Instruction (WorkOrderTaskID);

然后我插入表的数据并运行查询

SELECT COUNT(1)
FROM `Instruction` J
           INNER JOIN `Company`       C   ON C.`CompanyID` = J.`CompanyID`
           INNER JOIN `WorkOrder`     WO  ON WO.`yekCompany`    = C.`keyCompany` AND WO.`WorkOrderID` = J.`WorkOrderID`
           INNER JOIN `WorkOrderTask` WOT ON WOT.`yekWorkOrder` = WO.`keyWorkOrder` AND WOT.`WorkOrderTaskID` = J.`WorkOrderTaskID`

查询提供8小时。如果我终止进程并再次运行,则查询在15秒内完成。

我已经创建了加入表的所有索引。仅在我第一次运行查询时才会出现此问题。从第二次起,它非常快。

指令,WorkOrder,WorkOrderTask,Company表分别有840845,168623,181233和2行。

我使用的是MySQL 5.7.16版本

0 个答案:

没有答案