当我在 select 子句中同时具有主键列和非主键列时,Extra 下的 EXPLAIN 输出显示为空,如下所示: 查询:
EXPLAIN SELECT aggEI.c_id AS companyId,aggEI.uid AS uuid,
aggEI.rating AS rating, aggEI.ei_name AS name
FROM AGG_EI AS aggEI
where aggEI.c_id in (8)
1 SIMPLE aggEI ref agg_ei_comdm_fk_idx agg_ei_comdm_fk_idx 8 const 65909
在上面的查询中 c_id,uid 列是主键的一部分,而 rating,name 不是主键的一部分。如果我从选择子句查询中删除非主键,则使用如下索引:
EXPLAIN SELECT aggEI.c_id AS companyId,
aggEI.c_id AS uuid
FROM AGG_EXTERNALINDIVIDUAL AS aggEI
where aggEI.c_id in (8)
1 SIMPLE aggEI ref agg_ei_comdm_fk_idx agg_ei_comdm_fk_idx 8 const 65909 Using index
其中 agg_ei_comdm_fk_idx 是列 c_id 的外键索引。有人可以向我解释这种行为。我需要什么索引组合才能使查询使用索引
创建表:
CREATE TABLE IF NOT EXISTS `AGG_EI` (
`c_id` BIGINT NOT NULL ,
`uid` VARCHAR(150) NOT NULL ,
`ei_name` VARCHAR(150) NOT NULL ,
`rating` DOUBLE NULL ,
`cnt` DOUBLE NULL ,
PRIMARY KEY ( `c_id`, `uid`) ,
INDEX `agg_ei_comdm_fk_idx` (`c_id` ASC) ,
UNIQUE INDEX `id_UNIQUE` ( `c_id` ASC, `uid` ASC) ,
CONSTRAINT `agg_ei_comdm_fk`
FOREIGN KEY (`c_id` )
REFERENCES `COMPDM` (`c_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
ENGINE = InnoDB;