如何避免我的MySQL查询的全表扫描

时间:2016-11-01 05:26:06

标签: mysql

有人可以帮助我更改此查询吗

UPDATE APA_Pended_Demand 
SET APA_Pended_Demand.genericDemandId=( 
    SELECT APA_Generic_Demand_Details.genericDemandId  
    FROM APA_Generic_Demand_Details  
    WHERE APA_Generic_Demand_Details.demandID=APA_Pended_Demand.demandID  
    AND APA_Pended_Demand.isPend = 1 
    AND APA_Generic_Demand_Details.genericDemandId != ''  
    AND APA_Generic_Demand_Details.genericDemandId IS NOT NULL  
    AND APA_Pended_Demand.reactivateDate > UTC_TIMESTAMP()
    AND APA_Generic_Demand_Details.status < 300
    AND APA_Pended_Demand.ID BETWEEN 1 AND 10000

为表格提供的索引是

PRIMARY KEY (`id`),
 KEY `apgnricdemnddetils_bndnresn` (`abandonReason`),
 KEY `apgnricdmnddetils_prdcttype` (`productType`),
 KEY `apgnricdmnddtils_srcereqrle` (`sourceReqRole`),
 KEY `apgnericdemnddetils_methdin` (`methodIn`),
 KEY `IX1_APA_Generic_Demand_Details` (`status`,`customerReference`,`policyNo`,`genericDemandId`) USING BTREE,
 KEY `IX2_APA_Generic_Demand_Details` (`status`,`schemeReference`,`genericDemandId`) USING BTREE,
 KEY `IX3_APA_Generic_Demand_Details` (`status`,`aggregationKey`,`genericDemandId`) USING BTREE,
 KEY `IX4_APA_Generic_Demand_Details` (`status`,`initiator`,`aggregationKey`,`genericDemandId`) USING BTREE,
 KEY `IX5_APA_Generic_Demand_Details` (`status`,`policyNo`,`genericDemandId`) USING BTREE,
 KEY `IX7_APA_Generic_Demand_Details` (`genericDemandId`) USING BTREE,
 KEY `IX8_APA_Generic_Demand_Details` (`initiator`,`status`) USING BTREE, 

查询未使用index.I尝试解释查询,它显示类型为All,键显示为None。那么无论如何要让它使用索引还是我们需要更改查询?

1 个答案:

答案 0 :(得分:1)

改为使用更新联接:

UPDATE APA_Pended_Demand t1
INNER JOIN APA_Generic_Demand_Details t2
    ON t1.demandID = t2.demandID
SET t1.genericDemandId = t2.genericDemandId
WHERE t1.isPend = 1 AND
      COALESCE(t2.genericDemandId, '') != '' AND
      t1.reactivateDate > UTC_TIMESTAMP() AND
      t2.status < 300 AND
      t1.ID BETWEEN 1 AND 10000