Mysql innerjoin很慢

时间:2015-11-11 10:41:46

标签: mysql sql nhibernate

我有一个SQL查询需要4个多小时才能完成。

查询如下

UPDATE carepathaction a1 
       INNER JOIN carepathaction a2 
               ON a1.sourceoid = a2.oid 
       INNER JOIN carepathgroup g 
               ON g.sourceoid = a2.carepathgroupoid 
       INNER JOIN carepathphase f 
               ON f.oid = g.carepathphaseoid 
SET    a1.carepathgroupoid = g.oid 
WHERE  f.code = 'EDIT' 

他必须更新65000件物品。我可以在此查询中进行一些优化吗? CarePathAction

CREATE TABLE `carepathaction` 
  ( 
     `oid`                                INT(11) NOT NULL, 
     `typeid`                             CHAR(1) NOT NULL DEFAULT 'X', 
     `sequencenumber`                     INT(11) NOT NULL, 
     `description`                        VARCHAR(1024) NOT NULL, 
     `duration`                           INT(11) NOT NULL comment 'In minutes', 
     `deadline`                           INT(11) NOT NULL comment 
'# dagen in combinatie met deadlineType (sinds begin traject of sinds begin deze actie)' 
     , 
`deadlinetype`                       INT(11) NOT NULL comment 
'1) = deadline sinds zorgpad\n2) = deadline sinds start deze actie\n3) = deadline sinds zorgpadfase'
     , 
`repeatperweek`                      INT(11) DEFAULT NULL, 
`numbertorepeat`                     INT(11) DEFAULT NULL, 
`carepathoid`                        INT(11) DEFAULT NULL, 
`usertaskoid`                        INT(11) DEFAULT NULL, 
`templateattachment`                 VARCHAR(45) DEFAULT NULL, 
`partyrolecategoryoid`               INT(11) DEFAULT NULL, 
`automatic`                          BIT(1) NOT NULL DEFAULT b'0', 
`carepathphaseoid`                   INT(11) DEFAULT NULL, 
`parentoid`                          INT(11) DEFAULT NULL, 
`documentoid`                        INT(11) DEFAULT NULL, 
`epdtemplateoid`                     INT(11) DEFAULT NULL, 
`producttypeoid`                     INT(11) DEFAULT NULL, 
`evaluateby`                         INT(11) DEFAULT NULL, 
`pathlock`                           BIT(1) NOT NULL DEFAULT b'0', 
`carepathgroupoid`                   INT(11) NOT NULL, 
`activitygroupoid`                   INT(11) DEFAULT NULL, 
`title`                              VARCHAR(45) DEFAULT NULL, 
`carepathactionswitchtype`           INT(11) DEFAULT NULL, 
`modulesdomaincode`                  VARCHAR(10) DEFAULT NULL, 
`startday`                           INT(11) NOT NULL DEFAULT '0', 
`sourceoid`                          INT(11) DEFAULT NULL, 
`templatefilename`                   VARCHAR(100) DEFAULT NULL, 
`istask`                             BIT(1) NOT NULL DEFAULT b'0', 
`typecontact`                        INT(11) DEFAULT NULL, 
`carepathactionepdoid`               INT(11) DEFAULT NULL, 
`carepathactiontreatmentplanoid`     INT(11) DEFAULT NULL, 
`clientportalenabled`                BIT(1) DEFAULT NULL, 
`startafternumberofdaysepddatefield` INT(11) DEFAULT NULL, 
`startafterdateepdfieldoid`          INT(11) DEFAULT NULL, 
`epdtemplatefieldoid`                INT(11) DEFAULT NULL, 
     PRIMARY KEY (`oid`), 
     KEY `carepathactionfk1_idx` (`carepathoid`), 
     KEY `carepathactionfk2_idx` (`carepathphaseoid`), 
     KEY `carepathactionfk3_idx` (`documentoid`), 
     KEY `carepathactionfk4_idx` (`epdtemplateoid`), 
     KEY `carepathactionfk5_idx` (`producttypeoid`), 
     KEY `carepathactionfk6_idx` (`carepathgroupoid`), 
     KEY `carepathactionfk6_idx1` (`activitygroupoid`), 
     KEY `carepathactionfk7_idx` (`parentoid`), 
     KEY `carepathactionfk8_idx` (`usertaskoid`), 
     KEY `carepathactionfk10_idx` (`partyrolecategoryoid`), 
     KEY `carepathactionfk11_idx` (`carepathactionepdoid`), 
     KEY `carepathactionfk12_idx` (`carepathactiontreatmentplanoid`), 
     KEY `carepathactionfk13_idx` (`startafterdateepdfieldoid`), 
     KEY `carepathactionfk14_idx` (`epdtemplatefieldoid`), 
     CONSTRAINT `carepathactionfk14` FOREIGN KEY (`epdtemplatefieldoid`) 
     REFERENCES `epdtemplatefield` (`oid`) ON DELETE no action ON UPDATE no 
     action, 
     CONSTRAINT `carepathactionfk1` FOREIGN KEY (`carepathoid`) REFERENCES 
     `carepath` (`oid`) ON DELETE no action ON UPDATE no action, 
     CONSTRAINT `carepathactionfk10` FOREIGN KEY (`partyrolecategoryoid`) 
     REFERENCES `partyrolecategory` (`oid`) ON DELETE no action ON UPDATE no 
     action, 
     CONSTRAINT `carepathactionfk11` FOREIGN KEY (`carepathactionepdoid`) 
     REFERENCES `carepathaction` (`oid`) ON DELETE no action ON UPDATE no action 
     , 
     CONSTRAINT `carepathactionfk12` FOREIGN KEY ( 
     `carepathactiontreatmentplanoid`) REFERENCES `carepathaction` (`oid`) ON 
     DELETE no action ON UPDATE no action, 
     CONSTRAINT `carepathactionfk13` FOREIGN KEY (`startafterdateepdfieldoid`) 
     REFERENCES `carepathaction` (`oid`) ON DELETE no action ON UPDATE no action 
     , 
     CONSTRAINT `carepathactionfk2` FOREIGN KEY (`carepathphaseoid`) REFERENCES 
     `carepathphase` (`oid`) ON DELETE no action ON UPDATE no action, 
     CONSTRAINT `carepathactionfk3` FOREIGN KEY (`documentoid`) REFERENCES 
     `document` (`oid`) ON DELETE no action ON UPDATE no action, 
     CONSTRAINT `carepathactionfk4` FOREIGN KEY (`epdtemplateoid`) REFERENCES 
     `epdtemplate` (`oid`) ON DELETE no action ON UPDATE no action, 
     CONSTRAINT `carepathactionfk5` FOREIGN KEY (`producttypeoid`) REFERENCES 
     `producttype` (`oid`) ON DELETE no action ON UPDATE no action, 
     CONSTRAINT `carepathactionfk6` FOREIGN KEY (`carepathgroupoid`) REFERENCES 
     `carepathgroup` (`oid`) ON DELETE no action ON UPDATE no action, 
     CONSTRAINT `carepathactionfk7` FOREIGN KEY (`parentoid`) REFERENCES 
     `carepathaction` (`oid`) ON DELETE no action ON UPDATE no action, 
     CONSTRAINT `carepathactionfk8` FOREIGN KEY (`usertaskoid`) REFERENCES 
     `tasktoschedule` (`oid`) ON DELETE no action ON UPDATE no action 
  ) 

CarePathGroup

CREATE TABLE `carepathgroup` 
  ( 
     `oid`              INT(11) NOT NULL, 
     `sequencenumber`   INT(11) NOT NULL, 
     `title`            VARCHAR(50) NOT NULL, 
     `carepathoid`      INT(11) DEFAULT NULL, 
     `carepathphaseoid` INT(11) NOT NULL, 
     `sourceoid`        INT(11) DEFAULT NULL, 
     PRIMARY KEY (`oid`), 
     KEY `carepathgroupfk1_idx` (`carepathoid`), 
     KEY `carepathgroupfk2_idx` (`carepathphaseoid`), 
     CONSTRAINT `carepathgroupfk2` FOREIGN KEY (`carepathphaseoid`) REFERENCES 
     `carepathphase` (`oid`) ON DELETE no action ON UPDATE no action 
  ) 

CarePathPhase

CREATE TABLE `carepathphase` 
  ( 
     `oid`               INT(11) NOT NULL, 
     `code`              VARCHAR(6) NOT NULL, 
     `description`       VARCHAR(45) NOT NULL, 
     `sequencenumber`    INT(11) NOT NULL DEFAULT '0', 
     `carepathoid`       INT(11) DEFAULT NULL, 
     `carepathphasetype` INT(11) NOT NULL DEFAULT '0', 
     `sourceoid`         INT(11) DEFAULT NULL, 
     PRIMARY KEY (`oid`), 
     KEY `carepathphasefk1_idx` (`carepathoid`), 
     CONSTRAINT `carepathphasefk1` FOREIGN KEY (`carepathoid`) REFERENCES 
     `carepath` (`oid`) ON DELETE no action ON UPDATE no action 
  ) 

解释

1   SIMPLE  f   ALL PRIMARY             61925   Using where
1   SIMPLE  g   ref CarePathGroupFK2_idx    CarePathGroupFK2_idx    4   amis.f.Oid  1   
1   SIMPLE  a1  ALL                 166582  Using join buffer
1   SIMPLE  a2  eq_ref  PRIMARY,CarePathActionFK6_idx   PRIMARY 4   amis.a1.SourceOid   1   Using where

1 个答案:

答案 0 :(得分:0)

解释中有两种ALL连接类型,这些是您需要关注的类型(有关详细信息,请参阅mysql documentation on explain output)。

create table语句显示:

  1. 您在carepathphase.code字段中没有索引,该字段在where条件中使用,因此您应该纠正它。

  2. 在carepathaction.sourceoid上没有索引,它在连接中使用,所以你应该在那里创建一个索引。您可以尝试在oid,sourceoid字段(或其他方式)上创建组合索引,因为mysql可能能够使用组合索引来匹配行。

  3. 在添加索引后运行解释选择(如果你有mysql v5.6或更新,然后你可以直接在更新上运行它),看看,如果ALL改变了。在a2旁边的第二个似乎是对我来说真正重要的东西。