子查询“有点慢”如何改进?

时间:2012-05-30 20:35:37

标签: mysql performance join subquery

我已经和MySQL一起工作了一段时间,现在我遇到了一些子查询的问题,需要60秒才能执行! :( 我应该改变这个,因为它似乎“有点慢”,而且我不是SQL专家,所以我想我会问是否有更聪明的方法来做到这一点。这是当前的查询:

SELECT VAR_ID, VAR_REF, DET_VAR_NOMB, DET_VAR_IMG, DET_VAR_MEDIDA, DET_VAR_DETALLE, SEN_FECH, SEN_VALOR
FROM variable, detalle_variable, senal
WHERE equipo_EQO_ID =6
AND detalle_variable_DET_VAR_ID = DET_VAR_ID
AND variable_VAR_ID = VAR_ID
AND SEN_ID
IN (
    SELECT MAX( SEN_ID ) 
    FROM senal
    GROUP BY variable_VAR_ID
)

查询结果是:(这就是我想要选择的)

VAR_ID VAR_REF DET_VAR_NOMB DET_VAR_IMG DET_VAR_MEDIDA DET_VAR_DETALLE SEN_FECH SEN_VALOR

8 101 vsth ../../img/foto/variable/play_blue.png东西2012-05-30 03:14:17 16

表格如下:

-- -----------------------------------------------------
-- Table `pgssgc_stap2`.`detalle_variable`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `pgssgc_stap2`.`detalle_variable` ;

CREATE  TABLE IF NOT EXISTS `pgssgc_stap2`.`detalle_variable` (
  `DET_VAR_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
  `DET_VAR_NOMB` VARCHAR(45) NOT NULL ,
  `DET_VAR_MEDIDA` VARCHAR(45) NULL DEFAULT NULL ,
  `DET_VAR_DETALLE` VARCHAR(255) NULL DEFAULT NULL ,
  `DET_VAR_IMG` VARCHAR(255) NULL DEFAULT NULL ,
  PRIMARY KEY (`DET_VAR_ID`) )
ENGINE = InnoDB
AUTO_INCREMENT = 101
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;



-- -----------------------------------------------------
-- Table `pgssgc_stap2`.`variable`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `pgssgc_stap2`.`variable` ;

CREATE  TABLE IF NOT EXISTS `pgssgc_stap2`.`variable` (
  `VAR_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
  `VAR_REF` VARCHAR(45) NOT NULL ,
  `detalle_variable_DET_VAR_ID` INT(10) UNSIGNED NOT NULL ,
  `equipo_EQO_ID` INT(10) UNSIGNED NOT NULL ,
  PRIMARY KEY (`VAR_ID`) ,
  INDEX `fk_variable_detalle_variable1` (`detalle_variable_DET_VAR_ID` ASC) ,
  INDEX `fk_variable_equipo1` (`equipo_EQO_ID` ASC) ,
  CONSTRAINT `fk_variable_detalle_variable1`
    FOREIGN KEY (`detalle_variable_DET_VAR_ID` )
    REFERENCES `pgssgc_stap2`.`detalle_variable` (`DET_VAR_ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_variable_equipo1`
    FOREIGN KEY (`equipo_EQO_ID` )
    REFERENCES `pgssgc_stap2`.`equipo` (`EQO_ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 11
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;


-- -----------------------------------------------------
-- Table `pgssgc_stap2`.`senal`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `pgssgc_stap2`.`senal` ;

CREATE  TABLE IF NOT EXISTS `pgssgc_stap2`.`senal` (
  `SEN_ID` INT(11) NOT NULL AUTO_INCREMENT ,
  `variable_VAR_ID` INT(10) UNSIGNED NOT NULL ,
  `SEN_FECH` DATETIME NULL DEFAULT NULL ,
  `SEN_VALOR` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`SEN_ID`) ,
  INDEX `fk_senal_variable1` (`variable_VAR_ID` ASC) ,
  CONSTRAINT `fk_senal_variable1`
    FOREIGN KEY (`variable_VAR_ID` )
    REFERENCES `pgssgc_stap2`.`variable` (`VAR_ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 2086
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

感谢您的回复。

1 个答案:

答案 0 :(得分:0)

假设您只对子查询有问题。

确保表variable_VAR_ID的{​​{1}}列为senal