2015-11-11 98 views
0

我有一個SQL查詢需要4個多小時才能完成。Mysql innerjoin非常慢

查詢是如下

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

請發佈連接中所有表的創建表,以便我們可以看到索引。請將查詢改爲select,並在此處發佈解釋的結果。 – Shadow

+0

我希望這是足夠的信息?選擇有65000行,所以我不能在這裏發佈 – joey

+0

不,這是不夠的。我明確要求創建表語句來查看索引。我沒有要求選擇的結果被披露,我請求你對選擇進行解釋併發布解釋的結果。 – Shadow

回答

0

有兩種全部加入類型的解釋,這是那些你需要關注(詳情見mysql documentation on explain output)。

在CREATE TABLE語句表明:

  1. 您不必對carepathphase.code場,這是在哪裏標準使用,所以你應該糾正這一指數。

  2. carepathaction.sourceoid沒有用於連接的索引,所以您應該在那裏創建一個。您可以嘗試在oid,sourceoid字段(或其他方法)上創建組合索引,因爲mysql可能能夠使用組合索引來匹配行。

運行上的選擇說明添加索引後,如果承滴盤變化(如果你有mysql V5.6或更高版本,那麼你可以直接在更新運行),看看。 a2旁邊的第二個對我來說似乎是真正關鍵的東西。